Simplify camera

This commit is contained in:
Aevyrie 2023-07-18 22:54:42 -07:00
parent 347f51a5d6
commit 64e70471d9
2 changed files with 15 additions and 8 deletions

View File

@ -45,9 +45,10 @@ fn setup(
},
GridCell::<i128>::default(), // All spatial entities need this component
FloatingOrigin, // Important: marks this as the entity to use as the floating origin
CameraController::default()
CameraController::default() // Built-in camera controller
.with_max_speed(10e35)
.with_smoothness(0.9, 0.8), // Built-in camera controller
.with_smoothness(0.95, 0.9)
.with_speed(1.5),
));
let mesh_handle = meshes.add(
@ -182,7 +183,7 @@ fn closest<'a>(diameter: f32) -> (f32, &'a str) {
(12e6, "diameter of Earth"),
(3e6, "diameter of the Moon"),
(9e3, "height of Mt. Everest"),
(2e0, "height of a human"),
(1.8e0, "height of a human"),
(1e-1, "size of a cat"),
(1e-3, "size of an insect"),
(1e-4, "diameter of a eukaryotic cell"),

View File

@ -38,7 +38,7 @@ pub struct CameraController {
/// Rotational smoothness, from `0.0` to `1.0`.
pub rotational_smoothness: f64,
/// Maximum possible speed.
pub max_speed: f64,
pub speed: f64,
/// Whether the camera should slow down when approaching an entity's [`Aabb`].
pub slow_near_objects: bool,
nearest_object: Option<(Entity, f64)>,
@ -56,7 +56,7 @@ impl CameraController {
/// Sets the `max_speed` parameter of the controller, and returns the modified result.
pub fn with_max_speed(mut self, max_speed: f64) -> Self {
self.max_speed = max_speed;
self.speed = max_speed;
self
}
@ -66,6 +66,12 @@ impl CameraController {
self
}
/// Sets the speed of the controller, and returns the modified result.
pub fn with_speed(mut self, speed: f64) -> Self {
self.speed = speed;
self
}
/// Returns the translational and rotational velocity of the camera.
pub fn velocity(&self) -> (DVec3, DQuat) {
(self.vel_translation, self.vel_rotation)
@ -82,7 +88,7 @@ impl Default for CameraController {
Self {
smoothness: 0.8,
rotational_smoothness: 0.5,
max_speed: 10e8,
speed: 10e8,
slow_near_objects: true,
nearest_object: None,
vel_translation: DVec3::ZERO,
@ -195,8 +201,8 @@ pub fn camera_controller<P: GridPrecision>(
for (mut cam_transform, mut controller, mut cell) in camera.iter_mut() {
let speed = match (controller.nearest_object, controller.slow_near_objects) {
(Some(nearest), true) => nearest.1.abs(),
_ => controller.max_speed,
} * (1.0 + input.boost as usize as f64);
_ => controller.speed,
} * (controller.speed + input.boost as usize as f64);
let lerp_translation = 1.0 - controller.smoothness.clamp(0.0, 0.999);
let lerp_rotation = 1.0 - controller.rotational_smoothness.clamp(0.0, 0.999);