合并 Box2d 实体?
Pooling Box2d Bodies?
我正在尝试通过合并 box2d 主体来生成块,我不知道 libgdx 合并是否适用于主体,但如果适用,请有人解释我该怎么做以及我的错误是什么代码。
首先我在不同的方法上创建了 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
}
为了生成块,我简单地调用了这个方法:
public void spawnChunk(){
createPlatform();
}
我对此很陌生,我不知道块是什么意思,但我知道它在横向卷轴游戏中用于生成地形,我收到此错误消息:
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.run(LwjglApplication.java:120)
不要对 box2d 物体或关节使用池化,来自 box2d wiki:
So when you create a b2Body or a b2Joint, you need to call the factory
functions on b2World. You should never try to allocate these types in
another manner.
所以你只应该使用 Body body = world.createBody(bodyDef);
来创建物体。
接下来您将尸体添加到某种列表中,这是永远不应该做的!如果你需要访问你世界中的所有身体,那么这样做:
// Global field
Array<Body> bodies = new Array<>();
// When you want to access the bodies.
world.getBodies(bodies);
for(Body body : bodies)
{
// Do something
}
错误意味着 Body
class 没有看起来像
的构造函数
public Body() {}
因此不能由通用池 class 实现创建,因为它试图调用此构造函数。
要解决这个问题,您可以自己实现池,但如上所述,您不应该这样做。
您当前代码的另一个错误是您似乎误解了 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());
正如您在此示例中看到的那样,getFromPool();
没有任何效果,因为您在下一行中为 pooledBody
分配了一个新值。
一个更简单的例子,说明为什么它不能如你所愿地工作:
int a = getFromPool(); // lets says this assigns 4 to a
a = 5;
System.out.println(a);
将始终打印 5,从不打印 4!
您可以合并的是 BodyDef
、FixutureDef
和 Shape
classes,因为它们没有参数构造函数。
我正在尝试通过合并 box2d 主体来生成块,我不知道 libgdx 合并是否适用于主体,但如果适用,请有人解释我该怎么做以及我的错误是什么代码。
首先我在不同的方法上创建了 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
}
为了生成块,我简单地调用了这个方法:
public void spawnChunk(){
createPlatform();
}
我对此很陌生,我不知道块是什么意思,但我知道它在横向卷轴游戏中用于生成地形,我收到此错误消息:
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.run(LwjglApplication.java:120)
不要对 box2d 物体或关节使用池化,来自 box2d wiki:
So when you create a b2Body or a b2Joint, you need to call the factory functions on b2World. You should never try to allocate these types in another manner.
所以你只应该使用 Body body = world.createBody(bodyDef);
来创建物体。
接下来您将尸体添加到某种列表中,这是永远不应该做的!如果你需要访问你世界中的所有身体,那么这样做:
// Global field
Array<Body> bodies = new Array<>();
// When you want to access the bodies.
world.getBodies(bodies);
for(Body body : bodies)
{
// Do something
}
错误意味着 Body
class 没有看起来像
public Body() {}
因此不能由通用池 class 实现创建,因为它试图调用此构造函数。
要解决这个问题,您可以自己实现池,但如上所述,您不应该这样做。
您当前代码的另一个错误是您似乎误解了 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());
正如您在此示例中看到的那样,getFromPool();
没有任何效果,因为您在下一行中为 pooledBody
分配了一个新值。
一个更简单的例子,说明为什么它不能如你所愿地工作:
int a = getFromPool(); // lets says this assigns 4 to a
a = 5;
System.out.println(a);
将始终打印 5,从不打印 4!
您可以合并的是 BodyDef
、FixutureDef
和 Shape
classes,因为它们没有参数构造函数。