From 908ef391e407b59a0526ca1b2aa149eb6e4c498c Mon Sep 17 00:00:00 2001 From: HampusM Date: Fri, 21 Aug 2026 22:38:45 +0200 Subject: feat(engine): add fns for conversion between vec3 & euler angles --- engine/src/data_types/vector.rs | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) (limited to 'engine/src/data_types') 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 (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) -> 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 + { + Angles { + yaw: self.z.atan2(self.x).to_degrees(), + pitch: self.y.asin().to_degrees(), + roll: 0.0, } } } @@ -290,3 +300,12 @@ impl From> for [Value; 4] [vec.x, vec.y, vec.z, vec.w] } } + +#[derive(Debug, Clone, Copy, Reflection)] +#[reflection(impl_with_generics(, ))] +pub struct Angles +{ + pub yaw: Value, + pub pitch: Value, + pub roll: Value, +} -- cgit v1.2.3-18-g5258