Skip to content

Commit

Permalink
Add free orbit camera control
Browse files Browse the repository at this point in the history
  • Loading branch information
thatcomputerguy0101 committed Sep 7, 2024
1 parent fdf6f0a commit 0a5e9aa
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/renderer/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ mod orbit_control;
#[doc(inline)]
pub use orbit_control::*;

mod free_orbit_control;
#[doc(inline)]
pub use free_orbit_control::*;

mod first_person_control;
#[doc(inline)]
pub use first_person_control::*;
Expand Down
44 changes: 44 additions & 0 deletions src/renderer/control/free_orbit_control.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use crate::renderer::*;

///
/// A control that makes the camera orbit around a target, with no fixed up direction.
///
pub struct FreeOrbitControl {
control: CameraControl,
}

impl FreeOrbitControl {
/// Creates a new free orbit control with the given target and minimum and maximum distance to the target.
pub fn new(target: Vec3, min_distance: f32, max_distance: f32) -> Self {
Self {
control: CameraControl {
left_drag_horizontal: CameraAction::FreeOrbitLeft { target, speed: 0.1 },
left_drag_vertical: CameraAction::FreeOrbitUp { target, speed: 0.1 },
scroll_vertical: CameraAction::Zoom {
min: min_distance,
max: max_distance,
speed: 0.1,
target,
},
..Default::default()
},
}
}

/// Handles the events. Must be called each frame.
pub fn handle_events(&mut self, camera: &mut Camera, events: &mut [Event]) -> bool {
if let CameraAction::Zoom { speed, target, .. } = &mut self.control.scroll_vertical {
let x = target.distance(*camera.position());
*speed = 0.01 * x + 0.001;
}
if let CameraAction::FreeOrbitLeft { speed, target } = &mut self.control.left_drag_horizontal {
let x = target.distance(*camera.position());
*speed = 0.01 * x + 0.001;
}
if let CameraAction::FreeOrbitUp { speed, target } = &mut self.control.left_drag_vertical {
let x = target.distance(*camera.position());
*speed = 0.01 * x + 0.001;
}
self.control.handle_events(camera, events)
}
}

0 comments on commit 0a5e9aa

Please sign in to comment.