libgdx API

com.badlogic.gdx.physics.box2d
Class World

java.lang.Object
  extended by com.badlogic.gdx.physics.box2d.World
All Implemented Interfaces:
Disposable

public final class World
extends java.lang.Object
implements Disposable

The world class manages all physics entities, dynamic simulation, and asynchronous queries. The world also contains efficient memory management facilities.

Author:
mzechner

Field Summary
protected  LongMap<Body> bodies
          all known bodies
protected  ContactFilter contactFilter
          Contact filter
protected  ContactListener contactListener
          Contact listener
protected  LongMap<Fixture> fixtures
          all known fixtures
protected  Pool<Body> freeBodies
          pool for bodies
protected  Pool<Fixture> freeFixtures
          pool for fixtures
protected  LongMap<Joint> joints
          all known joints
 
Constructor Summary
World(Vector2 gravity, boolean doSleep)
          Construct a world object.
 
Method Summary
 void clearForces()
          Manually clear the force buffer on all bodies.
 Body createBody(BodyDef def)
          Create a rigid body given a definition.
 Joint createJoint(JointDef def)
          Create a joint to constrain bodies together.
 void destroyBody(Body body)
          Destroy a rigid body given a definition.
 void destroyJoint(Joint joint)
          Destroy a joint.
 void dispose()
          Releases all resources of this object.
 boolean getAutoClearForces()
          Get the flag that controls automatic clearing of forces after each time step.
 java.util.Iterator<Body> getBodies()
           
 int getBodyCount()
          Get the number of bodies.
 int getContactCount()
          Get the number of contacts (each may have 0 or more contact points).
 java.util.List<Contact> getContactList()
          Returns the list of Contact instances produced by the last call to step(float, int, int).
 Vector2 getGravity()
           
 int getJointCount()
          Get the number of joints.
 java.util.Iterator<Joint> getJoints()
           
 int getProxyCount()
          Get the number of broad-phase proxies.
static float getVelocityThreshold()
           
 boolean isLocked()
          Is the world locked (in the middle of a time step).
 void QueryAABB(QueryCallback callback, float lowerX, float lowerY, float upperX, float upperY)
          Query the world for all fixtures that potentially overlap the provided AABB.
 void rayCast(RayCastCallback callback, Vector2 point1, Vector2 point2)
          Ray-cast the world for all fixtures in the path of the ray.
 void setAutoClearForces(boolean flag)
          Set flag to control automatic clearing of forces after each time step.
 void setContactFilter(ContactFilter filter)
          Register a contact filter to provide specific control over collision.
 void setContactListener(ContactListener listener)
          Register a contact event listener.
 void setContinuousPhysics(boolean flag)
          Enable/disable continuous physics.
 void setDestructionListener(DestructionListener listener)
          Register a destruction listener.
 void setGravity(Vector2 gravity)
          Change the global gravity vector.
static void setVelocityThreshold(float threshold)
          Sets the box2d velocity threshold globally, for all World instances.
 void setWarmStarting(boolean flag)
          Enable/disable warm starting.
 void step(float timeStep, int velocityIterations, int positionIterations)
          Take a time step.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

freeBodies

protected final Pool<Body> freeBodies
pool for bodies


freeFixtures

protected final Pool<Fixture> freeFixtures
pool for fixtures


bodies

protected final LongMap<Body> bodies
all known bodies


fixtures

protected final LongMap<Fixture> fixtures
all known fixtures


joints

protected final LongMap<Joint> joints
all known joints


contactFilter

protected ContactFilter contactFilter
Contact filter


contactListener

protected ContactListener contactListener
Contact listener

Constructor Detail

World

public World(Vector2 gravity,
             boolean doSleep)
Construct a world object.

Parameters:
gravity - the world gravity vector.
doSleep - improve performance by not simulating inactive bodies.
Method Detail

rayCast

public void rayCast(RayCastCallback callback,
                    Vector2 point1,
                    Vector2 point2)
Ray-cast the world for all fixtures in the path of the ray. The ray-cast ignores shapes that contain the starting point.

Parameters:
callback - a user implemented callback class.
point1 - the ray starting point
point2 - the ray ending point

setDestructionListener

