Skip to content

GraphicsAPI reference

Class Camera2D

Namespace: MicroEngine.Graphics
Assembly: MicroEngine.Graphics2D.dll
Guide: Graphics

A 2D camera whose Position is the world point at the top-left of the view. Supports smooth following with a dead zone, world bounds and trauma-based shake.

C#
public sealed class Camera2D

Inheritance

object ← Camera2D

Examples

C#
var camera = new Camera2D(Game.Width, Game.Height) { Bounds = new RectF(0, 0, 1280, 360), DeadZone = new Vector2(24, 16) };
// Update:
camera.Follow(player.Center, dt);
camera.Update(dt);
// Draw:
renderer.PushCamera(camera);
DrawWorld(renderer);
renderer.PopCamera();

Remarks

By default one world pixel is one canvas pixel. Zoom scales and Rotation turns the view around its centre; WorldToView is the whole transform. Draw the world between the renderer’s PushCamera and PopCamera, and the interface after the pop.

Constructors

Camera2D(float, float)

Creates a camera at the world origin with the PixelArt defaults (snapped). Prefer the overload that takes the game’s presentation, or Game.CreateCamera(), so the defaults follow the game.

C#
public Camera2D(float viewWidth, float viewHeight)

Parameters

viewWidth float

Width of the view in pixels.

viewHeight float

Height of the view in pixels.

Camera2D(float, float, Presentation)

Creates a camera at the world origin whose defaults (PixelSnap) come from presentation.

C#
public Camera2D(float viewWidth, float viewHeight, Presentation presentation)

Parameters

viewWidth float

Width of the view in pixels.

viewHeight float

Height of the view in pixels.

presentation Presentation

The game’s presentation (GameSettings.Presentation; Game.CreateCamera passes it).

Exceptions

ArgumentNullException

presentation is null.

Properties

Bounds

Optional world limits; the view is kept inside when the world is larger than the view.

C#
public RectF? Bounds { get; set; }

Property Value

RectF?

Center

The world point at the centre of the view.

C#
public Vector2 Center { get; }

Property Value

Vector2

DeadZone

Half-size of the dead zone around the view centre in which the target can move freely.

C#
public Vector2 DeadZone { get; set; }

Property Value

Vector2

DrawPosition

Where the view is drawn when that is not Position, or null (the default) to draw at Position. Set it before drawing to show the camera somewhere the rules do not keep it, for example its position interpolated between the last two updates (a scene world’s DrawPosition2D of an entity that follows the camera): the rules keep reading and moving Position, and drawing never changes it. It stays until it is set again or set to null.

C#
public Vector2? DrawPosition { get; set; }

Property Value

Vector2?

Remarks

Drawing reads it through DrawnPosition: RenderOffset, WorldToView, ViewToWorld, WorldToScreen, ScreenToWorld, VisibleBounds and IsVisible. View, Center, Follow, CenterOn and Clamp are the rules’ and use Position.

DrawnPosition

The position the view is drawn at: DrawPosition when it is set, else Position.

C#
public Vector2 DrawnPosition { get; }

Property Value

Vector2

FollowSharpness

Per-second follow sharpness; 0 snaps immediately.

C#
public float FollowSharpness { get; set; }

Property Value

float

MaxShake

Maximum shake offset in pixels at full trauma.

C#
public float MaxShake { get; set; }

Property Value

float

PixelSnap

Rounds the camera’s translation to whole canvas pixels for crisp pixel art. Its default is the presentation’s (CameraPixelSnap: on for PixelArt, off for HighResolution2D); set it to override. With a zoom or a rotation the translation after them is rounded.

C#
public bool PixelSnap { get; set; }

Property Value

bool

Position

The world point at the top-left of the view, in pixels. Setting it directly does not apply Bounds; call Clamp for that.

C#
public Vector2 Position { get; set; }

Property Value

Vector2

RenderOffset

The translation the renderer should apply (negative DrawnPosition plus shake), snapped if requested.

C#
public Vector2 RenderOffset { get; }

Property Value

Vector2

Rotation

Rotation of the view around its centre, in radians; positive turns the world clockwise on screen (0 by default).

C#
public float Rotation { get; set; }

Property Value

float

ShakeOffset

Shake offset applied on top of DrawnPosition when rendering.

C#
public Vector2 ShakeOffset { get; }

Property Value

Vector2

View

The world rectangle the camera shows, without shake.

C#
public RectF View { get; }

Property Value

RectF

ViewHeight

Height of the view in pixels, usually the virtual canvas height.

C#
public float ViewHeight { get; }

Property Value

float

ViewToWorld

Canvas pixels to world coordinates: the inverse of WorldToView.

C#
public Matrix3x2 ViewToWorld { get; }

Property Value

Matrix3x2

ViewWidth

Width of the view in pixels, usually the virtual canvas width.

C#
public float ViewWidth { get; }

Property Value

float

VisibleBounds

The world rectangle that covers what the canvas shows, without shake: the view at DrawnPosition (View while no DrawPosition is set) with no zoom or rotation, otherwise the bounding box of the canvas’s corners in the world.

C#
public RectF VisibleBounds { get; }

Property Value

RectF

WorldToView

World coordinates to canvas pixels: the drawn view’s centre (at DrawnPosition, with shake) moves to the canvas centre, then the world is rotated by Rotation and scaled by Zoom around it; with PixelSnap the final translation is rounded. With no zoom or rotation it is the translation by RenderOffset.

C#
public Matrix3x2 WorldToView { get; }

Property Value

Matrix3x2

Zoom

Magnification around the view’s centre: 2 shows half as much of the world, twice as large (1 by default).

C#
public float Zoom { get; set; }

Property Value

float

Exceptions

ArgumentOutOfRangeException

The value is not a positive finite number.

Methods

CenterOn(Vector2)

Moves the view so target is at its centre at once, then applies Bounds.

C#
public void CenterOn(Vector2 target)

Parameters

target Vector2

Clamp()

Keeps the view inside Bounds; on an axis where the bounds are smaller than the view, centres the bounds instead. Does nothing without bounds.

C#
public void Clamp()

Follow(Vector2, float)

Moves toward keeping target inside the dead zone.

C#
public void Follow(Vector2 target, float dt)

Parameters

target Vector2

dt float

IsVisible(RectF, float)

Whether world overlaps the visible area, grown by margin plus MaxShake pixels. Use it to skip drawing off-screen objects.

C#
public bool IsVisible(RectF world, float margin = 0)

Parameters

world RectF

margin float

Returns

bool

ScreenToWorld(Vector2)

Converts a virtual-canvas point (for example a pointer position) to world pixels, including shake.

C#
public Vector2 ScreenToWorld(Vector2 screen)

Parameters

screen Vector2

Returns

Vector2

Shake(float)

Adds shake trauma (0..1). Shake strength is trauma squared, decaying over time.

C#
public void Shake(float trauma)

Parameters

trauma float

Update(float)

Decays shake trauma (fully in about 0.6 seconds from 1) and picks a new ShakeOffset. Call once per update.

C#
public void Update(float dt)

Parameters

dt float

Elapsed time in seconds.

WorldToScreen(Vector2)

Converts a world point to virtual-canvas pixels, including shake.

C#
public Vector2 WorldToScreen(Vector2 world)

Parameters

world Vector2

Returns

Vector2