move quat rotation to fn

This commit is contained in:
Joe Ardent 2023-02-21 14:04:24 -08:00
parent e9fed1940e
commit 8cd901dadc
1 changed files with 13 additions and 6 deletions

View File

@ -22,6 +22,17 @@ fn yaw_to_angle(yaw: f32) -> f32 {
yaw.powi(3) * FRAC_PI_4 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. /// The gravity vector points from the cyberbike to the center of the planet.
pub(super) fn gravity( pub(super) fn gravity(
mut query: Query<(&Transform, &mut ExternalForce), With<CyberBikeBody>>, mut query: Query<(&Transform, &mut ExternalForce), With<CyberBikeBody>>,
@ -68,13 +79,9 @@ pub(super) fn falling_cat(
) { ) {
let (xform, mut forces, mut control_vars) = bike_query.single_mut(); let (xform, mut forces, mut control_vars) = bike_query.single_mut();
let world_up = xform.translation.normalize(); 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 = Quat::from_axis_angle(xform.back(), lean.lean);
let rot_qup = rot.inverse() * qup * rot; let target_up = rotate_point(&world_up, &rot).normalize();
let target_up = -Vec3::from_array([rot_qup.x, rot_qup.y, rot_qup.z]).normalize();
let bike_right = xform.right(); let bike_right = xform.right();
let world_right = xform.forward().cross(world_up).normalize(); let world_right = xform.forward().cross(world_up).normalize();