I got this code from the internet
// O is your object's position
// P is the position of the object to face
// U is the nominal "up" vector (typically Vector3.Y)
// Note: this does not work when O is straight below or straight above P
Matrix RotateToFace(Vector3 O, Vector3 P, Vector3 U)
{
Vector3 D = (O - P);
Vector3 Right = Vector3.Cross(U, D);
Vector3.Normalize(ref Right, out Right);
Vector3 Backwards = Vector3.Cross(Right, U);
Vector3.Normalize(ref Backwards, out Backwards);
Vector3 Up = Vector3.Cross(Backwards, Right);
Matrix rot = new Matrix(Right.X, Right.Y, Right.Z, 0, Up.X, Up.Y, Up.Z, 0, Backwards.X, Backwards.Y, Backwards.Z, 0, 0, 0, 0, 1);
}
It does not work when O is straight below or straight above P. I must wondering is there any other better code around. I hear that
quaternion4f can do the job in a much simpler way and does not suffer from any short falls. But I'm lost in quaternion.
The example I follows from xith-tk, when I set the lookup(), the translation is being altered. I need the translation to be the same.
I hope this explains my problem in clear detail.
God Bless
Eng Huat