一个身体或多个身体上有多个固定装置? [英] Multiple fixtures on one body or multiple bodies?

查看:124
本文介绍了一个身体或多个身体上有多个固定装置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我想在屏幕上创建1000条甚至5000条静态身体线条。我想知道的是,将所有这些线(固定装置)连接到单个主体上,或将每个固定装置放在自己的主体上有什么区别。两种方法之间是否存在性能差异,或者一种方法是否提供了更多功能或控制其他方法?



下面显示了两种方法之间的差异。



将每一行附加到单个实体上:

  //创建我们的身体定义
BodyDef groundBodyDef = new BodyDef();
groundBodyDef.type = BodyType.StaticBody;

//从定义中创建一个实体并将其添加到世界
Body groundBody = world.createBody(groundBodyDef);

for(int i = 0; i< 1000; i ++){
//创建我们的行
EdgeShape ground = new EdgeShape();
ground.set(x1,y1,x2,y2);
groundBody.createFixture(ground,0.0f);
ground.dispose();
}

将每一行附加到自己的身体:

  //创建我们的身体定义
BodyDef groundBodyDef = new BodyDef();
groundBodyDef.type = BodyType.StaticBody;

for(int i = 0; i< 1000; i ++){
//从定义创建一个体并将其添加到世界
Body groundBody = world。 createBody(groundBodyDef);

//创建我们的行
EdgeShape ground = new EdgeShape();
ground.set(x1,y1,x2,y2);
groundBody.createFixture(ground,0.0f);
ground.dispose();
}

此代码示例特别在libGDX中,但我想这是一个相当基本的box2D的概念,即使没有libGDX经验也可以回答。



可能的功能差异的一个例子是,如果所有的线都连接到一个单独的身体而我们是调用 world.destroyBody(groundBody); ,它也会销毁所有行,但是如果每一行连接到不同的主体,我们只会销毁一行。 / p>

即使这会产生实质性的差异吗?我们可以简单地调用 groundBody.destroyFixture(fixture); 来销毁单行,如果它们都附加到单个主体上。

解决方案

来自 Box2D手册 7.3 Body Factory ):


将几个形状附加到静态主体比创建
几个静态主体更快更快每个单一的形状。在内部,
Box2D将静态物体的质量和反质量设置为零。这个
使数学运算成功,因此大多数算法都不需要将
静态物体视为一种特殊情况。


确实性能更高。它引人注目吗?取决于,如果你有很多身体有一个固定装置,你可以让它们成为一个单体的固定装置,但可能不适用于大多数游戏。



我建议你使用一个更容易实现的,只有在你真正需要它时才能使它更具性能。


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();
    }

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.

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.

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.

解决方案

From the Box2D Manual (7.3 Body Factory):

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.

这篇关于一个身体或多个身体上有多个固定装置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