我试图用LibGDX和Box2D编写一些突破性游戏。但有一点我不明白。必须有两块砖只与边缘接触。但是在模拟器上我只看到两个嵌套的砖块。
这里是createBox方法的代码:
private void createBox(float posX, float posY, float boxW, float boxH) {
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyDef.BodyType.StaticBody;
bodyDef.position.set(posX, posY);
Body body = world.createBody(bodyDef);
PolygonShape shape = new PolygonShape();
shape.setAsBox(boxW, boxH);
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = shape;
fixtureDef.density = 1f;
Fixture fixture = body.createFixture(fixtureDef);
shape.dispose();
}
编辑:那个相乘的代码。
createBox(CONS_HOLDER.BRICKS_LEFT_MARGIN + (i * CONS_HOLDER.BRICK_WIDTH * 2 ),
CONS_HOLDER.BRICK_TOP_SCREEN_MARGIN + (j * CONS_HOLDER.BRICK_HEIGHT * 2 ) + CONS_HOLDER.BRICKS_TOP_MARGIN,
CONS_HOLDER.BRICK_WIDTH / 2,
CONS_HOLDER.BRICK_HEIGHT / 2);
答案 0 :(得分:1)
setAsBox(float, float)
的文档指出参数为half-width
和half-height
。您应该将盒子的尺寸分成两半,以获得正确的尺寸。
private void createBox(float posX, float posY, float boxW, float boxH) {
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyDef.BodyType.StaticBody;
bodyDef.position.set(posX, posY);
Body body = world.createBody(bodyDef);
PolygonShape shape = new PolygonShape();
shape.setAsBox(boxW / 2.f, boxH / 2.f);
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = shape;
fixtureDef.density = 1f;
Fixture fixture = body.createFixture(fixtureDef);
shape.dispose();
}