Class Body

java.lang.Object
org.jbox2d.dynamics.Body

public class Body extends Object
A rigid body. These are created via World.createBody.
Author:
Daniel Murphy
Repolink:
Github: erincatto/box2d src/dynamics/b2_body.cpp
Github: erincatto/box2d include/box2d/b2_body.h
  • Field Details

  • Constructor Details

  • Method Details

    • createFixture

      public final Fixture createFixture(FixtureDef def)
      Creates a fixture and attach it to this body. Use this function if you need to set some fixture parameters, like friction. Otherwise, you can create the fixture directly from a shape. If the density is non-zero, this function automatically updates the mass of the body. Contacts are not created until the next time step.
      Parameters:
      def - The fixture definition.
      Repolink:
      Github: erincatto/box2d src/dynamics/b2_body.cpp Lines 204 - 211
      Warning:
      This function is locked during callbacks.
    • createFixture

      public final Fixture createFixture(Shape shape, float density)
      Creates a fixture from a shape and attach it to this body. This is a convenience function. Use FixtureDef if you need to set parameters like friction, restitution, user data, or filtering. If the density is non-zero, this function automatically updates the mass of the body.
      Parameters:
      shape - The shape to be cloned.
      density - The shape density (set to zero for static bodies).
      Repolink:
      Github: erincatto/box2d src/dynamics/b2_body.cpp Lines 204 - 211
      Warning:
      This function is locked during callbacks.
    • destroyFixture

      public final void destroyFixture(Fixture fixture)
      Destroy a fixture. This removes the fixture from the broad-phase and destroys all contacts associated with this fixture. This will automatically adjust the mass of the body if the body is dynamic and the fixture has positive density. All fixtures attached to a body are implicitly destroyed when the body is destroyed.
      Parameters:
      fixture - The fixture to be removed.
      Repolink:
      Github: erincatto/box2d src/dynamics/b2_body.cpp Lines 213 - 288
      Warning:
      This function is locked during callbacks.
    • setTransform

      public final void setTransform(Vec2 position, float angle)
      Set the position of the body's origin and rotation. This breaks any contacts and wakes the other bodies. Manipulating a body's transform may cause non-physical behavior. Note: contacts are updated on the next call to World.step().
      Parameters:
      position - The world position of the body's local origin.
      angle - The world rotation in radians.
      Repolink:
      Github: erincatto/box2d src/dynamics/b2_body.cpp Lines 420 - 445
    • getTransform

      public final Transform getTransform()
      Get the body transform for the body's origin.
      Returns:
      The world transform of the body's origin.
    • getPosition

      public final Vec2 getPosition()
      Get the world body origin position. Do not modify.
      Returns:
      The world position of the body's origin.
    • getAngle

      public final float getAngle()
      Get the angle in radians.
      Returns:
      The current world rotation angle in radians.
    • getWorldCenter

      public final Vec2 getWorldCenter()
      Get the world position of the center of mass. Do not modify.
    • getLocalCenter

      public final Vec2 getLocalCenter()
      Get the local position of the center of mass. Do not modify.
    • setLinearVelocity

      public final void setLinearVelocity(Vec2 v)
      Set the linear velocity of the center of mass.
      Parameters:
      v - The new linear velocity of the center of mass.
    • getLinearVelocity

      public final Vec2 getLinearVelocity()
      Get the linear velocity of the center of mass. Do not modify, instead use setLinearVelocity(Vec2).
      Returns:
      The linear velocity of the center of mass.
    • setAngularVelocity

      public final void setAngularVelocity(float w)
      Set the angular velocity.
      Parameters:
      w - The new angular velocity in radians/second.
    • getAngularVelocity

      public final float getAngularVelocity()
      Get the angular velocity.
      Returns:
      The angular velocity in radians/second.
    • getGravityScale

      public float getGravityScale()
      Get the gravity scale of the body.
      Repolink:
      Github: erincatto/box2d include/box2d/b2_body.h Lines 610 - 613
    • setGravityScale

      public void setGravityScale(float gravityScale)
      Set the gravity scale of the body.
      Repolink:
      Github: erincatto/box2d include/box2d/b2_body.h Lines 615 - 618
    • applyForce

      public final void applyForce(Vec2 force, Vec2 point)
      Apply a force at a world point. If the force is not applied at the center of mass, it will generate a torque and affect the angular velocity. This wakes up the body.
      Parameters:
      force - The world force vector, usually in Newtons (N).
      point - The world position of the point of application.
      Repolink:
      Github: erincatto/box2d include/box2d/b2_body.h Lines 743 - 761
    • applyForceToCenter

      public final void applyForceToCenter(Vec2 force)
      Apply a force to the center of mass. This wakes up the body.
      Parameters:
      force - The world force vector, usually in Newtons (N).
      Repolink:
      Github: erincatto/box2d include/box2d/b2_body.h Lines 763 - 780
    • applyTorque

      public final void applyTorque(float torque)
      Apply a torque. This affects the angular velocity without affecting the linear velocity of the center of mass. This wakes up the body.
      Parameters:
      torque - About the z-axis (out of the screen), usually in N-m.
      Repolink:
      Github: erincatto/box2d include/box2d/b2_body.h Lines 782 - 799
    • applyLinearImpulse

      public final void applyLinearImpulse(Vec2 impulse, Vec2 point, boolean wake)
      Apply an impulse at a point. This immediately modifies the velocity. It also modifies the angular velocity if the point of application is not at the center of mass. This wakes up the body if 'wake' is set to true. If the body is sleeping and 'wake' is false, then there is no effect.
      Parameters:
      impulse - The world impulse vector, usually in N-seconds or kg-m/s.
      point - The world position of the point of application.
      wake - Also wake up the body.
      Repolink:
      Github: erincatto/box2d include/box2d/b2_body.h Lines 801 - 819
    • applyAngularImpulse

      public void applyAngularImpulse(float impulse)
      Apply an angular impulse.
      Parameters:
      impulse - The angular impulse in units of kg*m*m/s
      Repolink:
      Github: erincatto/box2d include/box2d/b2_body.h Lines 840 - 857
    • getMass

      public final float getMass()
      Get the total mass of the body.
      Returns:
      The mass, usually in kilograms (kg).
    • getInertia

      public final float getInertia()
      Get the central rotational inertia of the body.
      Returns:
      The rotational inertia, usually in kg-m^2.
    • getMassData

      public final void getMassData(MassData data)
      Get the mass data of the body. The rotational inertia is relative to the center of mass.
    • setMassData

      public final void setMassData(MassData massData)
      Set the mass properties to override the mass properties of the fixtures. Note that this changes the center of mass position. Note that creating or destroying fixtures can also alter the mass. This function has no effect if the body isn't dynamic.
      Parameters:
      massData - The mass properties.
      Repolink:
      Github: erincatto/box2d src/dynamics/b2_body.cpp Lines 356 - 395
    • resetMassData

      public final void resetMassData()
      This resets the mass properties to the sum of the mass properties of the fixtures. This normally does not need to be called unless you called setMassData to override the mass, and you later want to reset the mass.
      Repolink:
      Github: erincatto/box2d src/dynamics/b2_body.cpp Lines 290 - 354
    • getWorldPoint

      public final Vec2 getWorldPoint(Vec2 localPoint)
      Get the world coordinates of a point given the local coordinates.
      Parameters:
      localPoint - A point on the body measured relative the body's origin.
      Returns:
      The same point expressed in world coordinates.
    • getWorldPointToOut

      public final void getWorldPointToOut(Vec2 localPoint, Vec2 out)
    • getWorldVector

      public final Vec2 getWorldVector(Vec2 localVector)
      Get the world coordinates of a vector given the local coordinates.
      Parameters:
      localVector - A vector fixed in the body.
      Returns:
      The same vector expressed in world coordinates.
    • getWorldVectorToOut

      public final void getWorldVectorToOut(Vec2 localVector, Vec2 out)
    • getWorldVectorToOutUnsafe

      public final void getWorldVectorToOutUnsafe(Vec2 localVector, Vec2 out)
    • getLocalPoint

      public final Vec2 getLocalPoint(Vec2 worldPoint)
      Gets a local point relative to the body's origin given a world point.
      Parameters:
      worldPoint - Point in world coordinates.
      Returns:
      The corresponding local point relative to the body's origin.
    • getLocalPointToOut

      public final void getLocalPointToOut(Vec2 worldPoint, Vec2 out)
    • getLocalVector

      public final Vec2 getLocalVector(Vec2 worldVector)
      Gets a local vector given a world vector.
      Parameters:
      worldVector - A vector in world coordinates.
      Returns:
      The corresponding local vector.
    • getLocalVectorToOut

      public final void getLocalVectorToOut(Vec2 worldVector, Vec2 out)
    • getLocalVectorToOutUnsafe

      public final void getLocalVectorToOutUnsafe(Vec2 worldVector, Vec2 out)
    • getLinearVelocityFromWorldPoint

      public final Vec2 getLinearVelocityFromWorldPoint(Vec2 worldPoint)
      Get the world linear velocity of a world point attached to this body.
      Parameters:
      worldPoint - A point in world coordinates.
      Returns:
      The world velocity of a point.
    • getLinearVelocityFromWorldPointToOut

      public final void getLinearVelocityFromWorldPointToOut(Vec2 worldPoint, Vec2 out)
    • getLinearVelocityFromLocalPoint

      public final Vec2 getLinearVelocityFromLocalPoint(Vec2 localPoint)
      Get the world velocity of a local point.
      Parameters:
      localPoint - A point in local coordinates.
      Returns:
      The world velocity of a point.
    • getLinearVelocityFromLocalPointToOut

      public final void getLinearVelocityFromLocalPointToOut(Vec2 localPoint, Vec2 out)
    • getLinearDamping

      public final float getLinearDamping()
      Get the linear damping of the body.
    • setLinearDamping

      public final void setLinearDamping(float linearDamping)
      Set the linear damping of the body.
    • getAngularDamping

      public final float getAngularDamping()
      Get the angular damping of the body.
    • setAngularDamping

      public final void setAngularDamping(float angularDamping)
      Set the angular damping of the body.
    • getType

      public BodyType getType()
    • setType

      public void setType(BodyType type)
      Set the type of this body. This may alter the mass and velocity.
    • isBullet

      public final boolean isBullet()
      Is this body treated like a bullet for continuous collision detection?
      Repolink:
      Github: erincatto/box2d include/box2d/b2_body.h Lines 632 - 635
    • setBullet

      public final void setBullet(boolean flag)
      Should this body be treated like a bullet for continuous collision detection?
      Repolink:
      Github: erincatto/box2d include/box2d/b2_body.h Lines 620 - 630
    • setSleepingAllowed

      public void setSleepingAllowed(boolean flag)
      You can disable sleeping on this body. If you disable sleeping, the body will be woken.
      Repolink:
      Github: erincatto/box2d include/box2d/b2_body.h Lines 675 - 686
    • isSleepingAllowed

      public boolean isSleepingAllowed()
      Is this body allowed to sleep?
      Repolink:
      Github: erincatto/box2d include/box2d/b2_body.h Lines 688 - 691
    • setAwake

      public void setAwake(boolean flag)
      Set the sleep state of the body. A sleeping body has very low CPU cost. Note that putting it to sleep will set its velocities and forces to zero.
      Parameters:
      flag - Set to true to wake the body, false to put it to sleep.
      Repolink:
      Github: erincatto/box2d include/box2d/b2_body.h Lines 637 - 658
    • isAwake

      public boolean isAwake()
      Get the sleeping state of this body.
      Returns:
      true if the body is awake.
      Repolink:
      Github: erincatto/box2d include/box2d/b2_body.h Lines 660 - 663
    • setActive

      public void setActive(boolean flag)
      Set the active state of the body. An inactive body is not simulated and cannot be collided with or woken up. If you pass a flag of true, all fixtures will be added to the broad-phase. If you pass a flag of false, all fixtures will be removed from the broad-phase and all contacts will be destroyed. Fixtures and joints are otherwise unaffected. You may continue to create/destroy fixtures and joints on inactive bodies. Fixtures on an inactive body are implicitly inactive and will not participate in collisions, ray-casts, or queries. Joints connected to an inactive body are implicitly inactive. An inactive body is still owned by a World object and remains in the body list.
      Repolink:
      Github: erincatto/box2d src/dynamics/b2_body.cpp Lines 471 - 515
    • isActive

      public boolean isActive()
      Get the active state of the body.
      Repolink:
      Github: erincatto/box2d include/box2d/b2_body.h Lines 665 - 668
    • setFixedRotation

      public void setFixedRotation(boolean flag)
      Set this body to have fixed rotation. This causes the mass to be reset.
      Repolink:
      Github: erincatto/box2d src/dynamics/b2_body.cpp Lines 517 - 537
    • isFixedRotation

      public boolean isFixedRotation()
      Does this body have fixed rotation?
      Repolink:
      Github: erincatto/box2d include/box2d/b2_body.h Lines 670 - 673
    • getFixtureList

      public final Fixture getFixtureList()
      Get the list of all fixtures attached to this body.
      Repolink:
      Github: erincatto/box2d include/box2d/b2_body.h Lines 693 - 701
    • getJointList

      public final JointEdge getJointList()
      Get the list of all joints attached to this body.
      Repolink:
      Github: erincatto/box2d include/box2d/b2_body.h Lines 703 - 711
    • getContactList

      public final ContactEdge getContactList()
      Get the list of all contacts attached to this body.
      Repolink:
      Github: erincatto/box2d include/box2d/b2_body.h Lines 713 - 721
      Warning:
      this list changes during the time step, and you may miss some collisions if you don't use ContactListener.
    • getNext

      public final Body getNext()
      Get the next body in the world's body list.
      Repolink:
      Github: erincatto/box2d include/box2d/b2_body.h Lines 374 - 376
      Github: erincatto/box2d include/box2d/b2_body.h Lines 723 - 731
    • getUserData

      public final Object getUserData()
      Get the user data pointer that was provided in the body definition.
      Repolink:
      Github: erincatto/box2d include/box2d/b2_body.h Lines 733 - 741
    • setUserData

      public final void setUserData(Object data)
      Set the user data. Use this to store your application specific data.
    • getWorld

      public final World getWorld()
      Get the parent world of this body.
    • synchronizeFixtures

      protected final void synchronizeFixtures()
      Repolink:
      Github: erincatto/box2d src/dynamics/b2_body.cpp Lines 447 - 469
    • synchronizeTransform

      public final void synchronizeTransform()
      Repolink:
      Github: erincatto/box2d include/box2d/b2_body.h Lines 859 - 863
    • shouldCollide

      public boolean shouldCollide(Body other)
      This is used to prevent connected bodies from colliding. It may lie, depending on the collideConnected flag.
      Repolink:
      Github: erincatto/box2d src/dynamics/b2_body.cpp Lines 397 - 418
    • advance

      protected final void advance(float t)