Skip to content

Quaternion Slerp honors closest distance.#90

Merged
dmitshur merged 2 commits intogo-gl:masterfrom
nitrix:fix/quat-slerp-closest
Apr 7, 2023
Merged

Quaternion Slerp honors closest distance.#90
dmitshur merged 2 commits intogo-gl:masterfrom
nitrix:fix/quat-slerp-closest

Conversation

@nitrix
Copy link
Contributor

@nitrix nitrix commented Dec 26, 2022

Hello,

This is a fix to the quaternion spherical linear interpolation to make it choose the shortest linear path which is the intended behavior in graphical animations.

//make sure we take the shortest path in case dot Product is negative
if(dotProduct < 0.0)
    nextQuat = -nextQuat
    dotProduct = -dotProduct
if (cosTheta < 0.0f) {
    glm_vec4_negate(q1);
    cosTheta = -cosTheta;
}

You'll find that my changes are in the same spirit as them. Added a test too.

// Make sure we take the shortest path in case dot product is negative.
if dot < 0.0 {
    q2 = q2.Scale(-1)
    dot = -dot
}

This is a very well known problem. Wikipedia documents it here in detail:
https://en.wikipedia.org/wiki/Slerp#Quaternion_Slerp

However, because the covering is double (q and −q map to the same rotation), the rotation path may turn either the "short way" (less than 180°) or the "long way" (more than 180°). Long paths can be prevented by negating one end if the dot product, cos Ω, is negative, thus ensuring that −90° ≤ Ω ≤ 90°.

I ran into this corner case using mathgl, my fork will stay up until the changes can get upstream:
https://github.com/nitrix/mathgl/tree/fix/quat-slerp-closest

Cheers,
Alex.

Copy link
Member

@dmitshur dmitshur left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks.

This change makes sense and looks right to me. I plan to merge later this week, to give a chance for additional comments.

Gofmt gets rid of it. (But avoid making further changes that
gofmt makes by now, those are best done as a separate change.)
@dmitshur dmitshur merged commit e426c08 into go-gl:master Apr 7, 2023
@nitrix nitrix deleted the fix/quat-slerp-closest branch April 7, 2023 19:26
liuhaozhan added a commit to liuhaozhan/mathgl that referenced this pull request Jan 31, 2024
Fix the quaternion spherical linear interpolation
to make it choose the shortest linear path, which
is the intended behavior in graphical animations.

GitHub-Pull-Request: go-gl/mathgl#90
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