Skip to content

Class: Camera

Michael Ebens edited this page Jun 22, 2018 · 4 revisions

The Camera class stores the information needed for a camera, and can apply the necessary transformations when drawing. To put it simply, the camera universally alters where things are drawn without the need for changing the coordinates of everything in the world. Each World has a camera of its own residing in World.camera.

You can subclass Camera to create your own types of cameras. For example, you could override Camera:update to follow the player.

Properties

bounds
A table in the form { x1, y1, x2, y2 } containing the minimum and maximum X and Y positions to be used in the bind functions, where x1, y1 are the minimums and x2, y2 are the maximums. Initialised to nil by default.

pos
A Vector for the camera's position. Coordinates are top-left.

angle
The angle of the camera (in radians). The camera will rotate the screen (but not the window).

world
The world this camera is assigned to (if any).

x
A shortcut to Camera.pos.x.

y
A shortcut to Camera.pos.y.

zoom
The zoom level. Defaults to 1, which specifies no zoom. Values above 1 zoom the camera in, values below 1 zoom it out.

Methods

getPosition()
Returns Camera.x and Camera.y.

move(dx, dy)
Adds dx and dy to the camera's X and Y positions, respectively.

dx: The X distance to move by.
dy: The Y distance to move by.

rotate(dr)
Adds dr to the camera's angle.

dr: The amount (in radians) to rotate by.

screenPosition(worldX, worldY) Converts a world point relative to the camera into screen-space coordinates.

worldX: X world-space coordinate.
worldY: Y world-space coordinate.

set(scale)
Sets in place all the necessary transformations. This is called automatically by the world.

scale: When setting the transformations, the X and Y coordinates will be multiplied by this value. This is where a layer's scale is used, allowing parallax effects. Defaults to 1.

setBounds(x1, y1, x2, y2)
Sets the Camera.bounds property.

x1: The minimum X position. y1: The minimum Y position. x2: The maximum X position. y2: The maximum Y position.

setPosition(x, y)
Sets the X and Y position of the camera.

x: The X position to set. If nil, the X position won't be set.
y: The Y position to set. If nil, the Y position won't be set.

start()
Called when the camera is assigned to a world. Does nothing by default.

stop()
Called when the camera is no longer assigned to a world. Does nothing by default.

unset()
Unsets the transformations made by Camera.set. This is called automatically by the world.

update(dt)
Called by the world every frame during the update loop. This does nothing by default.

dt: The delta time.

worldPosition(screenX, screenY)
Converts a screen-space point into world coordinates relative to the camera.

screenX: X screen-space coordinate.
screenY: Y screen-space coordinate.

Clone this wiki locally