Skip to content Skip to sidebar Skip to footer

Multiple Fixtures On One Body Or Multiple Bodies?

Let's say I wanted to create 1000, or maybe even 5000 static body lines on the screen. What I am wondering is, what is the difference between attaching all of these lines (fixtures

Solution 1:

From the Box2D Manual (7.3 Body Factory):

It is faster to attach several shapes to a static body than to create several static bodies with a single shape on each one. Internally, Box2D sets the mass and inverse mass of static bodies to zero. This makes the math work out so that most algorithms don't need to treat static bodies as a special case.

It is indeed more performant. Is it noticeable? depends, if you have a lot of bodies with one fixture and you make them fixtures of one single body, but probably not for most games.

I would recommend you to use the one which is easier to implement, and only make it more performant when you really need it.

Solution 2:

Memory usage may also be something to consider.

At least in the C++ version of Box2D, each body takes up a fixed amount of memory just like each fixture does and each shape does. So associating every fixture with its own body will use up more memory than creating multiple fixtures within a single body. The same number of fixtures and shapes will be used in either case, but more bodies are used in the former case.

To give an idea of what the memory consumption is, the C++ b2Body uses 184-bytes of memory on the 64-bit platform I just printed out the result of sizeof(b2Body) on.

Post a Comment for "Multiple Fixtures On One Body Or Multiple Bodies?"