简体   繁体   English

合并Box2d实体?

[英]Pooling Box2d Bodies?

I'm trying to spawn chunks by pooling box2d Bodies, I don't know if libgdx pooling is applicable to bodies but if it is please someone explain me how to do it and what's wrong with my code. 我正在尝试通过合并box2d实体来生成块,我不知道libgdx池是否适用于主体,但如果是,请有人向我解释该怎么做以及我的代码有什么问题。

first I created the BodyDef and Body on seperate methods: 首先,我在单独的方法上创建了BodyDef和Body:

public BodyDef createDef(){
        BodyDef def = new BodyDef();
        def.type = BodyDef.BodyType.StaticBody;
        def.fixedRotation = true;
        def.position.set(6, 6);

        return(def);
    }

    public Body createBody(){
        Body body = world.createBody(createDef());
        PolygonShape shape = new PolygonShape();
        shape.setAsBox(1, 1);
        body.createFixture(shape, 1.0f);
        shape.dispose();

        return(body);
    }

 public void createPlatform(){
      Body platform = Pools.obtain(Body.class); //then use pooling
        platform = createBody(); //here I set the value equal to the return value of createBody() method 
        bodies.add(platform);//adding platform to the ArrayList
    }

then to spawn chunks I simply call this method: 然后生成块,我只需调用此方法:

public void spawnChunk(){
        createPlatform();
    }

I'm so new to this I don't know what's the meaning of chunk but I know that is used on side scrolling game for spawning terrain, I get this error message : 我对此很陌生,我不知道块的含义是什么,但我知道它在侧面滚动游戏中用于生成地形, 我得到以下错误消息

Exception in thread "LWJGL Application" java.lang.RuntimeException: Class cannot be created (missing no-arg constructor): com.badlogic.gdx.physics.box2d.Body
    at com.badlogic.gdx.utils.ReflectionPool.<init>(ReflectionPool.java:41)
    at com.badlogic.gdx.utils.Pools.get(Pools.java:29)
    at com.badlogic.gdx.utils.Pools.get(Pools.java:38)
    at com.badlogic.gdx.utils.Pools.obtain(Pools.java:48)
    at com.nivekbryan.ragingpotato.Physics.createPlatform(Physics.java:53)
    at com.nivekbryan.ragingpotato.Physics.spawnChunk(Physics.java:59)
    at com.nivekbryan.ragingpotato.WorldController.update(WorldController.java:17)
    at com.nivekbryan.ragingpotato.MainClass.render(MainClass.java:27)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:215)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:120)

Don't use pooling for box2d bodies or joints, from the box2d wiki: 不要使用box2d Wiki中的box2d实体或关节的缓冲池:

So when you create a b2Body or a b2Joint, you need to call the factory functions on b2World. 因此,当您创建b2Body或b2Joint时,需要在b2World上调用工厂函数。 You should never try to allocate these types in another manner. 您永远不要尝试以其他方式分配这些类型。

So you only should use Body body = world.createBody(bodyDef); 因此,您只应使用Body body = world.createBody(bodyDef); to create bodies. 创造身体。

Next you add the bodies in some kind of list, which one should never do! 接下来,在某种列表中添加主体,这是永远都不要做的! If you need to access all bodies in your world, then do it like this: 如果您需要访问世界上的所有物体,请按照以下步骤操作:

// Global field
Array<Body> bodies = new Array<>();

// When you want to access the bodies.
world.getBodies(bodies);
for(Body body : bodies)
{
    // Do something
}

The error means that the Body class has no constructor that looks like 该错误意味着Body类没有看起来像的构造函数

public Body() {}

And thus can not be created by the generic pool class implementation, since it tries to call this constructor. 因此不能由通用池类实现创建,因为它试图调用此构造函数。

To solve this you could implement the pool yourself, but as stated above, you should not do this. 要解决此问题,您可以自己实现池,但是如上所述,您不应这样做。


Another error with your current code is that you seem to misunderstand variable assignment in java. 当前代码的另一个错误是您似乎误解了Java中的变量赋值。

// gets a body from your pool, arbitrary method
Body pooledBody = getFromPool();

// assigns a new body to pooledBody, so your pooling is "voided".
pooledBody = world.createBody(createDef());

As you can see in this example the getFromPool(); 如本例所示, getFromPool(); has no effect, since you assign a new value to pooledBody in the next line. 无效,因为您在下一行中为pooledBody分配了新值。

A more simple example, on why it does not work as you wish: 一个更简单的示例,说明为什么它不能如您所愿的工作:

int a = getFromPool(); // lets says this assigns 4 to a
a = 5;
System.out.println(a);

Will always print 5, never 4! 将始终打印5,从不打印4!


What you can pool is BodyDef , FixutureDef and the Shape classes, since these have no argument constructors. 您可以BodyDefBodyDefFixutureDefShape类,因为它们没有参数构造函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM