Multiple fixtures on one body or multiple bodies?(一个主体或多个主体上的多个固定装置?)
问题描述
假设我想在屏幕上创建 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.
这篇关于一个主体或多个主体上的多个固定装置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:一个主体或多个主体上的多个固定装置?
基础教程推荐
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01