Java – multiple clamps in one body or multiple bodies?
Suppose I want to create 1000 or even 5000 static body lines on the screen What I want to know is the difference between attaching all these wires (lamps) to a single body or placing each fixture on its own body Is there a performance difference between the two methods, or does one method provide more functionality or control other methods?
The difference between the two methods is shown below
Connect each line to 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(); }
Connect each line to your 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,0.0f); ground.dispose(); }
This code example is especially in libgdx, but I imagine this is a fairly basic @ R_ 559_ 2419@2D The concept can be answered even without libgdx experience
An example of a possible functional difference is if all rows are attached to a single principal and we call world destroyBody(groundBody); It will also destroy all rows, but if each row is attached to a different body, we will only destroy one line
Even if it makes a big difference? We can simply call groundbody destroyFixture(fixture); If they are all connected to a single body, destroy a line
Solution
From box2d manual (7.3 body factory):
This is really better Obviously, if you have many bodies with a fixture, you make them a single body, but probably not most games
I would suggest that you use a feature that is easier to implement and can only make it more performance when you really need it