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.
public sealed class Camera2DInheritance
Examples
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.
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.
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
presentation is null.
Properties
Bounds
Optional world limits; the view is kept inside when the world is larger than the view.
public RectF? Bounds { get; set; }Property Value
Center
The world point at the centre of the view.
public Vector2 Center { get; }Property Value
DeadZone
Half-size of the dead zone around the view centre in which the target can move freely.
public Vector2 DeadZone { get; set; }Property Value
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.
public Vector2? DrawPosition { get; set; }Property Value
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.
public Vector2 DrawnPosition { get; }Property Value
FollowSharpness
Per-second follow sharpness; 0 snaps immediately.
public float FollowSharpness { get; set; }Property Value
MaxShake
Maximum shake offset in pixels at full trauma.
public float MaxShake { get; set; }Property Value
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.
public bool PixelSnap { get; set; }Property Value
Position
The world point at the top-left of the view, in pixels. Setting it directly does not apply Bounds; call Clamp for that.
public Vector2 Position { get; set; }Property Value
RenderOffset
The translation the renderer should apply (negative DrawnPosition plus shake), snapped if requested.
public Vector2 RenderOffset { get; }Property Value
Rotation
Rotation of the view around its centre, in radians; positive turns the world clockwise on screen (0 by default).
public float Rotation { get; set; }Property Value
ShakeOffset
Shake offset applied on top of DrawnPosition when rendering.
public Vector2 ShakeOffset { get; }Property Value
View
The world rectangle the camera shows, without shake.
public RectF View { get; }Property Value
ViewHeight
Height of the view in pixels, usually the virtual canvas height.
public float ViewHeight { get; }Property Value
ViewToWorld
Canvas pixels to world coordinates: the inverse of WorldToView.
public Matrix3x2 ViewToWorld { get; }Property Value
ViewWidth
Width of the view in pixels, usually the virtual canvas width.
public float ViewWidth { get; }Property Value
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.
public RectF VisibleBounds { get; }Property Value
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.
public Matrix3x2 WorldToView { get; }Property Value
Zoom
Magnification around the view’s centre: 2 shows half as much of the world, twice as large (1 by default).
public float Zoom { get; set; }Property Value
Exceptions
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.
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.
public void Clamp()Follow(Vector2, float)
Moves toward keeping target inside the dead zone.
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.
public bool IsVisible(RectF world, float margin = 0)Parameters
world RectF
margin float
Returns
ScreenToWorld(Vector2)
Converts a virtual-canvas point (for example a pointer position) to world pixels, including shake.
public Vector2 ScreenToWorld(Vector2 screen)Parameters
screen Vector2
Returns
Shake(float)
Adds shake trauma (0..1). Shake strength is trauma squared, decaying over time.
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.
public void Update(float dt)Parameters
dt float
Elapsed time in seconds.
WorldToScreen(Vector2)
Converts a world point to virtual-canvas pixels, including shake.
public Vector2 WorldToScreen(Vector2 world)Parameters
world Vector2