Advertisement

Quaternion look-at

Started by March 04, 2014 11:13 PM
1 comment, last by Alundra 10 years, 11 months ago

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

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?

Advertisement

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.

This topic is closed to new replies.

Advertisement