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.22_1171]
Rating: 4.5/5 (8 votes cast)
Simplifying building bodies and joints with libGDX Box2D, 4.5 out of 5 based on 8 ratings

Tags: , , ,