Class 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.
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoiddropThrough(int ticks) Lets the box fall through one-way platforms for a few ticks.intThe top edge before the last move, which tells a stomp from a side hit.The moving platform the box stands on.intgetSlope()The slope the box stands on.intThe tallest step the box climbs, and the furthest it is pulled down to keep its footing on a falling slope, while grounded.booleanWhether the box hit a ceiling in its last move.booleanhitLeft()Whether the box was stopped by a wall on its left in its last move.booleanhitRight()Whether the box was stopped by a wall on its right in its last move.booleanWhether the box stood on something after its last move.booleanWhether the floor under the box is one-way (a drop-through platform).voidmove(TileGrid grid, List<MovingPlatform> platforms) Moves the box by its velocity through the grid and the platforms, updating the contact flags.booleanoverlaps(int ox, int oy, int ow, int oh) Whether the box overlaps a rectangle.voidsetIgnoreOneWay(boolean ignore) Makes one-way floors, including the top of a ladder, ignored until cleared, as climbing down a ladder needs.voidsetStepHeight(int stepHeight) Sets the step height.
-
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
x: left, fixed pointy: top, fixed pointw: width, fixed pointh: 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: 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: 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: 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
Tilekind, orTile#EMPTYon 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: left, fixed pointoy: top, fixed pointow: width, fixed pointoh: height, fixed point
Returns
true on overlap
-
getRiding
The moving platform the box stands on.
Returns
the platform, or null
-
move
Moves the box by its velocity through the grid and the platforms, updating the contact flags. Velocity into a contact is zeroed.
Parameters
grid: the levelplatforms: the moving platforms, already stepped this tick; may be null
-