Like2D Logo Like2D
← Back to Docs

Physics & Camera

Camera System

A 2D world camera that offsets all rendering. Use this for scrolling levels, following players, and zooming. Camera position represents the center of the view in world coordinates.

setCameraPosition(x, y)

Sets the camera center position in world coordinates. All subsequent renderImage(), drawRect(), and drawTextDirect() calls will be offset by the camera.

Parameters:

-- Follow the player
setCameraPosition(player.x, player.y)

getCameraPosition()

Returns the current camera center position.

Returns: x (number), y (number)

setCameraZoom(zoom)

Sets the camera zoom level. 1.0 = normal, 2.0 = zoomed in 2x, 0.5 = zoomed out.

Parameters:

setCameraZoom(2.0)  -- Zoom in 2x

getCameraZoom()

Returns the current camera zoom level.

Returns: zoom (number)

worldToScreen(wx, wy)

Converts world coordinates to screen coordinates, accounting for camera position and zoom. Useful for positioning UI elements relative to world objects.

Parameters:

Returns: screenX (number), screenY (number)

local sx, sy = worldToScreen(enemy.x, enemy.y)
drawTextDirect("Enemy", sx, sy - 20, 14, 255, 0, 0, 255)

Box2D Physics

Body Management

createBody(x, y, type, fixedRotation)

Creates a physics body. type: "dynamic", "static", or "kinematic". Returns body ID.

local ground = createBody(400, 550, "static", true)
local player = createBody(400, 300, "dynamic", false)

destroyBody(bodyId)

Destroys a physics body.

setBodyPosition(bodyId, x, y)

Teleports body to position.

getBodyPosition(bodyId)

Returns body position (x, y).

getPhysicsTransform(bodyId)

Returns table with x, y, and angle (degrees).

local t = getPhysicsTransform(playerBody)
drawRect(t.x - 25, t.y - 25, 50, 50, 255, 100, 100, 255, true, t.angle)

Velocity & Forces

setBodyVelocity(bodyId, vx, vy)

Sets linear velocity (pixels/second).

getBodyVelocity(bodyId)

Returns current velocity (vx, vy).

applyForce(bodyId, fx, fy)

Applies continuous force to body center.

applyImpulse(bodyId, ix, iy)

Applies instant impulse to body center.

applyImpulse(playerBody, 0, -400)

setGravity(x, y)

Sets world gravity (pixels/second²).

setGravity(0, 980)

Shapes

addBoxShape(bodyId, halfWidth, halfHeight, density, friction, restitution, isSensor)

Adds box collision shape to body.

addBoxShape(playerBody, 25, 25, 1.0, 0.3, 0.5, false)

addCircleShape(bodyId, radius, density, friction, restitution, isSensor)

Adds circle collision shape to body.

addCapsuleShape(bodyId, halfHeight, radius, density, friction)

Adds capsule (pill) shape to body.