Class PhysicsWorld

java.lang.Object
com.codename1.gaming.physics.PhysicsWorld

public class PhysicsWorld extends Object

A 2D rigid body physics world, wrapping a shaded Box2D (JBox2D) simulation in an idiomatic Codename One API.

The world works in pixels on the outside and meters internally (Box2D is tuned for objects a few meters in size, so feeding it pixels directly produces a sluggish, unstable simulation). The conversion is governed by #setPixelsPerMeter(float) (default 30). The screen y axis points down while Box2D's points up; the wrapper flips y so application code stays in screen coordinates -- so a positive gravity y value pulls bodies down the screen.

Drive the simulation from a com.codename1.gaming.GameView update loop:

PhysicsWorld world = new PhysicsWorld(0, 600); // gravity 600 px/s^2 downward
PhysicsBody ground = world.createBox(0, 460, 320, 40, BodyType.STATIC);
PhysicsBody crate = world.createBox(160, 0, 32, 32, BodyType.DYNAMIC);
crate.setLinkedSprite(crateSprite);
// in update(dt):
world.step((float) dt);   // integrates and syncs linked sprites
  • Constructor Details

    • PhysicsWorld

      public PhysicsWorld(float gravityXPx, float gravityYPx)
      Creates a world with the given gravity in pixels per second squared. A positive y pulls bodies down the screen.
  • Method Details

    • setPixelsPerMeter

      public void setPixelsPerMeter(float ppm)
      The pixels-per-meter scale used to convert between screen and simulation units. Set this once before creating bodies.
    • getPixelsPerMeter

      public float getPixelsPerMeter()
    • setVelocityIterations

      public void setVelocityIterations(int velocityIterations)
    • setPositionIterations

      public void setPositionIterations(int positionIterations)
    • setGravity

      public void setGravity(float gxPx, float gyPx)
      Sets gravity in pixels per second squared (positive y is downward).
    • step

      public void step(float deltaSeconds)
      Advances the simulation by the given time step (seconds) and then syncs every body's transform into its linked PhysicsLinkable (typically a com.codename1.gaming.Sprite). Call once per frame from the game loop.
    • syncSprites

      public void syncSprites()
      Pushes each body's current transform into its linked object, converting meters to pixels and flipping the y axis. Called automatically by #step(float).
    • createBox

      public PhysicsBody createBox(float xPx, float yPx, float widthPx, float heightPx, BodyType type)
      Creates a rectangular body centered at the given pixel position.
    • createCircle

      public PhysicsBody createCircle(float xPx, float yPx, float radiusPx, BodyType type)
      Creates a circular body centered at the given pixel position.
    • createPolygon

      public PhysicsBody createPolygon(float xPx, float yPx, float[] verticesPx, BodyType type)
      Creates a convex polygon body. The vertices are pixel offsets relative to the body center, as alternating x,y pairs (so verticesPx.length must be even).
    • removeBody

      public void removeBody(PhysicsBody body)
      Removes a body from the world.
    • addContactListener

      public void addContactListener(ContactListener listener)
      Registers a contact listener notified when bodies start and stop touching.
    • removeContactListener

      public void removeContactListener(ContactListener listener)
    • getNativeWorld

      public World getNativeWorld()
      Returns the underlying shaded Box2D world for advanced use. Coordinates on the native world are in meters with y pointing up.