Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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) onto a single body, or placing each fixture onto its own body. Is there a performance difference between the two methods, or does one method provide more functionality or control over the other method?

Below shows the difference between both methods.

Attaching each line onto a single body:

    // Create our body definition
    BodyDef groundBodyDef = new BodyDef();
    groundBodyDef.type = BodyType.StaticBody;

    // Create a body from the defintion and add it to the world
    Body groundBody = world.createBody(groundBodyDef);

    for (int i = 0; i < 1000; i++) {
        // Create our line
        EdgeShape ground = new EdgeShape();
        ground.set(x1, y1, x2, y2);
        groundBody.createFixture(ground, 0.0f);
        ground.dispose();
    }

Attaching each line to their own body:

    // Create our body definition
    BodyDef groundBodyDef = new BodyDef();
    groundBodyDef.type = BodyType.StaticBody;

    for (int i = 0; i < 1000; i++) {
        // Create a body from the defintion and add it to the world
        Body groundBody = world.createBody(groundBodyDef);

        // Create our line
        EdgeShape ground = new EdgeShape();
        ground.set(x1, y1, x2, y2);
        groundBody.createFixture(ground, 0.0f);
        ground.dispose();
    }

This code example is specifically in libGDX, however I imagine this is a fairly basic box2D concept and could be answered even without libGDX experience.

One example of a possible functionality difference is that if all of the lines are attached to a single body and we were to call world.destroyBody(groundBody);, it would also destroy all of the lines, however if each line is attached to a different body, we would only destroy one line.

Does even this make a substantial difference though? We can simply call groundBody.destroyFixture(fixture); to destroy a single line if they are all attached to a single body.

like image 223
Gatekeeper Avatar asked Jan 16 '14 16:01

Gatekeeper


2 Answers

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.

like image 182
Louis Langholtz Avatar answered Oct 21 '22 11:10

Louis Langholtz


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.

like image 21
Lestat Avatar answered Oct 21 '22 11:10

Lestat