Game Development Reference
In-Depth Information
Create a new
MyContactListener.java
file in the
com.packtpub.libgdx.
collisiontest
package and add the following class:
public class MyContactListener extends ContactListener {
@Override
public void onContactStarted(btCollisionObject colObj0,
btCollisionObject colObj1) {
Gdx.app.log(this.getClass().getName(), "onContactStarted");
}
}
In the
create()
method of our game class, we simply call the following method:
MyContactListener contactListener = new MyContactListener();
Bullet contact listener provides a lot of methods for various
collision states. For more information, visit
https://
Adding some rigid bodies
Now, we will create individual bodies and set their properties and put them into our
dynamics world, as follows:
modelbuilder.begin();
MeshPartBuilder mpb = modelbuilder.part("parts",
GL20.GL_TRIANGLES, Usage.Position | Usage.Normal | Usage.Color,
new Material(ColorAttribute.createDiffuse(Color.WHITE)));
mpb.setColor(1f, 1f, 1f, 1f);
mpb.box(0, 0, 0, 40, 1, 40);
Model model = modelbuilder.end();
groundInstance = new ModelInstance(model);
btCollisionShape groundshape = new btBoxShape(new Vector3(20, 1
/ 2f, 20));
btRigidBodyConstructionInfo bodyInfo = new
btRigidBodyConstructionInfo(0, null, groundshape, Vector3.Zero);
btRigidBody body = new btRigidBody(bodyInfo);
world.addRigidBody(body);