public void setDestructionListener(DestructionListener listener)
Register a destruction listener. The listener is owned by you and must remain in scope.


setContactFilter

public void setContactFilter(ContactFilter filter)
Register a contact filter to provide specific control over collision. Otherwise the default filter is used (b2_defaultFilter). The listener is owned by you and must remain in scope.


setContactListener

public void setContactListener(ContactListener listener)
Register a contact event listener. The listener is owned by you and must remain in scope.


createBody

public Body createBody(BodyDef def)
Create a rigid body given a definition. No reference to the definition is retained.


destroyBody

public void destroyBody(Body body)
Destroy a rigid body given a definition. No reference to the definition is retained. This function is locked during callbacks.


createJoint

public Joint createJoint(JointDef def)
Create a joint to constrain bodies together. No reference to the definition is retained. This may cause the connected bodies to cease colliding.


destroyJoint

public void destroyJoint(Joint joint)
Destroy a joint. This may cause the connected bodies to begin colliding.


step

public void step(float timeStep,
                 int velocityIterations,
                 int positionIterations)
Take a time step. This performs collision detection, integration, and constraint solution.

Parameters:
timeStep - the amount of time to simulate, this should not vary.
velocityIterations - for the velocity constraint solver.
positionIterations - for the position constraint solver.

clearForces

public void clearForces()
Manually clear the force buffer on all bodies. By default, forces are cleared automatically after each call to Step. The default behavior is modified by calling SetAutoClearForces. The purpose of this function is to support sub-stepping. Sub-stepping is often used to maintain a fixed sized time step under a variable frame-rate. When you perform sub-stepping you will disable auto clearing of forces and instead call ClearForces after all sub-steps are complete in one pass of your game loop. setAutoClearForces(boolean)


setWarmStarting

public void setWarmStarting(boolean flag)
Enable/disable warm starting. For testing.


setContinuousPhysics

public void setContinuousPhysics(boolean flag)
Enable/disable continuous physics. For testing.


getProxyCount

public int getProxyCount()
Get the number of broad-phase proxies.


getBodyCount

public int getBodyCount()
Get the number of bodies.


getJointCount

public int getJointCount()
Get the number of joints.


getContactCount

public int getContactCount()
Get the number of contacts (each may have 0 or more contact points).


setGravity

public void setGravity(Vector2 gravity)
Change the global gravity vector.


getGravity

public Vector2 getGravity()

isLocked

public boolean isLocked()
Is the world locked (in the middle of a time step).


setAutoClearForces

public void setAutoClearForces(boolean flag)
Set flag to control automatic clearing of forces after each time step.


getAutoClearForces

public boolean getAutoClearForces()
Get the flag that controls automatic clearing of forces after each time step.


QueryAABB

public void QueryAABB(QueryCallback callback,
                      float lowerX,
                      float lowerY,
                      float upperX,
                      float upperY)
Query the world for all fixtures that potentially overlap the provided AABB.

Parameters:
callback - a user implemented callback class.
lowerX - the x coordinate of the lower left corner
lowerY - the y coordinate of the lower left corner
upperX - the x coordinate of the upper right corner
upperY - the y coordinate of the upper right corner

getContactList

public java.util.List<Contact> getContactList()
Returns the list of Contact instances produced by the last call to step(float, int, int). Note that the returned list will have O(1) access times when using indexing. contacts are created and destroyed in the middle of a time step. Use ContactListener to avoid missing contacts

Returns:
the contact list

getBodies

public java.util.Iterator<Body> getBodies()
Returns:
all bodies currently in the simulation

getJoints

public java.util.Iterator<Joint> getJoints()
Returns:
all joints currently in the simulation

dispose

public void dispose()
Description copied from interface: Disposable
Releases all resources of this object.

Specified by:
dispose in interface Disposable

setVelocityThreshold

public static void setVelocityThreshold(float threshold)
Sets the box2d velocity threshold globally, for all World instances.

Parameters:
threshold - the threshold, default 1.0f

getVelocityThreshold

public static float getVelocityThreshold()
Returns:
the global box2d velocity threshold.

libgdx API

Copyright 2010 Mario Zechner (contact@badlogicgames.com), Nathan Sweet (admin@esotericsoftware.com)