This looks like an advertisement.
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
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
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
. To add Fixtures
to a Body
, add a Aliases.body
property with the same value to all of this Body
Since it's been requested two times, here's a video
about how to use it.http://www.youtube.com/v/EXiSpt-dLKg?version=3&hl=en_US&start=
At first, let's create a Body
Note that this is a very basic example; you could use DynamicBody
and so on.
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.
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.
To create a Joint
, just create any map object and put all the values in the properties:
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
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
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();
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 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)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);
This is part of libgdx-utils