Simplifying building bodies and joints with libGDX Box2D

In almost all of our latest games we are using libGDX as our main game library. As it comes with a wrapper of well known physics library Box2D, we are using it as well.

Some times when creating a Box2D body, you have to initialize a lot of stuff, you have to create a BodyDef and a FixtureDef and then create the Body with the BodyDef, after that create the fixture on the body using the FixtureDef, it could be a bit confuse.

The next code snippet shows an example of that:

	BodyDef bodyDef = new BodyDef();
	FixtureDef fixtureDef = new FixtureDef();

	bodyDef.type = BodyType.StaticBody;
	bodyDef.bullet = false;
	// ... more stuff

	Shape shape = new CircleShape();
	shape.setRadius(radius);

	fixtureDef.shape = shape
	fixtureDef.friction = 1f;
	// ... more fixtureDef stuff

	Body body = world.createBody(bodyDef);
	body.createFixture(fixtureDef);

To improve this a bit, I have created a BodyBuilder which lets you build Box2D physics bodies in less code lines.

The following code snippet shows an example of using the BodyBuilder:

	Body body = bodyBuilder
			.mass(1000f)
			.circleShape(radius * 0.1f)
			.position(x, y)
			.restitution(0f)
			.type(BodyType.StaticBody)
			.categoryBits(MiniPlanetCategoryBits)
			.build();

As you can see, it looks smaller and cleaner. However, it has the limitation (because I was lazy when I did the class) it works for only one fixture def, if you want to build a complex body that will be a problem.

There is also a similar builder for Joints named JointBuilder but it is just started.

The following code snippet shows an example of using the JointBuilder:

	Joint joint = jointBuilder.distanceJoint()
			.bodyA(bodyA)
			.bodyB(bodyB)
			.collideConnected(false)
			.length(1.5f)
			.build();

If you are using libGDX Box2D as well, both classes could be of help despite they are incomplete.

VN:F [1.9.11_1134]
Rating: 5.0/5 (4 votes cast)
Simplifying building bodies and joints with libGDX Box2D, 5.0 out of 5 based on 4 ratings

Tags: , , ,

  • http://blog.gemserk.com/2011/07/12/simplifying-building-bodies-and-joints-with-libgdx-box2d-2/ Simplifying building bodies and joints with libGDX Box2D – 2 « Gemserk

    [...] which I commented on a previous post, has been updated to work with multiple fixtures, keeping [...]

  • Mark Dunne

    Hey thanks man, I’ve just started learning Box2D and this speeded things up a bit.

  • Becske

    Hi, great work.
    Can’t see how works the restitution.

  • arielsan

    Hi, I am not totally sure if you don’t know how restitution works in Box2d or in our builder. For the first case, the box2d manual explains it very well.