Class PhysicsBody

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

public class PhysicsBody extends Object

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 Type
    Method
    Description
    void
    applyForce(float fxPx, float fyPx)
    Applies a continuous force (in pixel based units, y down) at the body center.
    void
    applyForce(float fxPx, float fyPx, float worldXPx, float worldYPx)
    Applies a continuous force at a world point (pixels, y down).
    void
    applyLinearImpulse(float ixPx, float iyPx)
    Applies an instantaneous impulse (pixel based units, y down) at the body center.
    void
    applyTorque(float torque)
    Applies an angular impulse / torque (clockwise positive).
    float
     
    float
     
    float
     
     
    Returns the underlying shaded Box2D body (meters, y up) for advanced use.
    float
    The body rotation in radians (clockwise positive, screen space).
     
    float
    The body center x position in pixels.
    float
    The body center y position in pixels (screen space, y down).
    void
    setAngularDamping(float damping)
     
    void
    setAngularVelocity(float radiansPerSecond)
    Sets the angular velocity in radians per second (clockwise positive).
    void
    setBullet(boolean bullet)
    Enables continuous collision detection, preventing fast bodies from tunneling through thin walls.
    void
    setDensity(float density)
     
    void
    setFixedRotation(boolean fixed)
     
    void
    setFriction(float friction)
     
    void
    setLinearDamping(float damping)
     
    void
    setLinearVelocity(float vxPx, float vyPx)
    Sets the linear velocity in pixels per second (y down).
    void
    Links this body to an object the simulation drives -- typically a com.codename1.gaming.Sprite.
    void
    setRestitution(float restitution)
     
    void
    setSensor(boolean sensor)
    Makes the body a sensor: it detects contacts (via ContactListener) but does not produce a collision response.
    void
    setTransform(float xPx, float yPx, float rotationRadians)
    Teleports the body to the given pixel position and rotation (radians).
    void
    setUserData(Object userData)
     

    Methods inherited from class Object

    clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • setLinkedSprite

      public void setLinkedSprite(Object sprite)
      Links this body to an object the simulation drives -- typically a com.codename1.gaming.Sprite. If the object implements PhysicsLinkable its position and rotation are updated every step.
    • getLinkedSprite

      public Object 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 (via ContactListener) but does not produce a collision response.
    • getUserData

      public Object getUserData()
    • setUserData

      public void setUserData(Object userData)
    • getNativeBody

      public Body getNativeBody()
      Returns the underlying shaded Box2D body (meters, y up) for advanced use.