问题描述
假设我想在屏幕上创建 1000 甚至 5000 条静态身体线条.我想知道的是,将所有这些线(夹具)连接到一个单一的身体上,或者将每个夹具放在自己的身体上有什么区别.两种方法之间是否存在性能差异,或者一种方法是否提供更多功能或对另一种方法的控制?
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();
}
此代码示例专门用于 libGDX,但我认为这是一个相当基本的 box2D 概念,即使没有 libGDX 经验也可以回答.
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.
一个可能的功能差异的例子是,如果所有的行都附加到一个单一的身体,我们要调用 world.destroyBody(groundBody);
,它也会破坏所有的线,但是如果每条线都连接到不同的主体,我们只会销毁一条线.
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.
即使这样也有很大的不同吗?我们可以简单地调用 groundBody.destroyFixture(fixture);
来销毁一行,如果它们都附加到一个主体上.
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.
推荐答案
来自 Box2D 手册 (7.3车身工厂):
将多个形状附加到静态主体比创建要快几个静态物体,每个物体都有一个形状.在内部,Box2D 将静态物体的质量和反质量设置为零.这使数学计算出来,这样大多数算法就不需要处理静态体作为特例.
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.
这篇关于一个主体或多个主体上的多个固定装置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!