我設法在libgdx中使用box2d。然而,給出here的示例代碼僅適用於動態主體。我試圖使用它,它的工作原理很好,但是當我將Dynamic更改爲KinematicBody時,代碼不起作用。這裏是我的代碼運動物體碰撞檢測LIBGDX
@Override
public void create() {
// Create our body definition
BodyDef groundBodyDef =new BodyDef();
groundBodyDef.type = BodyDef.BodyType.StaticBody;
// Set its world position
groundBodyDef.position.set(new Vector2(0, 10));
// Create a body from the defintion and add it to the world
groundBody = world.createBody(groundBodyDef);
// Create a polygon shape
PolygonShape groundBox = new PolygonShape();
// Set the polygon shape as a box which is twice the size of our view port and 20 high
// (setAsBox takes half-width and half-height as arguments)
groundBox.setAsBox(camera.viewportWidth, 10.0f);
// Create a fixture from our polygon shape and add it to our ground body
groundBody.createFixture(groundBox, 0.0f);
// Clean up after ourselves
groundBox.dispose();
//endregion
BodyDef bodyDef = new BodyDef();
// We set our body to dynamic, for something like ground which doesnt move we would set it to StaticBody
bodyDef.type = BodyDef.BodyType.KinematicBody;
// Set our body's starting position in the world
bodyDef.position.set(bolaX, bolaY);
// Create our body in the world using our body definition
body = world.createBody(bodyDef);
// Create a circle shape and set its radius to 6
CircleShape circle = new CircleShape();
circle.setRadius(20f);
// Create a fixture definition to apply our shape to
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = circle;
fixtureDef.density = 20;
fixtureDef.friction = 0;
fixtureDef.restitution = 0.6f; // Make it bounce a little bit
// Create our fixture and attach it to the body
Fixture fixture = body.createFixture(fixtureDef);
// Remember to dispose of any shapes after you're done with them!
// BodyDef and FixtureDef don't need disposing, but shapes do.
circle.dispose();
Gdx.input.setInputProcessor(this);
}
@Override
public void render() {
world.step(1/60f, 6, 2);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
camera.update();
//batch.getProjectionMatrix().set(camera.combined);
batch.begin();
textureBodies = body.getPosition();
float angle = MathUtils.radiansToDegrees * body.getAngle();
//batch.draw(ball,textureBodies.x ,textureBodies.y );
batch.draw(ball, textureBodies.x - (bola.getWidth()/2) , textureBodies.y - (bola.getHeight()/2) , // the bottom left corner of the box, unrotated
1f, 1f, // the rotation center relative to the bottom left corner of the box
bola.getWidth(), bola.getHeight(), // the width and height of the box
1, 1, // the scale on the x- and y-axis
angle);
batch.end();
debugRenderer.render(world, camera.combined);
if(Gdx.input.isKeyPressed(Input.Keys.DPAD_UP)){
Gdx.app.log("Input Test", "key down: " + "aw ---- x" + body.getPosition().x + " y " + body.getPosition().y);
vel += 1;
//Gdx.app.log("vel","" + vel);
body.setLinearVelocity(0f,vel);
} else{
vel -= 1;
// Gdx.app.log("vel","" + body.getPosition().y);
body.setLinearVelocity(0f,vel);
}
int numContacts = world.getContactCount();
if (numContacts > 0) {
Gdx.app.log("contact", "start of contact list");
for (Contact contact : world.getContactList()) {
Fixture fixtureA = contact.getFixtureA();
Fixture fixtureB = contact.getFixtureB();
Gdx.app.log("contact", "between " + fixtureA.toString() + " and " + fixtureB.toString());
}
Gdx.app.log("contact", "end of contact list");
}
}
這裏是圖像。您可以在左側看到的是DynamicBody,右側是KinematicBody。
獲取聯繫人的工作動態身體,但沒有運動的身體。你能告訴如何檢測運動物體中的碰撞嗎?
http://box2d.org/ forum/viewtopic.php?f = 3&t = 5360 – Pranav008
因此,在任何方向使用移動物體時,運動學的使用都是錯誤的?因爲運動學沒有碰撞? – thenewbie
好吧,我沒有說過,在運動的身體上沒有碰撞。運動身體與動態身體相撞。動態或運動或靜態物體的使用取決於遊戲的要求。一般來說,如果一個身體正在移動,那麼我寧願保持它的動態。 – Pranav008