SolidWorld

A set of static, axis-aligned solid rectangles for top-down movement (walls, water, prop bases), and moveAndSlide() to move a box through them: it stops flush against solids, slides along walls, and eases around corners it only just clips.

Every rectangle is {x, y, w, h} - (x, y) is the bottom-left corner, world +y up. This is plain rectangle maths, separate from colliders: colliders report overlaps, while a SolidWorld keeps a mover out of them. For platformer tiles (including one-way platforms) see BGE.resolveAabbTileCollision() instead.

Properties

Example

CODE
world = new BGE.SolidWorld()
world.addBounds(0, 0, mapWidth, mapHeight)
world.addSolid(64, 64, 32, 32)
' Each frame, move the player's feet box:
result = world.moveAndSlide({x: feetX, y: feetY, w: 20, h: 12}, dx, dy)
feetX = result.x
feetY = result.y

Or track a body and resolve every tracked body once everything has moved it (its own
movement, knockback, being pushed apart): each one's move since the last resolve is replayed
through the solids.
CODE
world.track(knight, 12) ' a 24x24 feet box centred on the knight, on the x/z ground plane
' In GameScene.onPostUpdate(), after BGE.separateCircles():
world.resolveTracked()
' Or have the game call it right after onPostUpdate():
m.trackedSolidWorld = world

Constructor

new SolidWorld(bucketSize?: integer): SolidWorld

Parameters

  • bucketSize (integer, optional, default: 128) — solids are grouped in square cells this size (px), so a move only tests nearby solids. Roughly the size of a typical solid works well. 0 or less uses the default.

Instance Methods

addSolid(x: float, y: float, w: float, h: float): string

Adds a solid rectangle.

Parameters

  • x (float) — bottom-left x
  • y (float) — bottom-left y
  • w (float)
  • h (float)

Returns

  • string — the solid's id, for removeSolid()

removeSolid(id: string): boolean

Removes a solid, e.g. a door that has opened. Movers can pass through it from then on.

Parameters

  • id (string) — from addSolid()

Returns

  • boolean — true if a solid was removed, false if no solid has that id

addBounds( x: float, y: float, w: float, h: float, thickness?: float, ): void

Adds four solids around the outside of a rectangle, so movers can't leave it (e.g. the edges of a map).

Parameters

  • x (float) — bottom-left x of the area to keep movers inside
  • y (float) — bottom-left y
  • w (float)
  • h (float)
  • thickness (float, optional, default: 64) — how thick each wall is

Returns

  • void

getSolidCount(): integer

Returns

  • integer — how many solids there are now

getSolids(): Array.<BGE.SolidWorldSolid>

Every solid added so far, e.g. for drawing them while debugging. Each is a copy, so changing one doesn't move the real solid.

Returns

getSolidsInArea( x: float, y: float, w: float, h: float, ): Array.<BGE.SolidWorldSolid>

The solids overlapping a box (touching edges don't count), as copies.

Parameters

  • x (float) — bottom-left x
  • y (float) — bottom-left y
  • w (float)
  • h (float)

Returns

isAreaFree(x: float, y: float, w: float, h: float): boolean

Whether a box overlaps no solid. A box only touching a solid's edge is free.

Parameters

  • x (float) — bottom-left x
  • y (float) — bottom-left y
  • w (float)
  • h (float)

Returns

  • boolean

moveAndSlide( mover: object, dx: float, dy: float, options?: BGE.SlideOptions, ): BGE.SlideResult

Moves a box by (dx, dy), stopping flush against solids and sliding along them. When moving along one axis only, a box that clips a corner by up to options.cornerNudge is eased around it. The box must start clear of every solid.

Parameters

  • mover (object) — {x, y, w, h}: the moving box, bottom-left corner
  • dx (float)
  • dy (float)
  • options (BGE.SlideOptions, optional, default: "{}") — cornerNudge (default 8, 0 = off), maxStep (default 4)

Returns

  • BGE.SlideResult — the box's new bottom-left corner, and which axes were blocked

track( entity: BGE.SolidTrackable, radius: float, options?: BGE.SolidTrackOptions, ): void

Starts keeping entity out of solids from where it is now: resolveTracked() replays each move it makes after this. Its feet box is a square, radius * 2 wide, centred on its position. Tracking an entity again updates its settings and forgets its move so far: do that after teleporting it (e.g. a respawn), or the jump is replayed as a move. The entity must start clear of every solid.

Parameters

Returns

  • void

untrack(entity: BGE.SolidTrackable): boolean

Stops keeping entity out of solids.

Parameters

Returns

  • boolean — true if it was tracked

untrackAll(): void

Stops tracking every body.

Returns

  • void

isTracked(entity: BGE.SolidTrackable): boolean

Parameters

Returns

  • boolean — whether entity is tracked (an invalidated entity never is)

getTrackedCount(): integer

Returns

  • integer — how many bodies are tracked, counting invalidated ones not yet dropped by resolveTracked()

resolveTracked(): void

Slides every tracked body back out of the solids: its move since the last resolve (or since track()) is replayed through moveAndSlide(), so it stops flush against solids and slides along them. Call once a frame after everything has moved the bodies, e.g. at the end of GameScene.onPostUpdate(), or set the scene's trackedSolidWorld to have the game call it. Invalidated entities are dropped.

Returns

  • void

resolveTrackedBody(entry: BGE.SolidWorldTrackedBody): void

protected

Parameters

Returns

  • void