Let's assume that lookVector
is the vector in model space that defines the view direction of the character. Usually this is one of the principal axes (e.g. the z-axis). Let's further assume that the character is positioned at characterPosition
and that the target point is target
.
The view direction towards the target in world space is
var view = target - characterPosition;
Now all we need to do is find a rotation matrix that maps lookVector
to view
. The solution is not unique, but we are looking for the solution with a minimal rotation angle.
The rotation axis can be found with the cross product, the rotation angle with the dot product:
var rotationAxis = Vector3.Cross(lookVector, view);
var rotationAngle = (float)Math.Acos(Vector3.Dot(lookVector, view) / lookVector.Length() / view.Length());
Now we can construct the rotation matrix:
var rotationMatrix = Matrix.CreateFromAxisAngle(rotationAxis, rotationAngle);