diff options
author | HampusM <hampus@hampusmat.com> | 2023-10-26 20:45:47 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2023-10-26 20:45:47 +0200 |
commit | 76a3a021402e05823e1f82fd592c853a4a6be563 (patch) | |
tree | 8c35c05db9f915630c5ca28d36e148bbc2f9a0c6 | |
parent | 87897e8d51b26c5c9f6c2fc1ab7d07d6d4a9d5e2 (diff) |
feat: add rotate around center with input
-rw-r--r-- | src/main.rs | 41 |
1 files changed, 39 insertions, 2 deletions
diff --git a/src/main.rs b/src/main.rs index e4afeac..ad59b38 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,7 +4,7 @@ use engine::color::Color; use engine::object::{Id as ObjectId, Object}; use engine::vector::Vec3; use engine::vertex::Builder as VertexBuilder; -use engine::{Engine, WindowSize}; +use engine::{Engine, Key, WindowSize}; use tracing::Level; use tracing_subscriber::FmtSubscriber; @@ -128,8 +128,45 @@ fn main() -> Result<(), Box<dyn Error>> engine.add_object(square); + let cam_speed = 3.0; + engine.start(|engine| { - let obj = engine.get_object_by_id_mut(ObjectId::new(1)); + let delta_time = *engine.delta_time(); + + if engine.is_key_pressed(Key::W).unwrap() { + let cam_target_direction = engine.camera().target_direction().clone(); + + *engine.camera_mut().position_mut() += + cam_target_direction * cam_speed * delta_time.as_secs_f32(); + } + if engine.is_key_pressed(Key::S).unwrap() { + let rev_cam_target_direction = -engine.camera().target_direction().clone(); + + *engine.camera_mut().position_mut() += + rev_cam_target_direction * cam_speed * delta_time.as_secs_f32(); + } + if engine.is_key_pressed(Key::A).unwrap() { + let cam_left = engine.camera().left().clone(); + + // Camera speed adjusted to be same no matter how far the distance is to the + // camera target + let cam_speed_dist_adj = cam_speed + * (engine.camera().position() - engine.camera().target()).length(); + + *engine.camera_mut().position_mut() += + cam_left * cam_speed_dist_adj * delta_time.as_secs_f32(); + } + if engine.is_key_pressed(Key::D).unwrap() { + let cam_right = engine.camera().right().clone(); + + // Camera speed adjusted to be same no matter how far the distance is to the + // camera target + let cam_speed_dist_adj = cam_speed + * (engine.camera().position() - engine.camera().target()).length(); + + *engine.camera_mut().position_mut() += + cam_right * cam_speed_dist_adj * delta_time.as_secs_f32(); + } })?; Ok(()) |