I managed to use box2d in libgdx. However the example code given here is for dynamic body only. I tried to use it and it works great but when i change the Dynamic to KinematicBody the code does not work. here is my code
@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");
}
}
And here is the image.. You can see in the left side is the DynamicBody and the right side is the KinematicBody.
Getting the contacts works on Dynamic Body but not on Kinematic Body. Can you tell how to detect the collision in kinematic body?