假设我想在屏幕上创建1000或甚至5000个静态线条。我想知道的是,将所有这些线条(装置)附加到单个物体上和将每个装置放置在其自己的物体上之间有什么区别。这两种方法之间是否存在性能差异,或者其中一种方法提供了更多功能或控制?
以下显示了两种方法之间的差异。
将每条线附加到单个物体上:
// 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();
}
将每条线附加到它们自己的主体上:
// 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();
}
这个代码示例是针对libGDX的,但我想这是一个相当基本的box2D概念,即使没有libGDX经验也可以回答。
一个可能存在的功能区别的例子是,如果所有的线都附着在一个单一的物体上,我们调用world.destroyBody(groundBody);
时,它也会摧毁所有的线,但是如果每条线都附着在不同的物体上,我们只会摧毁一条线。
但即使如此,这是否会有实质性的差别呢?如果所有线都附着在一个单一的物体上,我们可以简单地调用groundBody.destroyFixture(fixture);
来销毁一条单独的线。