public class PhysicsWorld
- Object
- 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
Constructors
public PhysicsWorld(float gravityXPx, float gravityYPx) | Creates a world with the given gravity in pixels per second squared. |
Methods
Inherited methods
Constructor details
PhysicsWorld
public PhysicsWorld(float gravityXPx, float gravityYPx)Method details
setPixelsPerMeter
public void setPixelsPerMeter(float ppm)getPixelsPerMeter
public float getPixelsPerMeter()setVelocityIterations
public void setVelocityIterations(int velocityIterations)setPositionIterations
public void setPositionIterations(int positionIterations)setGravity
public void setGravity(float gxPx, float gyPx)step
public void step(float deltaSeconds)PhysicsLinkable (typically a
com.codename1.gaming.Sprite). Call once per frame from the game loop.syncSprites
public void syncSprites()#step(float).createBox
public PhysicsBody createBox(float xPx, float yPx, float widthPx, float heightPx, BodyType type)createCircle
public PhysicsBody createCircle(float xPx, float yPx, float radiusPx, BodyType type)createPolygon
public PhysicsBody createPolygon(float xPx, float yPx, float[] verticesPx, BodyType type)verticesPx.length must be even).createShape
public PhysicsBody createShape(float xPx, float yPx, Shape shape, BodyType type)Creates a body whose collision outline is a Codename One
com.codename1.ui.geom.Shape – typically a
com.codename1.ui.geom.GeneralPath – so you can describe a hit-shape with the
same geometry API you draw with. The shape’s coordinates are pixel offsets
relative to the body center (xPx, yPx); Bezier curves are flattened to line
segments.
Each subpath becomes one fixture on the body (so a figure with several disconnected outlines yields a compound body). A subpath is converted as follows:
A closed, convex subpath of at most
Settings.maxPolygonVertices(8) points becomes a solidPolygonShape– usable by bodies of anyBodyType.Any other subpath (concave, more than 8 points, or left open) becomes a
ChainShape– an exact but one-sided edge outline. Chains are ideal forBodyType#STATICterrain; a dynamic body needs convex pieces to behave as a solid, so split a concave dynamic shape into convex subpaths.
For a single, simple convex hull #createPolygon(float, float, float[], BodyType)
is a lighter-weight alternative.
removeBody
public void removeBody(PhysicsBody body)addContactListener
public void addContactListener(ContactListener listener)removeContactListener
public void removeContactListener(ContactListener listener)getNativeWorld
public World getNativeWorld()createRevoluteJoint
public PhysicsJoint createRevoluteJoint(PhysicsBody a, PhysicsBody b, float anchorXPx, float anchorYPx)createDistanceJoint
public PhysicsJoint createDistanceJoint(PhysicsBody a, PhysicsBody b, float anchorAXPx, float anchorAYPx, float anchorBXPx, float anchorBYPx, float frequencyHz, float dampingRatio)frequencyHz 0 makes it a rigid rod; a positive value makes it a
softer spring (with dampingRatio 0..1).createWeldJoint
public PhysicsJoint createWeldJoint(PhysicsBody a, PhysicsBody b, float anchorXPx, float anchorYPx)createPrismaticJoint
public PhysicsJoint createPrismaticJoint(PhysicsBody a, PhysicsBody b, float anchorXPx, float anchorYPx, float axisX, float axisY)b may only translate along the given pixel
axis relative to body a (e.g. a piston or an elevator).createMouseJoint
public PhysicsJoint createMouseJoint(PhysicsBody ground, PhysicsBody target, float targetXPx, float targetYPx, float maxForce)target toward a moving pixel point with a capped
force – the basis for dragging a body with a finger. ground is any body
(typically a static one); update the point each frame with
PhysicsJoint#setTarget(float, float).destroyJoint
public void destroyJoint(PhysicsJoint joint)setDebugDrawFlags
public void setDebugDrawFlags(boolean shapes, boolean joints, boolean boundingBoxes)#debugDraw(com.codename1.ui.Graphics) renders. By default it
draws collision shapes and joints; bounding boxes are off.debugDraw
public void debugDraw(Graphics g)com.codename1.ui.Graphics, for debugging. Coordinates use the
same pixel scale and y-flip as the bodies, so the overlay lines up with the
sprites they drive. Call it from a component’s paint, onto an off-screen
com.codename1.ui.Image, or any other 2D drawing surface. See
#setDebugDrawFlags(boolean, boolean, boolean) to choose what is drawn.setDebugFillAlpha
public void setDebugFillAlpha(int alpha)#debugDraw(com.codename1.ui.Graphics); shape outlines stay opaque. The
default (90) is a light translucent fill.