Last active
November 23, 2024 03:36
-
-
Save lotus128/24f616d7dc547805c7a2b12af0a54f6e to your computer and use it in GitHub Desktop.
Bevy character controller rotate body around root
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /// translate and rotate a point around anhter | |
| pub fn rotate_point_around_pivot( | |
| point: Vec3, | |
| pivot: Vec3, | |
| rotation: Quat, | |
| ) -> Vec3 { | |
| // Translate the point to the origin | |
| let translated_point = point - pivot; | |
| // Apply the rotation | |
| let rotated_point = rotation * translated_point; | |
| // Translate back to the pivot point | |
| return rotated_point + pivot; | |
| } | |
| /// traverse the stage via rotate. | |
| pub fn traverse_via_rotate( | |
| rapier_context: &Res<RapierContext>, | |
| query_filter: QueryFilter, | |
| start_root_global_translation: Vec3, | |
| start_root_global_rotation: Quat, | |
| body_local_translation: Vec3, | |
| body_shape: SharedShape, | |
| rotation: Quat, | |
| ) -> (Vec3, Quat) { | |
| let start_body_global_translation = start_root_global_translation | |
| + start_root_global_rotation * body_local_translation; | |
| let end_body_global_rotation = rotation * start_root_global_rotation; | |
| // determine the global translation of the root after rotate | |
| let post_rotate_root_global_translation = rotate_point_around_pivot( | |
| start_root_global_translation, | |
| start_body_global_translation, | |
| rotation, | |
| ); | |
| // TODO raycast character back towards | |
| return ( | |
| post_rotate_root_global_translation, | |
| end_body_global_rotation, | |
| ); | |
| } | |
| #[cfg(test)] | |
| mod tests { | |
| #[cfg(test)] | |
| mod rotate_with_body { | |
| use std::f32::consts::PI; | |
| use bevy::math::{Quat, Vec3}; | |
| use crate::vec3::rotate_point_around_pivot; | |
| #[test] | |
| fn it_works() { | |
| let result_translation = rotate_point_around_pivot( | |
| Vec3 { | |
| x: 0.0, | |
| y: 0.0, | |
| z: 0.0, | |
| }, | |
| Vec3 { | |
| x: 0.0, | |
| y: 1.0, | |
| z: 0.0, | |
| }, | |
| Quat::from_axis_angle(Vec3::X, PI / 2.0), | |
| ); | |
| assert_eq!( | |
| Vec3 { | |
| x: 0.0, | |
| y: 1.0, | |
| z: -1.0 | |
| }, | |
| result_translation, | |
| ); | |
| } | |
| #[test] | |
| fn fdas() { | |
| let result_translation = rotate_point_around_pivot( | |
| Vec3 { | |
| x: 0.0, | |
| y: 1.0, | |
| z: 0.0, | |
| }, | |
| Vec3 { | |
| x: 0.0, | |
| y: 0.0, | |
| z: 0.0, | |
| }, | |
| Quat::IDENTITY, | |
| ); | |
| assert_eq!( | |
| Vec3 { | |
| x: 0.0, | |
| y: 1.0, | |
| z: 0.0 | |
| }, | |
| result_translation, | |
| ); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment