summaryrefslogtreecommitdiff
path: root/engine/src/data_types
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/data_types
parentffbbe51561393e559b6e70d9e9b39bc025f77d1c (diff)
feat(engine): add fns for conversion between vec3 & euler angles
Diffstat (limited to 'engine/src/data_types')
-rw-r--r--engine/src/data_types/vector.rs29
1 files changed, 24 insertions, 5 deletions
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,
+}