Quaternion Look-At (From Direction)

Started by
-1 comments, last by Alundra 9 years, 8 months ago

Hi all,

I always used LookAt matrix transposed to create the quaternion but I would change to use quaternion directly to be more efficient.

Here the code I use :


CQuaternion QuaternionFromDirection( const CVector3& Direction )
{
  const float Dot = VectorDot( CVector3( 0.0f, 0.0f, 1.0f ), Direction );
  if( CMath::Abs( Dot + 1.0f ) < CMath::EPSILON )
    return CQuaternion( CVector3( 0.0f, 1.0f, 0.0f ), CMath::PI );
  else if( CMath::Abs( Dot - 1.0f ) < CMath::EPSILON )
    return CQuaternion( 0.0f, 0.0f, 0.0f, 1.0f );
  const CVector3 Axis = VectorCross( CVector3( 0.0f, 0.0f, 1.0f ), Direction ).Normalized();
  const float Angle = CMath::ACos( Dot );
  return CQuaternion( Axis, Angle );
}

Is it the correct way to achieve that ? A up vector is needed to be more safe ?

If a up vector is needed, what is the best way to achieve that ?

Thanks for the help

This topic is closed to new replies.

Advertisement