Advertisement Jump to content
Sign in to follow this  
Alundra

Quaternion look-at

This topic is 1780 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all,

I have a look-at function like that :

// Set the look at matrix.
CMatrix4 LookAtMatrix;
const CVector3 From = CVector3( 0.0f, 0.0f, 0.0f );
const CVector3 To = ( TargetPoint - m_TranslationVector ).Normalized();
LookAtMatrix.LookAt( From, To, CVector3( 0.0f, 1.0f, 0.0f ) );

// Update the orientation quaternion.
m_OrientationQuaternion.FromMatrix( LookAtMatrix.Transposed() );

I would change to not use FromMatrix to have better perf, this version doesn't work sometimes :

const CVector3 Direction = ( TargetPoint - m_TranslationVector ).Normalized();
const CVector3 RotationAxis = VectorCross( m_ForwardVector, Direction ).Normalized();
const float RotationAngle = CMath::ACos( VectorDot( m_ForwardVector, Direction ) );
m_OrientationQuaternion *= CQuaternion( RotationAxis, RotationAngle );

Why does that give bad result sometimes ?

Thanks

 

Share this post


Link to post
Share on other sites
Advertisement

I can imagine that this version might be not be working when Forward and Direction are anti-parallel. 

 

I am curious if your version will be actually more efficient. Your version calls a ACos and the quaternion constructor very likely calls a Sin() and Cos(). 

 

In the original version you can build the quaternion from the look-at matrix and then conjugate instead of passing the transpose.

 

Still, I would guess non of this matters. How often are you calling this function per frame that you identified it to be a good time investment to optimize it?

Edited by Dirk Gregorius

Share this post


Link to post
Share on other sites

You right up-vector should be used instead of hard coded up-vector to avoid problem.

Actually this function is called once per frame but I would like to know how make a good look at with quaternion.

LookAt of a quaternion is a good use in a look-at node on an animation system, it's nice to understand how that works good.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!