Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return the target Quat when the angle is small #591

Merged
merged 1 commit into from
Dec 17, 2024

Conversation

t-yrka
Copy link
Contributor

@t-yrka t-yrka commented Dec 16, 2024

Returning source Quat is misleading as it skips the rotation completely.

See #590

Returning self is misleading as it skips the rotation completely
@bitshifter
Copy link
Owner

bitshifter commented Dec 16, 2024

Thanks, I was wondering how this issue manifests? You expect to rotate to a target but it never quite reaches it under this minimum angle?

Ignore the cargo-deny and clippy failures, they look unrelated to your change.

@t-yrka
Copy link
Contributor Author

t-yrka commented Dec 16, 2024

Thanks, I was wondering how this issue manifests? You expect to rotate to a target but it never quite reaches it under this minimum angle?

I've encountered this issue while trying to rotate a slowly moving object toward a point that is quite far away. I was drawing the object's center point projection with a given rotation and observed that it was jittering and sometimes off (from the target) by quite a significant margin.

Based on the data I've extracted at some point it can be observed/reproduced with the following code:

use glam::{Quat, Vec3};

fn main() {
    let source = Vec3::new(570.5249, -180.84932, 0.0);
    let source_rotation = Quat::from_array([0.0, 0.0, 0.6075659, 0.7947468]);
    let target = Vec3::new(-576.0742, 105.41407, 0.0);

    let vec_to_target = (target - source).normalize();
    let rotation_to_target = Quat::from_rotation_arc(Vec3::Y, vec_to_target);
    println!("Source rotation: {}", source_rotation);
    println!("Target rotation: {}", rotation_to_target);

    let result = source_rotation.rotate_towards(rotation_to_target, f32::to_radians(30.0));
    println!("Result rotation: {}", result);

    let angle = source_rotation.angle_between(rotation_to_target);
    println!("Angle: {}", angle);

    let shift_vec = result * Vec3::Y;
    let distance = target.distance(source);
    let proj_with_result = source + shift_vec * distance;
    println!("Projection with result: {}", proj_with_result);

    let shift_vec = rotation_to_target * Vec3::Y;
    let distance = target.distance(source);
    let proj_with_target = source + shift_vec * distance;
    println!("Projection with target: {}", proj_with_target);

    let proj_distance = proj_with_result.distance(proj_with_target);
    println!("Projections distance: {}", proj_distance);
}

Output:

Source rotation: [0, 0, 0.6075659, 0.7947468]
Target rotation: [0, -0, 0.61553717, 0.7881078]
Result rotation: [0, 0, 0.6075659, 0.7947468]
Angle: 0
Projection with result: [-570.7594, 129.35521, 0]
Projection with target: [-576.0742, 105.41408, 0]
Projections distance: 24.52397

@bitshifter bitshifter merged commit e446a0a into bitshifter:main Dec 17, 2024
16 of 18 checks passed
@t-yrka t-yrka deleted the fix/quat-rotation branch December 17, 2024 09:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants