QuickStart Engine Fixed Camera Update Method (from v0.19)
From XNAWiki
This is just the camera's update function. Just about any camera system could implement this code fairly easily, it only requires a position, forward vector, forward looking distance, and then the rotation of the object you want the camera fixed to.
To see the entire camera system, the base camera class, and all camera code can be seen within the code browsing at the QuickStart SourceForge site.
// Update the camera. Update the view matrix and frustum if needed.
//Take into account any movement of the camera.
public void Update(GameTime gameTime)
{
// Position camera should look at with respect to its position offset.
Vector3 forwardDistance = Vector3.Transform(this.viewOffset, this.parentEntity.Rotation);
// Position camera in space
this.Position = this.positionOffset;
// Keeps camera behind its target
this.Position = Vector3.Transform(this.Position, this.parentEntity.Rotation);
// Keeps camera "attached" to its target
this.Position = Vector3.Transform(
this.Position,
Matrix.CreateTranslation(this.parentEntity.Position + forwardDistance));
// Updates the direction the camera is facing
this.Forward = Vector3.Normalize(this.parentEntity.Position + forwardDistance - this.Position);
// Update the camera's view matrix
this.viewMatrix = Matrix.CreateLookAt(
this.Position,
this.parentEntity.Position + forwardDistance,
Vector3.Up);
// Any changes in the camera's position or rotation result in an update of the camera's view frustum
UpdateFrustum();
}