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

[feat.request] Add RotationBetweenVectors in GLM_GTX_quaternion #22

Closed
Calvin1602 opened this issue Dec 20, 2012 · 2 comments
Closed

[feat.request] Add RotationBetweenVectors in GLM_GTX_quaternion #22

Calvin1602 opened this issue Dec 20, 2012 · 2 comments
Assignees
Milestone

Comments

@Calvin1602
Copy link

I couldn't find in glm a function to rotate a direction into another one.
Here's one :

quat RotationBetweenVectors(vec3 start, vec3 dest){
    start = normalize(start);
    dest = normalize(dest);

    float cosTheta = dot(start, dest);
    vec3 rotationAxis;

    if (cosTheta < -1 + 0.001f){
        // special case when vectors in opposite directions :
        // there is no "ideal" rotation axis
        // So guess one; any will do as long as it's perpendicular to start
        // This implementation favors a rotation around the Up axis (Y),
        // since it's often what you want to do.
        rotationAxis = cross(vec3(0.0f, 0.0f, 1.0f), start);
        if (gtx::norm::length2(rotationAxis) < 0.01 ) // bad luck, they were parallel, try again!
            rotationAxis = cross(vec3(1.0f, 0.0f, 0.0f), start);

        rotationAxis = normalize(rotationAxis);
        return gtx::quaternion::angleAxis(180.0f, rotationAxis);
    }

    // Implementation from Stan Melax's Game Programming Gems 1 article
    rotationAxis = cross(start, dest);

    float s = sqrt( (1+cosTheta)*2 );
    float invs = 1 / s;

    return quat(
        s * 0.5f, 
        rotationAxis.x * invs,
        rotationAxis.y * invs,
        rotationAxis.z * invs
    );


}

Ogre's version enables the user to choose the fallback axis instead of forcing the Y one; but when not provided, it favours the Z axis, which I find weird : https://bitbucket.org/sinbad/ogre/src/3cbd67467fab3fef44d1b32bc42ccf4fb1ccfdd0/OgreMain/include/OgreVector3.h?at=default#cl-651

@Groovounet
Copy link
Member

I am a bit surprize to see:
"return gtx::quaternion::angleAxis(180.0f, rotationAxis);"
in the proposed code.
angleAxis is supposed to take radians...

Thanks,
Christophe

@ghost ghost assigned Groovounet Feb 22, 2013
Groovounet pushed a commit that referenced this issue Feb 22, 2013
@Groovounet
Copy link
Member

This feature request has been implemented. This is just one change, by default the function assume that the inputs are already normalized.

Thanks for contributing!
Christophe

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants