lookAt method
Creates a rotation matrix that orients an object to face towards a specified target direction.
Implementation
Matrix3 lookAt(Vector3 localForward, Vector3 targetDirection, Vector3 localUp ) {
localRight.crossVectors( localUp, localForward ).normalize();
// orthonormal linear basis A { localRight, localUp, localForward } for the object local space
worldRight.crossVectors( worldUp, targetDirection ).normalize();
if ( worldRight.squaredLength == 0 ) {
// handle case when it's not possible to build a basis from targetDirection and worldUp
// slightly shift targetDirection in order to avoid collinearity
temp.copy( targetDirection ).addScalar( MathUtils.epsilon );
worldRight.crossVectors( worldUp, temp ).normalize();
}
perpWorldUp.crossVectors( targetDirection, worldRight ).normalize();
// orthonormal linear basis B { worldRight, perpWorldUp, targetDirection } for the desired target orientation
_m1.makeBasis( worldRight, perpWorldUp, targetDirection );
_m2.makeBasis( localRight, localUp, localForward );
// finalruct a matrix that maps basis A to B
multiplyMatrices( _m1, _m2.transpose() );
return this;
}