Class Camera
Represents a simple, orthographic 2-dimensional camera that can be moved, scaled and that supports automatic viewport sizing. To draw with the camera's positioning and scaling applied, use ViewMatrix.
public class Camera
- Inheritance
-
Camera
- Inherited Members
Constructors
Camera(GraphicsDevice, bool)
Creates a new camera.
public Camera(GraphicsDevice graphicsDevice, bool roundPosition = true)
Parameters
graphicsDeviceGraphicsDeviceThe game's graphics device
roundPositionboolWhether the camera's Position should be rounded to full integers when calculating the ViewMatrix
Fields
AutoScaleReferenceSize
public Point AutoScaleReferenceSize
Field Value
- Point
AutoScaleWithScreen
If this is true, the camera will automatically adapt to changed screen sizes. You can use AutoScaleReferenceSize to determine the initial screen size that this camera should base its calculations on.
public bool AutoScaleWithScreen
Field Value
Epsilon
This field holds an epsilon value used in some camera calculations to mitigate floating point rounding inaccuracies. If camera Position or ScaledViewport size are extremely small or extremely big, this value can be reduced or increased.
public static float Epsilon
Field Value
MaxScale
The maximum Scale that the camera can have
public float MaxScale
Field Value
MinScale
The minimum Scale that the camera can have
public float MinScale
Field Value
Position
The top-left corner of the camera's viewport. LookingPosition
public Vector2 Position
Field Value
- Vector2
RoundPosition
Whether the camera's Position should be rounded to full integers when calculating the ViewMatrix. If this value is true, the occurence of rendering fragments due to floating point rounding might be reduced.
public bool RoundPosition
Field Value
Properties
ActualScale
The scale that this camera currently has, based on Scale and AutoScaleReferenceSize if AutoScaleWithScreen is true.
public float ActualScale { get; }
Property Value
LookingPosition
The center of the camera's viewport, or the position that the camera is looking at.
public Vector2 LookingPosition { get; set; }
Property Value
- Vector2
Max
The bottom-right corner of the camera's viewport LookingPosition
public Vector2 Max { get; set; }
Property Value
- Vector2
Scale
The scale that this camera's ViewMatrix should have.
public float Scale { get; set; }
Property Value
ScaledViewport
The viewport of this camera, based on the game's Microsoft.Xna.Framework.Graphics.GraphicsDevice.Viewport and this camera's ActualScale
public Vector2 ScaledViewport { get; }
Property Value
- Vector2
ViewMatrix
The matrix that this camera "sees", based on its position and scale.
Use this in your SpriteBatch.Begin calls to render based on the camera's viewport.
public Matrix ViewMatrix { get; }
Property Value
- Matrix
Methods
ConstrainWorldBounds(Vector2, Vector2)
Forces the camera's bounds into the given min and max positions in world space. If the space represented by the given values is smaller than what the camera can see, its position will be forced into the center of the area.
public bool ConstrainWorldBounds(Vector2 min, Vector2 max)
Parameters
minVector2The top left bound, in world space
maxVector2The bottom right bound, in world space
Returns
- bool
Whether or not the camera's position changed as a result of the constraint
GetVisibleRectangle()
Returns the area that this camera can see, in world space. This can be useful for culling of tile and other entity renderers.
public RectangleF GetVisibleRectangle()
Returns
- RectangleF
A rectangle that represents the camera's visible area in world space
ToCameraPos(Vector2)
Converts a given position in world space to screen space
public Vector2 ToCameraPos(Vector2 pos)
Parameters
posVector2The position in world space
Returns
- Vector2
The position in camera space
ToWorldPos(Vector2)
Converts a given position in screen space to world space
public Vector2 ToWorldPos(Vector2 pos)
Parameters
posVector2The position in screen space
Returns
- Vector2
The position in world space
Zoom(float, Vector2?)
Zoom in the camera's view by a given amount, optionally focusing on a given center point.
public void Zoom(float delta, Vector2? zoomCenter = null)
Parameters
deltafloatThe amount to zoom in or out by
zoomCenterVector2?The position that should be regarded as the zoom's center, in screen space. The default value is the center.