【发布时间】:2014-02-05 16:52:27
【问题描述】:
假设我想在屏幕上创建 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); 来销毁一行。
【问题讨论】:
标签: java android performance libgdx box2d