I'm trying to implement a camera that tracks a 3D model in my game. I've taken a look at this MSDN article on creating a 3rd person camera, as well as experimenting on my own; but I don't get the correct result.
My camera follows behind my model correctly, until I rotate the model about the y-axis. In this case, the camera seems to fly off to one side or the other.
What I want, is when the model is rotated to the left the camera will remain behind it; so as too mimic the view that can been seen in the left image (minus the fact that the rest of the world will display the rotation). I have tried looking around for tracking camera implementations, but to no avail.
Here is the code I use to calculate the position and look-at vectors of the camera:
// Calculate the offset vector Vector3 offset = new Vector3(0.0f, this.DistanceAboveTarget, -this.DistanceBehindTarget); // Transform the offset by the target's rotation float targetRotation = mTarget.Rotation.Y; Matrix rotationMatrix = Matrix.CreateRotationY(targetRotation); Vector3 transformedOffset = Vector3.Transform(offset, rotationMatrix); // Calculate the new position and look at vectors Vector3 targetPosition = mTarget.Position; mLookAt = targetPosition; mPosition = targetPosition + transformedOffset;
All other matrices are calculate elsewhere in my code and are correct, so far as I understand. Full code samples can be found in the below links: