Skip to content

2D games ​

A World2D is a box in a UI surface that shows a 2D world: the Sprites, AnimatedSprites and TileMaps inside it. It lays out like any other node, so it can fill the window or sit in a corner as a minimap, and UI nodes inside it draw over the world as a HUD.

luau
local world = Ui.World2D({ Width = "100%", Height = "100%", Zoom = 3 })
Ui.Surface({ Target = Enum.SurfaceTarget.Window, Children = { world } }).Parent = Scene

local level = Instance.new("TileMap")
level.TileSet = "assets/Tiles.png"
level.TileSize = Vector2.new(16, 16)
level:Fill(Vector2.new(-20, 2), Vector2.new(20, 2), 0) -- a floor of tile 0
level:SetTileSolid(0)
level.Parent = world

local hero = Instance.new("AnimatedSprite")
hero.Image = "assets/Hero.png"
hero.FrameSize = Vector2.new(16, 16)
hero:AddAnimation("Idle", { 0, 1 }, 2)
hero:AddAnimation("Run", { 8, 9, 10, 11 }, 12)
hero:Play("Idle")
hero.Parent = world
world.CameraSubject = hero

World units are pixels at a Zoom of 1, and y grows downwards, as on the screen. The camera's point, CameraPosition or the position of CameraSubject when that is set, is at the centre of the world's box.

World2D memberDoes
CameraPosition, ZoomWhere the camera looks and how many screen pixels a world unit takes
CameraSubjectA sprite the camera follows, or nil
WorldToScreen(position), ScreenToWorld(position)Between world units and the surface's pixels, for mouse clicks and labels over sprites
Move(sprite, delta)Moves a sprite, stopping at walls. See Movement
Raycast(origin, direction, ignore?)The first hitbox or solid tile along direction (its length is how far to look), as {Instance, Position, Normal, Distance, Cell?}, or nil
Overlap(center, size, ignore?)The sprites and tile maps touching a box
GetOverlapping(sprite)What touches a sprite's hitbox, such as the coins a player picks up
GetSpritesAt(position)The visible sprites drawn at a point, topmost first

Queries find sprites and tile maps whose CanQuery is true, and in a tile map only the tiles marked solid. A ray that starts inside a box passes over it, as Roblox's raycasts do.

Sprites ​

A Sprite draws its Image (a path relative to the project, or an http URL with the Http permission) so that its Pivot (0 to 1 across its box, the middle by default) is at Position. It is drawn Size world units big, or the size of its region, or of the image.

Sprite memberDoes
Image, Position, Size, Pivot, RotationWhat it shows and where. Rotation is in degrees, clockwise
RegionOffset, RegionSizeA part of the image in its pixels, for one sprite of a sheet
FlipX, FlipYMirrors it, such as a character facing left
Color, Transparency, VisibleTints, fades or hides it
ZIndexHigher draws on top. Equal ones draw in tree order, tile maps included
PixelatedTrue by default, so pixel art stays crisp. False smooths it
CanCollideFalse by default. True blocks other sprites' Move
CanQueryTrue by default. False hides it from raycasts and overlaps
HitboxSize, HitboxOffsetThe box collision and queries use: its own size when 0, centred on it. It doesn't turn with Rotation
OnFloor, OnWall, OnCeilingRead only, set by the last Move

An AnimatedSprite is a sprite that shows one frame of a sprite sheet. Frames are FrameSize pixels, numbered from 0 left to right and then down, Columns to a row (as many as fit across the image when 0). Set Frame to show one yourself, or add animations and play them; they advance every step.

AnimatedSprite memberDoes
AddAnimation(name, frames, fps?, looped?)frames is a list of frame numbers. 10 frames a second and looped unless you say otherwise
Play(name?)Starts an animation from its first frame, or resumes the current one
Pause(), Stop()Stop also goes back to the first frame
Frame, Animation, Playing, SpeedScaleAnimation and Playing are read only
FrameChanged, AnimationFinishedAnimationFinished fires once an animation that doesn't loop has shown its last frame

Animations are set up by scripts and aren't saved with a place.

Tile maps ​

A TileMap is a grid of tiles from one TileSet image. Tiles are TileSize pixels in the image, numbered like sprite sheet frames, and cell x, y covers CellSize world units (TileSize when 0) from Position + Vector2.new(x, y) * CellSize. Only the cells in view are drawn, so a map can be big.

TileMap memberDoes
SetCell(x, y, tile), GetCell(x, y)-1 is an empty cell
Fill(from, to, tile)Sets every cell in a rectangle, corners included
Clear(), GetUsedCells()
WorldToCell(position), CellToWorld(cell)CellToWorld gives a cell's top left corner
SetTileSolid(tile, solid?), IsTileSolid(tile), IsCellSolid(x, y)Solid tiles block Move and are found by queries. No tile is solid until you say so
CanCollide, CanQueryTurn the map's solid tiles off for movement or for queries
Columns, Color, Transparency, Visible, ZIndex, PixelatedAs for sprites
Cells, SolidTilesThe cells as x,y,tile;... and the solid tiles as 1,2,..., which is how places save them

Movement ​

World2D:Move(sprite, delta) moves a sprite across and then down, stopping each at the first colliding sprite or solid tile in the way, and returns how far it went. Afterwards the sprite's OnFloor, OnWall and OnCeiling say what it ran into. A sprite already overlapping something can always move out of it.

A platformer's player, with gravity applied every step so OnFloor stays right:

luau
local velocity = Vector2.zero
Run.Stepped:Connect(function(_, deltaTime)
	local direction = (Input:IsKeyDown(Enum.KeyCode.D) and 1 or 0) - (Input:IsKeyDown(Enum.KeyCode.A) and 1 or 0)
	velocity = Vector2.new(direction * 80, velocity.Y + 400 * deltaTime)
	if hero.OnFloor and Input:IsKeyDown(Enum.KeyCode.Space) then
		velocity = Vector2.new(velocity.X, -180)
	end
	world:Move(hero, velocity * deltaTime)
	if hero.OnFloor or hero.OnCeiling then
		velocity = Vector2.new(velocity.X, 0)
	end
	if direction ~= 0 then
		hero.FlipX = direction < 0
	end
	for _, touched in world:GetOverlapping(hero) do
		if touched:HasTag("Coin") then
			touched:Destroy()
		end
	end
end)

Collision is between unrotated boxes, which suits platformers and top-down games. For bodies that bounce, spin and stack, use the 3D physics with a side-on camera.

Apache License 2.0