Simplifying building bodies and joints with libGDX Box2D - 2

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

Internally, it uses a FixtureDef builder named FixtureDefBuilder which lets you specify a fixture definition for each fixture.

Here is an example of how it looks now it supports multiple fixture definitions:

Body body = bodyBuilder 
		.fixture(bodyBuilder.fixtureDefBuilder() 
				.circleShape(radius * 0.1f) 
				.categoryBits(CategoryBits.MiniPlanetCategoryBits) 
				.restitution(0f)) 
		.fixture(bodyBuilder.fixtureDefBuilder() 
				.circleShape(radius) 
				.categoryBits(CategoryBits.AllCategoryBits) 
				.sensor()) 
		.position(x, y) 
		.mass(1f) 
		.type(BodyType.StaticBody) 
		.userData(e) 
		.build();

The previous example shows how to declare two fixtures for a Body, one of them is a sensor. For you to know, I am using that code in Super Flying Thing to declare the destination planet (that's the name for now), the sensor is to detect when the ship is near to trigger an event and then attach it to the planet by creating a Box2D Joint.

If you are a game programmer, it could be useful to maintain your code clean and simple.

VN:F [1.9.22_1171]
Rating: 4.2/5 (6 votes cast)
Simplifying building bodies and joints with libGDX Box2D - 2, 4.2 out of 5 based on 6 ratings

Tags: , , ,

  • drorasta

    Hi there, nice classes, you should dispose all shapes in your FixtureDefBuilder class after finishing using them (-;

  • Ariel Coppes

    Well, the idea is to use it with BodyBuilder which disposes all the fixtureDef shapes on method reset() (which is called internally on build).

  • drorasta

    ah ok, just missed it (-; thanks