Quick Links: Download Gideros Studio | Gideros Documentation | Ultimate Guide | Gideros Development Center
TileMap to Box2D - Gideros Forum

TileMap to Box2D

totebototebo Member
edited December 2 in General questions
I'm trying to work out a good way to convert platforms imported from Tiled to collision fixtures in Box2D. The lazy option is to make a square fixture for each tile. This would be quite inefficient. @hgy29 had a great suggestion:

"Make a two tables of all tile edges: one for horizontal edges and the other for vertical ones. Then go through your wall tiles and increment the correct locations in your two arrays for the four edges of your tiles. In the end you'll get odd numbers for outlines and even numbers for edges not part of the outline"

This would work in most cases, but not in some, like the example below.

image

Box2D doesn't seem to allow the below boxes as one shape, because the vertices in between the two shapes have no width/height.

Do you have an idea of how to solve this?

Comments

  • antixantix Member
    edited December 3
    @totebo my first thought would be to add a 1 pixel border around the entire thing which would then make there be a space in there :)

    In theory you could also use a chain shape which should also work. I'm not sure what is faster.

    I suppose it depends on performance as well. If you need every CPU cycle then you will need to optimize it a lot but going from 6 boxes to 2 is a pretty good saving already ;)

    You will also need to think about how large your levels will be and how many physics objects you will be creating, the more you create the more CPU cycles used I guess. Sleeping might alleviate this though.

    Oh.. are you procedurally generating these maps?
    +1 -1
  • Chain shapes, that could work really well actually! May be a bit slower, but probably not much in it. I think sleeping would only affect dynamic bodies, these ones would be static.

    I'll load each Tiled "chunk" procedurally, so the Tiles content itself is hard coded.
    My Gideros games: www.totebo.com
    +1 -1
  • antixantix Member
    Okay that sounds good. I think Chains would be faster because you are just performing segment vs polygon checks instead of polygon vs polygon? I don't really know about such stuff myself but it sounds logical in my own tiny mind ;)

    So if the chunk is hardcoded.. why not just create an object layer in tiled and parse that as your box2d objects??
    +1 -1
  • That's a good point, actually. Each chunk would take longer to make i suppose. But good fallback if the dynamic chain solution fails. Will report back!
    My Gideros games: www.totebo.com
    +1 -1
  • So, I think I've solved this, at least in theory. The thing I can't seem to figure out, though, is how to separate each individual shape to be drawn with chain shapes or polys.

    In the example above, the left shape would need to be separated from the single tile.

    Any ideas?
    My Gideros games: www.totebo.com
    +1 -1
  • totebototebo Member
    edited December 4
    My quite convoluted plan is as follows:

    1. Go through each tile to determine which type it is (top left corner, top edge, top right corner etc)
    2. Start with an edge, find the next edge tile and follow the shape around until the first tile is found and close the shape.
    3. Start with a new edge shape that hasn't been previously checked and repeat.
    My Gideros games: www.totebo.com
    +1 -1
  • Success! The above method paid off and now works as it should.
    My Gideros games: www.totebo.com
    +1 -1
  • antixantix Member
    edited December 5
    Really? Great going! I was thinking of my CollisionMaker class which makes horizontal strips that could be turned into rectangles. Your new method sounds a lot better though.. I'll totally have to give that a go :)
    +1 -1
  • It will be handy for sure, having Tiled objects and tiles playing nicely with Box2D, as well as using TileMap. Now let's make a game!

    Aye: antix

    My Gideros games: www.totebo.com
    +1 -1 (+1 / -0 )
  • Do you have a marker to say what type of tile it is, or does your code auto figure it out or do you just look for specific block numbers?
    +1 -1
  • The code figures it out and assigns it to one of the known types (quite a few types!). I figure it out by checking up, right, down and left for each tile. This feels a bit brute force, but works well since I do it once per map.
    My Gideros games: www.totebo.com
    +1 -1 (+2 / -0 )
  • antixantix Member
    Does your code make irregular shapes or just rectangles?
    +1 -1
  • Irregular shapes. It's a miracle I was able to pull it off, no idea where that came from.

    Aye: antix

    My Gideros games: www.totebo.com
    +1 -1 (+1 / -0 )
Sign In or Register to comment.