summaryrefslogtreecommitdiff
path: root/engine/src
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2026-08-21 22:38:45 +0200
committerHampusM <hampus@hampusmat.com>2026-08-21 22:38:45 +0200
commit908ef391e407b59a0526ca1b2aa149eb6e4c498c (patch)
tree9b507955f5b360f312e056a0dbe01a423cfbe400 /engine/src
parentffbbe51561393e559b6e70d9e9b39bc025f77d1c (diff)
feat(engine): add fns for conversion between vec3 & euler angles
Diffstat (limited to 'engine/src')
-rw-r--r--engine/src/camera.rs2
-rw-r--r--engine/src/camera/fly.rs25
-rw-r--r--engine/src/data_types/vector.rs29
3 files changed, 32 insertions, 24 deletions
diff --git a/engine/src/camera.rs b/engine/src/camera.rs
index 4a7768b..51cd54c 100644
--- a/engine/src/camera.rs
+++ b/engine/src/camera.rs
@@ -31,7 +31,7 @@ impl Default for Camera
fn default() -> Self
{
Self {
- target: Vec3::FRONT,
+ target: Vec3 { x: 0.0, y: 0.0, z: 0.0 },
global_up: Vec3::UP,
projection: Projection::Perspective(Perspective::default()),
}
diff --git a/engine/src/camera/fly.rs b/engine/src/camera/fly.rs
index 0651e58..5f5ab4b 100644
--- a/engine/src/camera/fly.rs
+++ b/engine/src/camera/fly.rs
@@ -14,7 +14,7 @@ use crate::input::keyboard::{Key, Keyboard};
use crate::input::mouse::Mouse;
use crate::reflection::Reflection;
use crate::transform::Transform;
-use crate::vector::{Vec2, Vec3};
+use crate::vector::{Angles, Vec2, Vec3};
builder! {
/// A fly camera.
@@ -22,8 +22,7 @@ builder! {
#[derive(Debug, Component, Reflection)]
#[non_exhaustive]
pub struct Fly {
- pub current_pitch: f64,
- pub current_yaw: f64,
+ pub angles: Angles<f32>,
pub speed: f32,
}
}
@@ -50,8 +49,7 @@ impl Default for Builder
fn default() -> Self
{
Self {
- current_yaw: -90.0,
- current_pitch: 0.0,
+ angles: Vec3::BACK.into_deg_angles(),
speed: 3.0,
}
}
@@ -112,22 +110,13 @@ fn update(
let y_offset = (-mouse.curr_tick_position_delta.y)
* f64::from(options.mouse_sensitivity);
- fly_camera.current_yaw += x_offset;
- fly_camera.current_pitch += y_offset;
+ fly_camera.angles.yaw += x_offset as f32;
+ fly_camera.angles.pitch += y_offset as f32;
- fly_camera.current_pitch = fly_camera.current_pitch.clamp(-89.0, 89.0);
+ fly_camera.angles.pitch = fly_camera.angles.pitch.clamp(-89.0, 89.0);
}
- // TODO: This casting to a f32 from a f64 is horrible. fix it
- #[allow(clippy::cast_possible_truncation)]
- let direction = Vec3 {
- x: (fly_camera.current_yaw.to_radians().cos()
- * fly_camera.current_pitch.to_radians().cos()) as f32,
- y: fly_camera.current_pitch.to_radians().sin() as f32,
- z: (fly_camera.current_yaw.to_radians().sin()
- * fly_camera.current_pitch.to_radians().cos()) as f32,
- }
- .normalize();
+ let direction = Vec3::direction_from_deg_angles(fly_camera.angles);
let cam_right = direction.cross(&Vec3::UP).normalize();
diff --git a/engine/src/data_types/vector.rs b/engine/src/data_types/vector.rs
index 8644dd2..dfe9a99 100644
--- a/engine/src/data_types/vector.rs
+++ b/engine/src/data_types/vector.rs
@@ -173,14 +173,24 @@ impl Vec3<f32>
(self.x * rhs.x) + (self.y * rhs.y) + (self.z * rhs.z)
}
- /// Returns a direction vector from the specified angle (in degrees).
+ /// Returns a normalized direction vector from the specified angle (in degrees).
#[must_use]
- pub fn direction_from_angle(pitch_degs: f32, yaw_degs: f32) -> Self
+ pub fn direction_from_deg_angles(angles: Angles<f32>) -> Self
{
Self {
- x: yaw_degs.to_radians().cos() * pitch_degs.to_radians().cos(),
- y: pitch_degs.to_radians().sin(),
- z: yaw_degs.to_radians().sin() * pitch_degs.to_radians().cos(),
+ x: angles.yaw.to_radians().cos() * angles.pitch.to_radians().cos(),
+ y: angles.pitch.to_radians().sin(),
+ z: angles.yaw.to_radians().sin() * angles.pitch.to_radians().cos(),
+ }
+ .normalize()
+ }
+
+ pub fn into_deg_angles(self) -> Angles<f32>
+ {
+ Angles {
+ yaw: self.z.atan2(self.x).to_degrees(),
+ pitch: self.y.asin().to_degrees(),
+ roll: 0.0,
}
}
}
@@ -290,3 +300,12 @@ impl<Value> From<Vec4<Value>> for [Value; 4]
[vec.x, vec.y, vec.z, vec.w]
}
}
+
+#[derive(Debug, Clone, Copy, Reflection)]
+#[reflection(impl_with_generics(<f32>, <f64>))]
+pub struct Angles<Value>
+{
+ pub yaw: Value,
+ pub pitch: Value,
+ pub roll: Value,
+}