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.