Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
TileMap to Box2D — Gideros Forum

TileMap to Box2D

totebototebo Member
edited December 2017 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?
tiles to box2d.png
1121 x 330 - 34K
My Gideros games: www.totebo.com

Comments

  • antixantix Member
    edited December 2017
    @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?
  • 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
  • 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??
  • 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
  • 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
  • totebototebo Member
    edited December 2017
    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
  • Success! The above method paid off and now works as it should.
    My Gideros games: www.totebo.com
  • antixantix Member
    edited December 2017
    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 :)
  • 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!

    Likes: antix

    My Gideros games: www.totebo.com
    +1 -1 (+1 / -0 )Share on Facebook
  • 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?
    Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
    https://deluxepixel.com
  • 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 )Share on Facebook
  • Does your code make irregular shapes or just rectangles?
  • Irregular shapes. It's a miracle I was able to pull it off, no idea where that came from.

    Likes: antix

    Dislikes: JuanZambrano

    My Gideros games: www.totebo.com
    +1 -1 (+1 / -1 )Share on Facebook
  • totebototebo Member
    edited December 2017
    I've now ended up with a scenario where I have too many vertices. How would I work out to remove the redundant vertices (like the image below)? It seems like it should be simple, but it's quite tricky indeed!


    My Gideros games: www.totebo.com
Sign In or Register to comment.