Class Body

java.lang.Object
com.codename1.gaming.platformer.Body

public final class Body extends Object

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.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    int
    Height, fixed point.
    int
    Horizontal velocity, fixed point per tick.
    int
    Vertical velocity, fixed point per tick; positive is down.
    int
    Width, fixed point.
    int
    Left edge, fixed point.
    int
    Top edge, fixed point.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Body(int x, int y, int w, int h)
    Creates a box.
  • Method Summary

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

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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: left, fixed point
      • y: top, fixed point
      • w: width, fixed point
      • h: 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 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: left, fixed point
      • oy: top, fixed point
      • ow: width, fixed point
      • oh: 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: the level
      • platforms: the moving platforms, already stepped this tick; may be null