Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (757)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (844)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Box2DMapObjectParser: edit Box2D with your map editor  (Read 24978 times)
0 Members and 1 Guest are viewing this topic.
Offline dermetfan
« Posted 2013-06-20 01:28:00 »

This looks like an advertisement.

Hello everyone Smiley

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.
<a href=";hl=en_US&amp;start=" target="_blank">;hl=en_US&amp;start=</a>

Mini tutorial:
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.
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:

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
// get an ObjectMap of the parsed fixtures
// get an ObjectMap of the parsed joints
// 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 Smiley

- 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)

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.

Offline xelost

Innocent Bystander

« Reply #1 - Posted 2013-09-16 03:53:31 »

yesterday I was using this class and it works perfectly, great job! for the next game I will use it  Smiley
Offline Jimmt
« League of Dukes »

JGO Kernel

Medals: 167
Projects: 5
Exp: 6 years

« Reply #2 - Posted 2013-09-16 04:18:21 »

Looks very useful, I've always anticipated needing something like this and predicted I would have to make one myself.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline matheus23

JGO Kernel

Medals: 138
Projects: 3

You think about my Avatar right now!

« Reply #3 - Posted 2013-09-16 15:31:18 »

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.

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline dermetfan
« Reply #4 - Posted 2013-09-16 18:54:10 »

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.

Offline ShuKen

Senior Devvie

Medals: 5
Projects: 1

« Reply #5 - Posted 2013-09-17 20:22:27 »

Amazing! This should be part of gdx!
Offline grevius
« Reply #6 - Posted 2013-10-07 16:46:56 »

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
Offline dermetfan
« Reply #7 - Posted 2013-10-07 16:53:08 »

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.

Pages: [1]
  ignore  |  Print  

EgonOlsen (74 views)
2018-06-10 19:43:48

EgonOlsen (55 views)
2018-06-10 19:43:44

EgonOlsen (75 views)
2018-06-10 19:43:20

DesertCoockie (254 views)
2018-05-13 18:23:11

nelsongames (154 views)
2018-04-24 18:15:36

nelsongames (154 views)
2018-04-24 18:14:32

ivj94 (895 views)
2018-03-24 14:47:39

ivj94 (156 views)
2018-03-24 14:46:31

ivj94 (808 views)
2018-03-24 14:43:53

Solater (172 views)
2018-03-17 05:04:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!