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, and background color. If the Camera is 3D, it can also control field of view, and viewable distance.
By default the camera in FlatRedBall looks down the negative Z axis. In other words negative Z points away from the camera. Increasing the Camera's Z moves it backwards while decreasing moves it forward. FlatRedBall uses a right handed coordinate system.
Of course, if the Camera is rotated, the forward and backward directions change. Most games can change camera properties through the FlatRedBall Editor's Camera Window. Alternatively, the Camera can be modified in code.
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 information 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 can be added to a Screen such as an empty Screen created in the FlatRedBall Editor: Add the following using statements:
Replace CustomInitialize and CustomActivity with the following code:
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
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 page.
Last updated