Define a minimum and maximum pitch (#149)

This commit is contained in:
Max Ammann 2022-12-13 17:52:02 +01:00 committed by GitHub
parent f780529109
commit 516d642079
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 26 deletions

View File

@ -16,10 +16,7 @@ impl UpdateState for TiltHandler {
fn update_state( fn update_state(
&mut self, &mut self,
MapContext { MapContext {
world: World { world: World { view_state, .. },
view_state: _view_state,
..
},
.. ..
}: &mut MapContext, }: &mut MapContext,
dt: Duration, dt: Duration,
@ -27,7 +24,7 @@ impl UpdateState for TiltHandler {
let dt = dt.as_secs_f64() * (1.0 / self.speed); let dt = dt.as_secs_f64() * (1.0 / self.speed);
let delta = self.delta_pitch * dt; let delta = self.delta_pitch * dt;
_view_state.camera_mut().pitch_self(delta); view_state.camera_mut().tilt(delta);
self.delta_pitch -= delta; self.delta_pitch -= delta;
} }
} }

View File

@ -66,6 +66,9 @@ impl ModelViewProjection {
} }
} }
const MIN_PITCH: Rad<f64> = Rad(-0.5);
const MAX_PITCH: Rad<f64> = Rad(0.5);
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Camera { pub struct Camera {
position: Point3<f64>, // The z axis never changes, the zoom is used instead position: Point3<f64>, // The z axis never changes, the zoom is used instead
@ -87,7 +90,7 @@ impl SignificantlyDifferent for Camera {
} }
impl Camera { impl Camera {
pub fn new<V: Into<Point3<f64>>, Y: Into<cgmath::Rad<f64>>, P: Into<cgmath::Rad<f64>>>( pub fn new<V: Into<Point3<f64>>, Y: Into<Rad<f64>>, P: Into<Rad<f64>>>(
position: V, position: V,
yaw: Y, yaw: Y,
pitch: P, pitch: P,
@ -103,14 +106,6 @@ impl Camera {
} }
} }
pub fn move_to(&mut self, point: Point3<f64>) {
self.position = point;
}
pub fn move_relative(&mut self, delta: Vector3<f64>) {
self.position += delta;
}
pub fn resize(&mut self, width: u32, height: u32) { pub fn resize(&mut self, width: u32, height: u32) {
self.width = width as f64; self.width = width as f64;
self.height = height as f64; self.height = height as f64;
@ -364,7 +359,7 @@ impl Camera {
self.yaw self.yaw
} }
pub fn yaw_self<P: Into<Rad<f64>>>(&mut self, delta: P) { pub fn rotate<P: Into<Rad<f64>>>(&mut self, delta: P) {
self.yaw += delta.into(); self.yaw += delta.into();
} }
@ -372,13 +367,33 @@ impl Camera {
self.pitch self.pitch
} }
pub fn pitch_self<P: Into<Rad<f64>>>(&mut self, delta: P) { pub fn tilt<P: Into<Rad<f64>>>(&mut self, delta: P) {
self.pitch += delta.into(); let new_pitch = self.pitch + delta.into();
if new_pitch <= MAX_PITCH && new_pitch >= MIN_PITCH {
self.pitch = new_pitch;
}
}
pub fn move_relative(&mut self, delta: Vector3<f64>) {
self.position += delta;
}
pub fn move_to(&mut self, new_position: Point3<f64>) {
self.position = new_position;
}
pub fn position_vector(&self) -> Vector3<f64> {
self.position.to_vec()
}
pub fn homogenous_position(&self) -> Vector4<f64> {
self.position.to_homogeneous()
} }
} }
pub struct Perspective { pub struct Perspective {
fovy: cgmath::Rad<f64>, fovy: Rad<f64>,
znear: f64, znear: f64,
zfar: f64, zfar: f64,
@ -386,13 +401,7 @@ pub struct Perspective {
} }
impl Perspective { impl Perspective {
pub fn new<F: Into<cgmath::Rad<f64>>>( pub fn new<F: Into<Rad<f64>>>(width: u32, height: u32, fovy: F, znear: f64, zfar: f64) -> Self {
width: u32,
height: u32,
fovy: F,
znear: f64,
zfar: f64,
) -> Self {
let rad = fovy.into(); let rad = fovy.into();
Self { Self {
current_projection: Self::calc_matrix(width as f64 / height as f64, rad, znear, zfar), current_projection: Self::calc_matrix(width as f64 / height as f64, rad, znear, zfar),
@ -411,7 +420,7 @@ impl Perspective {
); );
} }
fn calc_matrix(aspect: f64, fovy: cgmath::Rad<f64>, znear: f64, zfar: f64) -> Matrix4<f64> { fn calc_matrix(aspect: f64, fovy: Rad<f64>, znear: f64, zfar: f64) -> Matrix4<f64> {
OPENGL_TO_WGPU_MATRIX * cgmath::perspective(fovy, aspect, znear, zfar) OPENGL_TO_WGPU_MATRIX * cgmath::perspective(fovy, aspect, znear, zfar)
} }
} }