The Camera encapsulates functionality to simplify and control the display of a FlatRedBall application to the user. The camera controls view position, rotation, field of view, and viewable distance.

By default the camera in FlatRedBall XNA looks down the negative Z axis – that is negative Z points away from the camera. Increasing the Camera’s Z moves it “forward” while decreasing moves it “backwards”. Of course, if the Camera is rotated, the forward and backward directions change.

For information on screen resolution and running full screen FlatRedBall applications, see the Game class wiki entry.

Accessing the Camera

FlatRedBall projects automatically create a Camera which can be accessed by Camera.Main. The following code moves the Camera to X = 5:

Camera.Main.X = 5;

For more informaiton see the Camera.Main page.

In most cases you will never need to create your own Camera – this one is the default camera that all single-Camera games use.

Controlling the Camera

The camera inherits from the PositionedObject class so it shares the same interface for positioning, rotation, and attachment.

Camera movement is not noticeable without something visible in the scene to compare the movement against. The following code creates a strip of Sprites in Initialize and moves the Camera with the Keyboard in Update.

Add the following using statement:

using FlatRedBall.Input;

Replace your Initialize method with the following:

 protected override void Initialize()
     FlatRedBallServices.InitializeFlatRedBall(this, this.graphics);

     int numberOfSprites = 60;

     for (int i = 0; i < numberOfSprites; i++)
         Sprite sprite = SpriteManager.AddSprite("redball.bmp");
         sprite.Z = 20 - i * 2;


Replace your Update method with the following:

 protected override void Update(GameTime gameTime)

    float velocityMagnitude = 10;

    if (InputManager.Keyboard.KeyDown(Keys.Right))
       Camera.Main.XVelocity = velocityMagnitude;
    else if(InputManager.Keyboard.KeyDown(Keys.Left))
       Camera.Main.XVelocity = -velocityMagnitude;
       Camera.Main.XVelocity = 0;

    if (InputManager.Keyboard.KeyDown(Keys.Up))
       Camera.Main.YVelocity = velocityMagnitude;
    else if (InputManager.Keyboard.KeyDown(Keys.Down))
       Camera.Main.YVelocity = -velocityMagnitude;
       Camera.Main.YVelocity = 0;


2D Cameras and Pixels

For information about 2D coordinates and how to create 2D scenes, see the 2D In FlatRedBall tutorial.

To convert between world and pixel coordinates, see the World and Screen Coordinates page.

Camera Edges

For information on finding the absolute coordinates of the edges of the Camera, see the RelativeXEdgeAt article.

Multiple Cameras

FlatRedBall supports multiple cameras for different viewports (split screen). The following code creates a Sprite and views it from two different cameras.

In Initialize

 // Add a Sprite

 // We have one camera already made, so only one more is needed for split screen.
 string contentManagerName = "YourContentManager";
 Camera camera = new Camera(contentManagerName);
 // Put it as part of the SpriteManager's list

 // Now we adjust the viewports:

 // Move the camera on the right closer
 SpriteManager.Cameras[1].Z = 5;


For info on the content manager argument see the FlatRedBall Content Manager wiki entry.

Manually Setting Destination Rectangle

Every camera has a DestinationRectangle which defines where on the window it draws. When specifying the DestinationRectangle the top-left of the window is 0,0 and positive Y moves down. Assuming the window is 800 pixels wide and 600 pixels tall the following code creates a 50 pixel border around the DestinationRectangle:

// Will work with any camera but for this sample we'll use the default Camera
Camera camera = Camera.Main;
// 50 pixel border on each side decreases the dimensions by 100.
// 600 - 100 = 500
// 800 - 100 = 700
camera.DestinationRectangle = new Rectangle(50, 50, 700, 500);

For more information, see the DestinationRectangle entry.

Related Tutorials

Extra Information