From 8cd901dadc051d8f9eb3c0a90e4189418f37678b Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Tue, 21 Feb 2023 14:04:24 -0800 Subject: [PATCH] move quat rotation to fn --- src/action/systems.rs | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/action/systems.rs b/src/action/systems.rs index 086bdb0..e3b0b0b 100644 --- a/src/action/systems.rs +++ b/src/action/systems.rs @@ -22,6 +22,17 @@ fn yaw_to_angle(yaw: f32) -> f32 { yaw.powi(3) * FRAC_PI_4 } +fn rotate_point(pt: &Vec3, rot: &Quat) -> Vec3 { + // thanks to https://danceswithcode.net/engineeringnotes/quaternions/quaternions.html + let [x, y, z] = pt.to_array(); + let qpt = Quat::from_xyzw(x, y, z, 0.0); + // p' = rot^-1 * qpt * rot + let rot_qpt = rot.inverse() * qpt * *rot; + + // why does this need to be inverted??? + -Vec3::from_array([rot_qpt.x, rot_qpt.y, rot_qpt.z]) +} + /// The gravity vector points from the cyberbike to the center of the planet. pub(super) fn gravity( mut query: Query<(&Transform, &mut ExternalForce), With>, @@ -68,13 +79,9 @@ pub(super) fn falling_cat( ) { let (xform, mut forces, mut control_vars) = bike_query.single_mut(); let world_up = xform.translation.normalize(); - let [x, y, z] = world_up.to_array(); - // turn our "world up" point into a Quat - let qup = Quat::from_xyzw(x, y, z, 0.0); - // p' = rot^-1 * qup * rot let rot = Quat::from_axis_angle(xform.back(), lean.lean); - let rot_qup = rot.inverse() * qup * rot; - let target_up = -Vec3::from_array([rot_qup.x, rot_qup.y, rot_qup.z]).normalize(); + let target_up = rotate_point(&world_up, &rot).normalize(); + let bike_right = xform.right(); let world_right = xform.forward().cross(world_up).normalize();