XNA Lesson 6 -- Difficulty: Easy-Moderate |
September 12th, 2007 -- Setting up a simple fixed 3D camera |
This example shows how to setup a simple fixed camera. A fixed camera usually means that it is attached to something and doesn't move on its own. In this case, we create a camera that follows behind and slightly above the player. The fixed camera is derived from a camera class. This makes it easy to setup different types of cameras.
First, here is our abstract Camera class. The abstract keywords prevents one from being able to create a base class Camera object. Instead we will later create derived cameras based off of our camera class. This way any type of camera we want to create is derived from camera, and those will be the only ones we'd want to be able to instanciate.
public abstract class Camera
{
#region Fields
protected Vector3 Position = new Vector3(0f, 0f, 20f); // Default starting position of a camera
public Vector3 ForwardVector = Vector3.UnitY; // Default 'facing' direction of camera
protected Vector3 FlatFront = Vector3.Zero;
protected Vector3 TiltedFront = Vector3.Zero;
protected Vector3 RightVector = Vector3.UnitX;
protected Vector3 UpVector = Vector3.UnitZ;
protected Matrix PitchMatrix = Matrix.Identity;
protected Matrix TurnMatrix = Matrix.Identity;
protected float Pitch = 0.0f; // This will change later with input
protected float Turn = 0.0f; // This will change later with input
public Matrix viewMatrix
{
get { return ViewMatrix; }
}
protected Matrix ViewMatrix;
#endregion
#region Initialization
// In C#, all base classes to a derived class must
// have a default constructor if our base classes
// are not calling the base class in their own
// constructor.
public Camera()
{
}
#endregion
#region Methods
// Marking a function as abstract tells C# that every derived camera type
// that we create must have its own Update function implemented. You can only
// use abstract functions with abstract classes.
public abstract void Update();
public Matrix GetView()
{
return ViewMatrix;
}
#endregion
} |
Now we create our FixedCamera class, derived from the Camera class we just made.
public class FixedCamera : Camera
{
#region Properties
// No specific properties in this class
#endregion
#region Fields
BaseEntity CameraTarget; // For this sample all that is important to know about an
// entity is that is has a position and rotation.
Vector3 PositionOffset = Vector3.Zero;
Vector3 ForwardOffset = Vector3.Zero;
float ShakeValue = 0f;
#endregion
#region Initialization
public FixedCamera( BaseEntity cameraTarget )
{
CameraTarget = cameraTarget;
PositionOffset = new Vector3(0, -10.0f, 2.5f);
}
#endregion
#region Methods
public override void Update()
{
Vector3 ForwardDistance = Vector3.Zero;
// Position camera in space
Position = PositionOffset;
// Position camera should look at
ForwardDistance = new Vector3(ShakeValue + ForwardOffset.X, ForwardOffset.Y, ForwardOffset.Z);
ForwardDistance = Vector3.Transform(ForwardDistance, CameraTarget.RotationMatrix);
// Keeps camera behind its target
Position = Vector3.Transform(Position, CameraTarget.RotationMatrix);
// Keeps camera "attached" to its target
Position = Vector3.Transform(Position, Matrix.CreateTranslation(CameraTarget.position + ForwardDistance));
ViewMatrix = Matrix.CreateLookAt(Position, CameraTarget.position + ForwardDistance, UpVector);
}
#endregion
} |
This are parts of the main game class....
BaseEntity Player1;
FixedCamera Camera;
Matrix ViewMatrix;
protected override void Initialize()
{
base.Initialize();
Camera = new FixedCamera(Player1);
}
protected override void Update(GameTime gameTime)
{
Camera.Update();
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
// Clears the entire window/screen to a specific color
Device.Clear(Color.Black);
// Get the ViewMatrix from this scene's camera
ViewMatrix = Camera.GetView();
// Drawing code here....
}
|
This sample isn't a complete code sample, it gives you a general idea on creating a camera. For a full sample, please download the 3D game template (look for the 3D Game Template sample), which contains a full working camera code, complete with input. Note: The camera in is currently a free roaming camera. However, the fixed camera class in this sample can simply be added as a new class to the game template; Say it with me, modular code is fun!