Parallax 2D
What it is
Section titled “What it is”Parallax2Dis 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
Transformof 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)).
Basic use
Section titled “Basic use”- Make a root entity for the layer, with a
Transform. - Link the layer content (sprites, tiles, anything with a transform) under the root.
- Attach
Parallax2Dto 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, Transformimport "luxe: system/parallax2D.modifier" for Parallax2D
var layer = Entity.create(world)Transform.create(layer)Parallax2D.create(layer)modeisbackground,foregroundorcustom.distanceis 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_ratiotimes the world (default 2). axeslimits the parallax toxory, the other axis moves with the world.
Camera
Section titled “Camera”camerais 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.
zoomisfolloworcustom.followzooms 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.
customuseszoom_distanceinstead, with the same meaning asdistance.- The layer scales around the camera, so it stays in place on screen.
Repeating
Section titled “Repeating”spanis 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.
autoscrolladds a constant movement in world units per second.- It uses the world delta, so the world rate and pausing apply.
Raw values
Section titled “Raw values”modecustom showsratio, the movement factor per axis.- 1 moves with the world, 0 stays with the camera, above 1 passes faster than the world.
- With
modecustom,zoom_distanceis a raw zoom factor. Parallax2D.get_factor(entity)returns the movement factor as[x, y].Parallax2D.get_zoom_factor(entity)returns the zoom factor.
Advanced
Section titled “Advanced”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.