Box2DMapObjectParser: edit Box2D with your map editor

This looks like an advertisement.

Hello everyone :slight_smile:

I spent some time writing a Box2DMapObjectParser and thought it may be useful for you. It’s a class that allows you to very easily create Box2D Bodies, Fixtures and Joints in any map editor LibGDX supports (tested with Tiled) and load them in one line of code.

In your object layer, right-click an object and set its properties to those of the Body / Fixture / Joint you’d like. You can use custom keys for the properties by using a Aliases object (inner class of the parser holding some public Strings). For type, you have to choose Aliases.body or Aliases.fixture. To add Fixtures to a Body, add a Aliases.body property with the same value to all of this Body's Fixtures.


Since it’s been requested two times, here’s a video about how to use it.

EXiSpt-dLKg


Mini tutorial:
[tr][td]At first, let’s create a Body:

Note that this is a very basic example; you could use DynamicBody with angularVelocity, linearDamping, gravityScale and so on.[/td]

[td]Then, add a Fixture to the just created Body:

Note the property “body: environment”, which adds this fixture to the body created on the left.[/td][/tr]

[tr][td]If you just need one Fixture on a Body anyway, you can use the type Aliases.object:

This way, you can put all the fixture’s and body’s properties in and don’t need to create one map object for the body and one map object for the fixture.[/td]

[td]To create a Joint, just create any map object and put all the values in the properties:


[/td][/tr]


Unit Scale
What about the unit scale between the editor’s unit (tiles in this case) and Box2D meters?
You have to put a Aliases.unitScale property in the properties of the map or the object layer/s or set one using #setUnitScale(float).
If there’s Aliases.unitScale information in your map/layer properties that you’d like to ignore, use #setIgnoreMapUnitScale(boolean).
There’s also a constructor that allows to pass in a unit scale in and sets ignoreMapUnitScale to true.
This way, you can use different scaling for each layer or choose a scale in code.

loading the objects in code
The simplest way to load your map objects in your game is the following:

new Box2DMapObjectParser().load(world, map);

This will create all the Bodies / Fixtures / Joints in the given World that it can parse.
There are also some more constructors that allow you to pass in just what you need and/or an Aliases object.

You can also get the created objects later:

Box2DMapObjectParser parser = new Box2DMapObjectParser();
parser.load(world, map);

// get an ObjectMap of the parsed bodies
parser.getBodies();
// get an ObjectMap of the parsed fixtures
parser.getFixtures();
// get an ObjectMap of the parsed joints
parser.getJoints();
// the keys are the names you specified in the editor

I hope I explained everything in an understandable way. If you have any questions or improvements, I'm happy to hear about them.

download Box2DMapObjectParser in the library or get the source

Hopefully someone will find this useful :slight_smile:

[i]TODO:

  • add Joint support
  • add object type (to create a body and fixture in one map object)
  • add some nicer error messages than NullPointerExceptions if the map is corrupt
  • add CircleMapObject support
  • find a better name
  • add optional automatic scaling
  • add support for concave shapes (split into triangles)[/i]

TL;DR:
Edit objects in an object layer in your map editor, set their properties according to the Box2DMapObjectParser.Aliases fields, and load them using new Box2DMapObjectParser().load(world, map);.


[b]This is part of [url=http://dermetfan.bplaced.net/libgdx-utils.php]libgdx-utils[/url][/b].

yesterday I was using this class and it works perfectly, great job! for the next game I will use it :slight_smile:

Looks very useful, I’ve always anticipated needing something like this and predicted I would have to make one myself.

Nah, Better don’t decompose into triangles, It’s better to decompose into Convex polygons. I’ve found some C# code for a very good decomposer, and I’ve myself written something like that object parser you did there for my game Ruins of Revenge, just take a look at the ported decomposer and how to use it.

You now have the choice to triangulate or decompose into convex polygons. I’d have done it from the start, but didn’t know any good resource (also I didn’t search much since LibGDX’s triangulator worked fine, so I was happy). Decomposing into convex polygons is way cooler of course, so that’s what it does by default (change with setTriangulate(boolean)). Thanks for this!
I updated the downloads.

Amazing! This should be part of gdx!

I have a quastion, in your parser, are the tile cell connected with the tile image? if i draw a box and i set i dynamic, can i move the object and the tile too?
I don’t know if i’m clear or not, sorry XP

Tiles have nothing to do with Box2D bodies. Also, tiles cannot move around freely. To draw images on bodies, you can use the Box2DSprite (thread) or do it manually.