Chain Shape vs. Polygon boxes

Hi all (said the newbie). I've got a small question regarding Box2D performance. A short primer first:

I have a level map consisting of square blocks (all the same size). They're arranged into an array, so I need to create a body out of them. They are going to make up the static objects of a level, i.e. where the player object can walk over and can't pass through.

I see two ways to approach this:

  1. Create a ground body which has all the individual blocks as fixtures.
  2. Create a ground body which contains a number of chain edges as fixtures, in which groups adjacent blocks are placed inside a single closed chain edge. Since groups of adjacent blocks do not necessarily make up squares, a chain edge is not always a rectangle.

Needless to say, having an array to work with the first solution is massively more easy for me because I can just create fixtures at the right places. For the second I will have to trace contours in the array and make sure I don't miss anything. I really only want to go there if I need to.

The question is this: which is the most efficient in terms of performance? It's fairly important to me as this uses a Javascript port of Box2D.

Thank you for your time!
(don't quote me on this, but) I think the performance would be quite similar, with edges having a slight advantage, especially if you make sensible optimizations like merging a long continuous floor or wall into one edge instead of simply using the original box edges.

Probably a bigger problem if you have a player character that should slide along the ground, will be the 'ghost collisions' caused by the boxes method which are not a problem with the edges method. Search the forums and manual for that term.
Thank you for your answer!

irresistible force wrote:
(don't quote me on this, but) I think the performance would be quite similar, with edges having a slight advantage, especially if you make sensible optimizations like merging a long continuous floor or wall into one edge instead of simply using the original box edges.

Yes, assuming I was going to go with edges, that's exactly what I was planning to do. But I was hoping to be able to skip the contour tracing involved.

Contour tracing is actually rather simple with regular edges (they'd just be a bunch of horizontal and vertical lines), but more difficult with chain edges. Would there be any difference in engine performance between the two when used for collisions? Assuming one chain in a fixture can make up several edges, while I need to put regular edges in fixtures of their own.

Come to think of it, I read somewhere in the manual that for polygons, only convex polygons are supported. This doesn't seem to count for chain edges, so I am half guessing that because of that added complexity, collision detection would also be more complex.

Quote:
Probably a bigger problem if you have a player character that should slide along the ground, will be the 'ghost collisions' caused by the boxes method which are not a problem with the edges method. Search the forums and manual for that term.

Yes, I read about it, but I thought the manual said this is an edge-specific problem (unless you use ghost vertices).

I'm still unsure what way to go - polygon boxes (of various sizes so I can reduce the number of bodies), edges, or chained edges. I'm not even sure merging smaller boxes into larger ones or several edges into a chained edge will actually speed up the physics engine - I don't know how Box2D works internally, but some kind of broad phase collision detection seems to be involved. Doesn't this mean that using larger shapes actually means they'll be evaluated more often in collision detection because they're "in the way" more often?
irresistible force wrote:
Probably a bigger problem if you have a player character that should slide along the ground, will be the 'ghost collisions' caused by the boxes method which are not a problem with the edges method. Search the forums and manual for that term.

I'm a little bit alarmed by this, because as far as I've read about character movement, everything in a platform game that walks really does slide along the ground (since gravity is always into effect).

This isn't a problem if you've created large polygon boxes or edges to walk along. However, I'm working on a game (I'm in the process of implementing Box2D to help me out with the nasty physics bits) where a character can push boxes around and can also walk on top of those boxes. Such boxes would obviously use a polygon box shape. If a player pushes two of those boxes close together, and then attempts to walk on top of them, would this according to you cause the ghost collisions? If so, how can we avoid them?
Yes it would, but I don't think it would cause the same wtf?! reaction for the player since at least they can see the boxes as separate objects and it makes sense. If you really needed to take care of it for movable boxes, I think you would need to do something like this when you detect that a box has a neighbor that should be treated as a continuous surface.
http://box2d.org/forum/viewtopic.php?f=8&t=7917
These could be extra fixtures on the box body that get their collision bits turned off and on at the right time, but if you also need to cater for all orientations of the box it could be quite a bit of work.

edit: I suppose it could also be a static fixture that just gets positioned at the right place, that would be easier 🙂
No, the game is quite simple and squarish, I'm not going to allow the boxes to rotate. I was, however, hoping that boxes placed together would automatically be a continuous surface. Not just because a player walks on it, but also because he should be able to push other boxes over such a surface, preferably without a speed bump. Since there may be a number of boxes, moving or dropping a box in place will have me write code that checks for edges being enlarged, connected or destroyed for every time a box moves. It's feasible, but the less JS has to do, the better.

I'm half wondering if using Box2D is a good solution for the platformer I'm making - the one thing I needed it for is making sure that multiple boxes stacked on top of a moving platform are dragged along correctly, but I'm now considering simply not allowing boxes to stand on platforms. I also thought Box2D would make stacking easier, but having experimented with it I noticed there is always a bit of teeter in stacked boxes. It just doesn't look right in what is supposed to be a retro-style game.

Regardless, thank you for your answer, it's much appreciated.
I've also seen examples of platformers that use a circle with fixed rotation or a motor as the locomotion (ie instead of a square or a point at the feet) this would help traverse these ghost collisions like a wheel would)

also correct me if I'm wrong but setting a bodies density to zero prevents it from rotating (ie teetering) i think. or you could use fixed rotation.

PS I've never done it, Kind of a newb myself but its some food for thought
Deftwun wrote:
I've also seen examples of platformers that use a circle with fixed rotation or a motor as the locomotion (ie instead of a square or a point at the feet) this would help traverse these ghost collisions like a wheel would)

Interesting approach. May be worth an attempt (though not in this game).

Quote:
also correct me if I'm wrong but setting a bodies density to zero prevents it from rotating (ie teetering) i think. or you could use fixed rotation.

Ah, this might just be my English, but when I say 'teetering' I meant 'slightly shifting position while in rest'. I was already using fixed rotations. Not sure if zero density works, but I actually would want them to have some weight...
If smooth character movement is important, I recommend using one or more circles for your character. The edge chains are mainly meant to solve the problem of the player pushing a box smoothly across the ground.
I released a game that was very sensitive to smoothness in the edges. I had overwhelmingly better performance using the edges and the edges stopped the intermittent tunneling.

Original thread: https://box2d.org/forum/viewtopic.php?t=8104