Camera not staying behind model while moving in circle
- by ChocoMan
I have a camera behind a model (3rd Person) and I'm having problems KEEPING it behind the model. When I first start my game, you see the back of the model. If the model moves forward, backward or strafe left or right, the camera moves along accordingly. When the model rotates (stationary), the camera rotates accordingly with the model still pointing at the model's back. So far, so good.
The problem comes when the player is BOTH moving and rotating at the same time. Take for example a model moving in a circular pattern like running around a track. As the model moves in this motion, the model rotates slightly more with each complete rotation. Eventually, instead of looking at the model's back, eventually you will see the model in a profile view and before you know it, the model's front is facing the camera. And when you stop moving the model, the model stays in that position.
So, as long as my model is stationary and rotating in one place, the camera rotates correctly. But as soon as there is any sort movement while rotating, the model is offset by a mysterious increasing amount. How can I keep the camera maintaining the same view no matter how I move AND rotate at the same time?
// Rotates model and pitches camera on its own axis
public void modelRotMovement(GamePadState pController)
{
/* For rotating the model left or right.
* Camera maintains distance from model
* throughout rotation and if model moves
* to a new position.
*/
Yaw = pController.ThumbSticks.Right.X * MathHelper.ToRadians(speedAngleMAX);
AddRotation = Quaternion.CreateFromAxisAngle(Vector3.Up, yaw);
//AddRotation = Quaternion.CreateFromYawPitchRoll(Yaw, 0, 0);
ModelLoad.MRotation *= AddRotation;
MOrientation = Matrix.CreateFromQuaternion(ModelLoad.MRotation);
Pitch = pController.ThumbSticks.Right.Y * MathHelper.ToRadians(speedAngleMAX);
AddPitch = Quaternion.CreateFromAxisAngle(Vector3.Up, pitch);
ModelLoad.CRotation *= AddPitch;
COrientation = Matrix.CreateFromQuaternion(ModelLoad.CRotation);
}
// Orbit (yaw) Camera around model
public void cameraYaw(float yaw)
{
Vector3 yawAngle = ModelLoad.CameraPos - ModelLoad.camTarget;
Vector3 axisYaw = Vector3.Up;
ModelLoad.CameraPos = Vector3.Transform(yawAngle,
Matrix.CreateFromAxisAngle(axisYaw, yaw)) + ModelLoad.camTarget;
}