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
- Object
- 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, aMovingPlatform) that only stop a box falling onto them from above, and thatdropThroughlets 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 x | Left edge, fixed point. |
public int y | Top edge, fixed point. |
public int w | Width, fixed point. |
public int h | Height, fixed point. |
public int vx | Horizontal velocity, fixed point per tick. |
public int vy | Vertical 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 xLeft edge, fixed point.
y
public int yTop edge, fixed point.
w
public int wWidth, fixed point.
h
public int hHeight, fixed point.
vx
public int vxHorizontal velocity, fixed point per tick.
vy
public int vyVertical 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
xint- left, fixed point
yint- top, fixed point
wint- width, fixed point
hint- 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
stepHeightint- the step height, fixed point
dropThrough
public void dropThrough(int ticks)Lets the box fall through one-way platforms for a few ticks.
Parameters
ticksint- 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
ignoreboolean- 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 airgetPreviousY
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
oxint- left, fixed point
oyint- top, fixed point
owint- width, fixed point
ohint- 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
gridTileGrid- the level
platformsList<MovingPlatform>- the moving platforms, already stepped this tick; may be null