From 76a3a021402e05823e1f82fd592c853a4a6be563 Mon Sep 17 00:00:00 2001 From: HampusM Date: Thu, 26 Oct 2023 20:45:47 +0200 Subject: feat: add rotate around center with input --- src/main.rs | 41 +++++++++++++++++++++++++++++++++++++++-- 1 file 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> 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(()) -- cgit v1.2.3-18-g5258