Skip to content

Parallax 2D

  • Parallax2D is a modifier that moves a layer at a depth relative to a camera.
  • A far layer follows less of the camera movement than the world, a near layer follows more.
  • It writes the Transform of the entity, the same way physics moves a body.
  • It also scales the layer with the camera zoom, and can wrap the layer so it repeats.
  • It runs after the other modifiers each tick (#phase(after, tick)).
  • Make a root entity for the layer, with a Transform.
  • Link the layer content (sprites, tiles, anything with a transform) under the root.
  • Attach Parallax2D to the root.
  • The layer looks the way it was placed when seen from the camera start position at zoom 1.
import "luxe: world" for Entity, Transform
import "luxe: system/parallax2D.modifier" for Parallax2D
var layer = Entity.create(world)
Transform.create(layer)
Parallax2D.create(layer)
  • mode is background, foreground or custom.
  • distance is 0 to 1 and means how far the layer is from the gameplay layer.
  • 0 moves with the world in both modes.
  • A background at 1 stays with the camera (a sky).
  • A foreground at 1 moves at foreground_ratio times the world (default 2).
  • axes limits the parallax to x or y, the other axis moves with the world.
  • camera is a link to the camera the layer follows.
  • Left empty, the layer follows the default camera of the world.
  • A camera change does not move the layer, the new camera becomes the reference.
  • Parallax2D.get_camera(entity) returns the camera in use, or null.
  • zoom is follow or custom.
  • follow zooms the layer at the same depth as its movement.
  • A background at distance 1 does not zoom, a layer at distance 0 zooms with the world.
  • custom uses zoom_distance instead, with the same meaning as distance.
  • The layer scales around the camera, so it stays in place on screen.
  • span is the length one repeat covers, per axis, in the local space of the layer.
  • Each direct child is kept within half a span of the view center by moving it a whole span.
  • A layer with no children wraps itself.
  • 0 on an axis does not wrap that axis.
  • The children have to cover the view plus one repeat at the widest zoom out used.
  • autoscroll adds a constant movement in world units per second.
  • It uses the world delta, so the world rate and pausing apply.
  • mode custom shows ratio, the movement factor per axis.
  • 1 moves with the world, 0 stays with the camera, above 1 passes faster than the world.
  • With mode custom, zoom_distance is a raw zoom factor.
  • Parallax2D.get_factor(entity) returns the movement factor as [x, y].
  • Parallax2D.get_zoom_factor(entity) returns the zoom factor.
  • foreground_ratio (under advanced) sets the movement factor of a foreground at distance 1.
  • Parallax2D.rebase(entity) takes the current camera position as the new reference.
  • Parallax layers inside parallax layers add their movement together.
  • Gameplay moves on a layer stay, the parallax adds to them.
  • Transform snap is respected, small moves accumulate until they reach a snap step.