Velaptor.Camera2D
Velaptor
Velaptor
Camera2D Class
A simple 2-D camera that applies pan, zoom, and rotation transforms on the CPU for world-to-screen coordinate conversion.
public sealed class Camera2D : Velaptor.ICamera2D
Inheritance System.Object → Camera2D
Implements ICamera2D
Remarks
Works in screen pixel coordinates: top-left origin, Y increases downward. Position is a pan offset — positive X shifts the view right, which makes scene content appear to move left (standard game-camera convention).
Unlike GPU-uniform cameras, transforms are applied entirely on the CPU before vertex data is submitted. Call TransformPosition(Vector2) and TransformSize(float) when building render data each frame.
Zoom: 1.0 = one world pixel maps to exactly one screen pixel. Values above 1.0 magnify the scene; values below 1.0 shrink it. Zoom is applied relative to the screen center so the center of the window stays fixed while zooming.
Constructors
Camera2D() Constructor
Initializes a new instance of the Camera2D class.
public Camera2D();
Properties
Position
Gets or sets the camera pan offset in screen pixel units. Moving the camera right (positive X) shifts all scene content leftward. Default is System.Numerics.Vector2.Zero (no pan).
public System.Numerics.Vector2 Position { get; set; }
Implements Position
Property Value
Rotation
Gets or sets the camera rotation in degrees. Positive values rotate the view
clockwise. Default is 0 (no rotation). Rotation is applied around the
screen center after zoom.
public float Rotation { get; set; }
Implements Rotation
Property Value
WindowSize
Gets the current window size in pixels. Used to locate the screen center for zoom calculations. Updated automatically on window resize.
public Velaptor.SizeU WindowSize { get; }
Implements WindowSize
Property Value
WorldBounds
Gets the axis-aligned bounds of the visible world area in world pixel coordinates. When the camera is rotated, this is the bounding box of the rotated view frustum.
public System.Drawing.RectangleF WorldBounds { get; }
Implements WorldBounds
Property Value
Zoom
Gets or sets the zoom factor. 1.0 = native pixel size.
Values above 1.0 magnify; values below shrink. Default is 1.0.
public float Zoom { get; set; }
Implements Zoom
Property Value
ZoomMax
Gets or sets the maximum zoom level. Default is 4.0.
public float ZoomMax { get; set; }
Implements ZoomMax
Property Value
ZoomMin
Gets or sets the minimum zoom level. Default is 0.1.
public float ZoomMin { get; set; }
Implements ZoomMin
Property Value
Methods
IsVisible(Vector2)
Determines whether a world-space point is visible on the screen.
public bool IsVisible(System.Numerics.Vector2 worldPos);
Parameters
worldPos System.Numerics.Vector2
The position in world pixel coordinates.
Implements IsVisible(Vector2)
Returns
System.Boolean
true if the point is within the visible screen area; otherwise, false.
IsVisible(Vector2, Vector2)
Determines whether a world-space rectangle intersects the visible screen area. Use this for culling objects with size (sprites, tiles, etc.).
public bool IsVisible(System.Numerics.Vector2 worldPos, System.Numerics.Vector2 worldSize);
Parameters
worldPos System.Numerics.Vector2
The top-left position of the object in world pixel coordinates.
worldSize System.Numerics.Vector2
The size of the object in world pixels.
Implements IsVisible(Vector2, Vector2)
Returns
System.Boolean
true if any part of the rectangle is visible; otherwise, false.
TransformPosition(Vector2)
Transforms a world-space pixel position into a screen render position, accounting for camera pan and zoom.
public System.Numerics.Vector2 TransformPosition(System.Numerics.Vector2 worldPos);
Parameters
worldPos System.Numerics.Vector2
The position in world pixel coordinates.
Implements TransformPosition(Vector2)
Returns
System.Numerics.Vector2
The position in screen pixel coordinates.
TransformRotation(float)
Transforms a world-space rotation angle (in degrees) into a screen-space rotation angle, accounting for camera rotation. Rotation is additive: positive camera rotation (clockwise) adds to the object's rotation.
public float TransformRotation(float worldRotation);
Parameters
worldRotation System.Single
The rotation angle in world space, in degrees.
Implements TransformRotation(float)
Returns
System.Single
The rotation angle in screen space, in degrees.
TransformSize(float)
Scales a size value by the current zoom level. Use this for width, height, radius, and other size-related values.
public float TransformSize(float size);
Parameters
size System.Single
The size in world pixels.
Implements TransformSize(float)
Returns
System.Single
The scaled size in screen pixels.