Class PhysicsBody
java.lang.Object
com.codename1.gaming.physics.PhysicsBody
A rigid body in a PhysicsWorld, wrapping a shaded Box2D body.
Positions and velocities are expressed in pixels (with the screen y axis
pointing down); the wrapper converts to and from Box2D's meters/y-up internally.
A body may be linked to a PhysicsLinkable (typically a
com.codename1.gaming.Sprite) via #setLinkedSprite(Object); after each
PhysicsWorld#step(float) the body pushes its transform into that object so the
sprite follows the simulation.
-
Method Summary
Modifier and TypeMethodDescriptionvoidapplyForce(float fxPx, float fyPx) Applies a continuous force (in pixel based units, y down) at the body center.voidapplyForce(float fxPx, float fyPx, float worldXPx, float worldYPx) Applies a continuous force at a world point (pixels, y down).voidapplyLinearImpulse(float ixPx, float iyPx) Applies an instantaneous impulse (pixel based units, y down) at the body center.voidapplyTorque(float torque) Applies an angular impulse / torque (clockwise positive).floatfloatfloatReturns the underlying shaded Box2D body (meters, y up) for advanced use.floatThe body rotation in radians (clockwise positive, screen space).floatgetX()The body center x position in pixels.floatgetY()The body center y position in pixels (screen space, y down).voidsetAngularDamping(float damping) voidsetAngularVelocity(float radiansPerSecond) Sets the angular velocity in radians per second (clockwise positive).voidsetBullet(boolean bullet) Enables continuous collision detection, preventing fast bodies from tunneling through thin walls.voidsetDensity(float density) voidsetFixedRotation(boolean fixed) voidsetFriction(float friction) voidsetLinearDamping(float damping) voidsetLinearVelocity(float vxPx, float vyPx) Sets the linear velocity in pixels per second (y down).voidsetLinkedSprite(Object sprite) Links this body to an object the simulation drives -- typically acom.codename1.gaming.Sprite.voidsetRestitution(float restitution) voidsetSensor(boolean sensor) Makes the body a sensor: it detects contacts (viaContactListener) but does not produce a collision response.voidsetTransform(float xPx, float yPx, float rotationRadians) Teleports the body to the given pixel position and rotation (radians).voidsetUserData(Object userData)
-
Method Details
-
setLinkedSprite
Links this body to an object the simulation drives -- typically acom.codename1.gaming.Sprite. If the object implementsPhysicsLinkableits position and rotation are updated every step. -
getLinkedSprite
-
getX
public float getX()The body center x position in pixels. -
getY
public float getY()The body center y position in pixels (screen space, y down). -
getRotation
public float getRotation()The body rotation in radians (clockwise positive, screen space). -
setTransform
public void setTransform(float xPx, float yPx, float rotationRadians) Teleports the body to the given pixel position and rotation (radians). -
setLinearVelocity
public void setLinearVelocity(float vxPx, float vyPx) Sets the linear velocity in pixels per second (y down). -
getLinearVelocityX
public float getLinearVelocityX() -
getLinearVelocityY
public float getLinearVelocityY() -
setAngularVelocity
public void setAngularVelocity(float radiansPerSecond) Sets the angular velocity in radians per second (clockwise positive). -
getAngularVelocity
public float getAngularVelocity() -
applyForce
public void applyForce(float fxPx, float fyPx) Applies a continuous force (in pixel based units, y down) at the body center. -
applyForce
public void applyForce(float fxPx, float fyPx, float worldXPx, float worldYPx) Applies a continuous force at a world point (pixels, y down). -
applyLinearImpulse
public void applyLinearImpulse(float ixPx, float iyPx) Applies an instantaneous impulse (pixel based units, y down) at the body center. -
applyTorque
public void applyTorque(float torque) Applies an angular impulse / torque (clockwise positive). -
setFixedRotation
public void setFixedRotation(boolean fixed) -
setBullet
public void setBullet(boolean bullet) Enables continuous collision detection, preventing fast bodies from tunneling through thin walls. -
setLinearDamping
public void setLinearDamping(float damping) -
setAngularDamping
public void setAngularDamping(float damping) -
setDensity
public void setDensity(float density) -
setFriction
public void setFriction(float friction) -
setRestitution
public void setRestitution(float restitution) -
setSensor
public void setSensor(boolean sensor) Makes the body a sensor: it detects contacts (viaContactListener) but does not produce a collision response. -
getUserData
-
setUserData
-
getNativeBody
Returns the underlying shaded Box2D body (meters, y up) for advanced use.
-