Client and backend. This class is shared: the same code is compiled into your app and into your server, so everything on this page works in both.

public final class Body

  1. Object
  2. Body

A kinematic box that moves through a TileGrid: the platformer’s collision controller.

It is not a physics body. Each tick it is given a velocity and moves the X axis and then the Y axis, scanning exactly the cells its edges cross, so it never tunnels and never jitters. It understands what a platformer needs and a rigid body simulation does not:

  • Slopes of 45 and 22.5 degrees, walked up and down at full speed without bouncing: while grounded the box is snapped down to the floor by up to getStepHeight(), and steps of that height are climbed.
  • One-way platforms (Tile#ONE_WAY, the top rung of a ladder, a MovingPlatform) that only stop a box falling onto them from above, and that dropThrough lets it fall through.
  • Riding: a box standing on a moving platform is carried by it.
  • Contacts on each side, read back by the abilities: grounded, a ceiling, a wall on the left or right.

Coordinates are fixed point (Fx), y grows downward, and x, y are the top-left corner.

Fields

public int xLeft edge, fixed point.
public int yTop edge, fixed point.
public int wWidth, fixed point.
public int hHeight, fixed point.
public int vxHorizontal velocity, fixed point per tick.
public int vyVertical velocity, fixed point per tick; positive is down.

Constructors

public Body(int x, int y, int w, int h)Creates a box.

Methods

public int getStepHeight()The tallest step the box climbs, and the furthest it is pulled down to keep its footing on a falling slope, while grounded.
public void setStepHeight(int stepHeight)Sets the step height.
public void dropThrough(int ticks)Lets the box fall through one-way platforms for a few ticks.
public void setIgnoreOneWay(boolean ignore)Makes one-way floors, including the top of a ladder, ignored until cleared, as climbing down a ladder needs.
public boolean isGrounded()Whether the box stood on something after its last move.
public boolean hitCeiling()Whether the box hit a ceiling in its last move.
public boolean hitLeft()Whether the box was stopped by a wall on its left in its last move.
public boolean hitRight()Whether the box was stopped by a wall on its right in its last move.
public boolean isOnOneWay()Whether the floor under the box is one-way (a drop-through platform).
public int getSlope()The slope the box stands on.
public int getPreviousY()The top edge before the last move, which tells a stomp from a side hit.
public boolean overlaps(int ox, int oy, int ow, int oh)Whether the box overlaps a rectangle.
public MovingPlatform getRiding()The moving platform the box stands on.
public void move(TileGrid grid, List<MovingPlatform> platforms)Moves the box by its velocity through the grid and the platforms, updating the contact flags.

Inherited methods

Field details

x

public int x
Left edge, fixed point.

y

public int y
Top edge, fixed point.

w

public int w
Width, fixed point.

h

public int h
Height, fixed point.

vx

public int vx
Horizontal velocity, fixed point per tick.

vy

public int vy
Vertical velocity, fixed point per tick; positive is down.

Constructor details

Body

public Body(int x, int y, int w, int h)
Creates a box.

Parameters

x int
left, fixed point
y int
top, fixed point
w int
width, fixed point
h int
height, fixed point

Method details

getStepHeight

public int getStepHeight()
The tallest step the box climbs, and the furthest it is pulled down to keep its footing on a falling slope, while grounded.

Returns

the step height, fixed point

setStepHeight

public void setStepHeight(int stepHeight)
Sets the step height. Keep it below half a tile; it must exceed the largest horizontal speed for a 45 degree slope to be climbed.

Parameters

stepHeight int
the step height, fixed point

dropThrough

public void dropThrough(int ticks)
Lets the box fall through one-way platforms for a few ticks.

Parameters

ticks int
how long one-way floors are ignored

setIgnoreOneWay

public void setIgnoreOneWay(boolean ignore)
Makes one-way floors, including the top of a ladder, ignored until cleared, as climbing down a ladder needs.

Parameters

ignore boolean
true to ignore one-way floors

isGrounded

public boolean isGrounded()
Whether the box stood on something after its last move.

Returns

true when grounded

hitCeiling

public boolean hitCeiling()
Whether the box hit a ceiling in its last move.

Returns

true on a ceiling hit

hitLeft

public boolean hitLeft()
Whether the box was stopped by a wall on its left in its last move.

Returns

true on a left wall

hitRight

public boolean hitRight()
Whether the box was stopped by a wall on its right in its last move.

Returns

true on a right wall

isOnOneWay

public boolean isOnOneWay()
Whether the floor under the box is one-way (a drop-through platform).

Returns

true on a one-way floor

getSlope

public int getSlope()
The slope the box stands on.

Returns

a slope Tile kind, or Tile#EMPTY on flat ground or in the air

getPreviousY

public int getPreviousY()
The top edge before the last move, which tells a stomp from a side hit.

Returns

the previous y, fixed point

overlaps

public boolean overlaps(int ox, int oy, int ow, int oh)
Whether the box overlaps a rectangle.

Parameters

ox int
left, fixed point
oy int
top, fixed point
ow int
width, fixed point
oh int
height, fixed point

Returns

true on overlap

getRiding

public MovingPlatform getRiding()
The moving platform the box stands on.

Returns

the platform, or null

move

public void move(TileGrid grid, List<MovingPlatform> platforms)
Moves the box by its velocity through the grid and the platforms, updating the contact flags. Velocity into a contact is zeroed.

Parameters

grid TileGrid
the level
platforms List<MovingPlatform>
the moving platforms, already stepped this tick; may be null