B2World

From Box2D Wiki

b2World
Box2D/Dynamics
Visibility Public
Manual reference [1]
API reference [2]
Source reference H.png Cpp.png

b2World is the the main object to deal with the Box2d engine. It stores all the joints and bodies, handles listeners and is responsible for stepping through the simulation.

It also provides b2World.Query, for querying the internal broadphase. Its constructor allows you to set the gravity and the world AABB.

Contents

b2World.CreateBody

Creates a b2Body from a b2BodyDef.

Note: API reference is outdated. CreateStaticBody/CreateDynamicBody are now both CreateBody.

b2World.DestroyBody

Destroys a b2Body. Be sure to not delete a body in a callback from the Step function.

API Reference [3]

b2World.CreateJoint

Creates a b2Joint from a b2JointDef.

API Reference [4]

b2World.DestroyJoint

Destroys a b2Joint. Be sure to not delete a joint in a callback from the Step function.

API Reference [5]

b2World.Step

This performs collision detection, integration, and constraint solution. It should be called when you want Box2D to continue updating your world.

API Reference [6]

b2World.Query

(Python users, see here)

This code snippet comes from Examples/Testbed/Framework/Test.cpp, and demonstrates how to query the world broadphase to see if a point is inside any shape. In the testbed, this is used to attach the mouse joint to dragged bodies.

b2Vec2 p; //The world location you want to test
 
// Make a small box.
b2AABB aabb;
b2Vec2 d;
d.Set(0.001f, 0.001f);
aabb.lowerBound = p - d;
aabb.upperBound = p + d;
 
// Query the world for overlapping shapes.
const int32 k_maxCount = 10; // max number of overlapping shapes to return
b2Shape* shapes[k_maxCount];
int32 count = m_world->Query(aabb, shapes, k_maxCount);
 
// At this point, the shapes array contains all shapes with
// b2AABB overlaps with the given point, but we still need
// to verify that there is an actual shape overlap.
 
b2Body* body = NULL;
for (int32 i = 0; i < count; ++i)
{
	b2Body* shapeBody = shapes[i]->GetBody();
 
	// Omit this check if you also want to check for static bodies
	if (shapeBody->IsStatic() == false && shapeBody->GetMass() > 0.0f)
	{
		bool inside = shapes[i]->TestPoint(shapeBody->GetXForm(), p);
		if (inside)
		{
			// shapeBody now has a pointer to the hit body,
			// and we have a hit - do something with it!
		}
	}
}

Other functions

There are many other functions available in b2World, see the API reference [7] for more.


TOOLBOX
LANGUAGES