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.
Example
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.yConstructor
new SolidWorld(bucketSize?: integer): SolidWorldParameters
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): void
Adds a solid rectangle.
Parameters
x(float) — bottom-left xy(float) — bottom-left yw(float)h(float)
Returns
void
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 insidey(float) — bottom-left yw(float)h(float)thickness(float, optional, default: 64) — how thick each wall is
Returns
void
getSolidCount(): integer
Returns
integer— how many solids have been added
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
Array.<BGE.SolidWorldSolid>
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 xy(float) — bottom-left yw(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 cornerdx(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