Skip to content

Commit

Permalink
Return the target Quat when the angle is small (#591)
Browse files Browse the repository at this point in the history
Returning self is misleading as it skips the rotation completely
  • Loading branch information
t-yrka authored Dec 17, 2024
1 parent 80cd0e5 commit e446a0a
Show file tree
Hide file tree
Showing 8 changed files with 12 additions and 7 deletions.
2 changes: 1 addition & 1 deletion codegen/templates/quat.rs.tera
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,7 @@ impl {{ self_t }} {
glam_assert!(self.is_normalized() && rhs.is_normalized());
let angle = self.angle_between(rhs);
if angle <= 1e-4 {
return *self;
return rhs;
}
let s = (max_angle / angle).clamp(-1.0, 1.0);
self.slerp(rhs, s)
Expand Down
2 changes: 1 addition & 1 deletion src/f32/coresimd/quat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ impl Quat {
glam_assert!(self.is_normalized() && rhs.is_normalized());
let angle = self.angle_between(rhs);
if angle <= 1e-4 {
return *self;
return rhs;
}
let s = (max_angle / angle).clamp(-1.0, 1.0);
self.slerp(rhs, s)
Expand Down
2 changes: 1 addition & 1 deletion src/f32/neon/quat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ impl Quat {
glam_assert!(self.is_normalized() && rhs.is_normalized());
let angle = self.angle_between(rhs);
if angle <= 1e-4 {
return *self;
return rhs;
}
let s = (max_angle / angle).clamp(-1.0, 1.0);
self.slerp(rhs, s)
Expand Down
2 changes: 1 addition & 1 deletion src/f32/scalar/quat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ impl Quat {
glam_assert!(self.is_normalized() && rhs.is_normalized());
let angle = self.angle_between(rhs);
if angle <= 1e-4 {
return *self;
return rhs;
}
let s = (max_angle / angle).clamp(-1.0, 1.0);
self.slerp(rhs, s)
Expand Down
2 changes: 1 addition & 1 deletion src/f32/sse2/quat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@ impl Quat {
glam_assert!(self.is_normalized() && rhs.is_normalized());
let angle = self.angle_between(rhs);
if angle <= 1e-4 {
return *self;
return rhs;
}
let s = (max_angle / angle).clamp(-1.0, 1.0);
self.slerp(rhs, s)
Expand Down
2 changes: 1 addition & 1 deletion src/f32/wasm32/quat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ impl Quat {
glam_assert!(self.is_normalized() && rhs.is_normalized());
let angle = self.angle_between(rhs);
if angle <= 1e-4 {
return *self;
return rhs;
}
let s = (max_angle / angle).clamp(-1.0, 1.0);
self.slerp(rhs, s)
Expand Down
2 changes: 1 addition & 1 deletion src/f64/dquat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ impl DQuat {
glam_assert!(self.is_normalized() && rhs.is_normalized());
let angle = self.angle_between(rhs);
if angle <= 1e-4 {
return *self;
return rhs;
}
let s = (max_angle / angle).clamp(-1.0, 1.0);
self.slerp(rhs, s)
Expand Down
5 changes: 5 additions & 0 deletions tests/quat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,11 @@ macro_rules! impl_quat_tests {
);
assert_approx_eq!(q2, q0.rotate_towards(q1, -FRAC_PI_2), eps);
assert_approx_eq!(q2, q0.rotate_towards(q1, -FRAC_PI_2 * 1.5), eps);

// Small angles
let q0 = $quat::from_euler(EulerRot::YXZ, 0.0, 0.0, 0.0);
let q1 = $quat::from_euler(EulerRot::YXZ, 1e-4, 0.0, 0.0);
assert_eq!(q1, q0.rotate_towards(q1, FRAC_PI_2))
});

glam_test!(test_fmt, {
Expand Down

0 comments on commit e446a0a

Please sign in to comment.