FlatRedBall.Camera

Introduction

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:

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:

Replace your Initialize method with the following:

Replace your Update method with the following:

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

SplitScreen.png

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:

For more information, see the DestinationRectangle entry.

Related Tutorials

Extra Information