Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (575)
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 8945 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="http://www.youtube.com/v/EXiSpt-dLKg?version=3&amp;hl=en_US&amp;start=" target="_blank">http://www.youtube.com/v/EXiSpt-dLKg?version=3&amp;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:
1  
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:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
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 Smiley

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.


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: 136
Projects: 4
Exp: 3 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: 109
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
« 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  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (35 views)
2014-10-17 03:59:02

Norakomi (25 views)
2014-10-16 15:22:06

Norakomi (24 views)
2014-10-16 15:20:20

lcass (26 views)
2014-10-15 16:18:58

TehJavaDev (50 views)
2014-10-14 00:39:48

TehJavaDev (50 views)
2014-10-14 00:35:47

TehJavaDev (40 views)
2014-10-14 00:32:37

BurntPizza (63 views)
2014-10-11 23:24:42

BurntPizza (36 views)
2014-10-11 23:10:45

BurntPizza (75 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org 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‑gaming.org
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!