Game Development Reference
In-Depth Information
A custom MotionState class
In a 3D world with many physics objects, all might not be at motion at the same time.
For each frame render, if we iterate and update positions of all render objects we're
simulating, it would require a lot of time especially if the game has a lot of physics
bodies. Luckily, the Bullet wrapper offers callback methods that will be called when
a certain event occurs. We create a custom interface extending the btMotionState
class where we include what to do when something happens. For example, create
a new MyMotionState.java file in the com.packtpub.libgdx.collisiontest
package and add the following code:
public class MyMotionState extends btMotionState {
final ModelInstance instance;
public MyMotionState (ModelInstance instance) {
this.instance = instance;
}
@Override
public void getWorldTransform(Matrix4 worldTrans) {
worldTrans.set(instance.transform);
}
@Override
public void setWorldTransform(Matrix4 worldTrans) {
instance.transform.set(worldTrans);
}
}
The setWorldTransform() function will set the transformation of the render object,
whereas the getWorldTransform() function returns the transformation of the
current render object. This custom class will update the render instance when Bullet
updates the position of respective physics object.
A simple ContactListener class
LibGDX Bullet wrapper offers callback methods to notify us when a collision occurs.
Here, we can define what should happen when a collision occurs. This is similar to
contact listener in Box2D. However, this callback class, ContactListener , is not a
Bullet class but a class specifically created for the Bullet wrapper. For this reason, we
do not have to inform Bullet to use ContactListener .
 
Search WWH ::




Custom Search