Class PhysicsWorld
java.lang.Object
com.codename1.gaming.physics.PhysicsWorld
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 Summary
ConstructorsConstructorDescriptionPhysicsWorld(float gravityXPx, float gravityYPx) Creates a world with the given gravity in pixels per second squared. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddContactListener(ContactListener listener) Registers a contact listener notified when bodies start and stop touching.Creates a rectangular body centered at the given pixel position.createCircle(float xPx, float yPx, float radiusPx, BodyType type) Creates a circular body centered at the given pixel position.createPolygon(float xPx, float yPx, float[] verticesPx, BodyType type) Creates a convex polygon body.Returns the underlying shaded Box2D world for advanced use.floatvoidremoveBody(PhysicsBody body) Removes a body from the world.voidremoveContactListener(ContactListener listener) voidsetGravity(float gxPx, float gyPx) Sets gravity in pixels per second squared (positive y is downward).voidsetPixelsPerMeter(float ppm) The pixels-per-meter scale used to convert between screen and simulation units.voidsetPositionIterations(int positionIterations) voidsetVelocityIterations(int velocityIterations) voidstep(float deltaSeconds) Advances the simulation by the given time step (seconds) and then syncs every body's transform into its linkedPhysicsLinkable(typically acom.codename1.gaming.Sprite).voidPushes each body's current transform into its linked object, converting meters to pixels and flipping the y axis.
-
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 linkedPhysicsLinkable(typically acom.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
Creates a rectangular body centered at the given pixel position. -
createCircle
Creates a circular body centered at the given pixel position. -
createPolygon
Creates a convex polygon body. The vertices are pixel offsets relative to the body center, as alternating x,y pairs (soverticesPx.lengthmust be even). -
removeBody
Removes a body from the world. -
addContactListener
Registers a contact listener notified when bodies start and stop touching. -
removeContactListener
-
getNativeWorld
Returns the underlying shaded Box2D world for advanced use. Coordinates on the native world are in meters with y pointing up.
-