From 5159b8ef911ea6443a35d6e83e53658f507bb478 Mon Sep 17 00:00:00 2001 From: gered Date: Sun, 8 Sep 2024 22:56:42 -0400 Subject: [PATCH] add Matrix4x4 method for creating a rotation about an arbitrary axis --- ggdt/src/math/matrix4x4.rs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/ggdt/src/math/matrix4x4.rs b/ggdt/src/math/matrix4x4.rs index 80c8507..8239a22 100644 --- a/ggdt/src/math/matrix4x4.rs +++ b/ggdt/src/math/matrix4x4.rs @@ -78,6 +78,37 @@ impl Matrix4x4 { rotate_z * rotate_y * rotate_x } + // Creates a new rotation matrix about the specified axis. + // + // # Arguments + // + // * `radians`: angle to rotate by (in radians) + // * `x`: the x component of the axis vector to rotate around + // * `y`: the y component of the axis vector to rotate around + // * `z`: the z component of the axis vector to rotate around + pub fn new_rotation(radians: f32, x: f32, y: f32, z: f32) -> Matrix4x4 { + let (s, c) = radians.sin_cos(); + + Matrix4x4::new( + (x * x) * (1.0 - c) + c, + (x * y) * (1.0 - c) - (z * s), + (x * z) * (1.0 - c) + (y * s), + 0.0, + (y * x) * (1.0 - c) + (z * s), + (y * y) * (1.0 - c) + c, + (y * z) * (1.0 - c) - (x * s), + 0.0, + (z * x) * (1.0 - c) - (y * s), + (z * y) * (1.0 - c) + (x * s), + (z * z) * (1.0 - c) + c, + 0.0, + 0.0, + 0.0, + 0.0, + 1.0, + ) + } + /// Creates a new rotation matrix for rotation around the x axis. /// /// # Arguments