Bullet Physics for Collision Detection and Smooth Response

| | August 5, 2015

I’m creating a game using LibGDX, and to prevent the need to spend lots of time writing collision detection code I’ve decided to make use of Bullet Physics, simply for collision detection and not for physics simulation. I also figure that any collision detection algorithms in a mature open source project is going to be far more efficient than I can implement in the time that I have.

However, I’m having issues with creating a smooth collision response. I’m using a very simple custom callback that looks like this:

        public boolean onContactAdded(btManifoldPoint cp, int userValue0, int partId0, int index0, int userValue1, int partId1, int index1) {
        if(userValue0 == CollisionTags.PLAYER.getID() && userValue1 == CollisionTags.ENEMY.getID()) {
            Vector3 normal = new Vector3(0, 0, 0);
            cp.getNormalWorldOnB(normal);
            player.translate(normal.scl(-cp.getDistance1()));
        }
        return true;
    }

In other words, I take the normal (direction in which the collision has occurred), and translate the player back in this direction (scaling this vector by the depth of the collision, obtained by cp.getDistance1()). However, this does not result in very smooth collisions, the player appears to jump in and out of the object they are colliding with. I’m having problems finding any other data that I could use that is given to me by btManifoldPoint in order to make this collision response smoother. I have also tried to stop the player’s movement along the axis of the collision with:

if(normal.x != 0)
    player.setVelocity(0, 0, player.getVelocity().z);
else if(normal.z != 0)
    player.setVelocity(player.getVelocity().x, 0, 0);

However, as this is happening when the player’s bounding box has already intersected the wall, this has very little effect. There does not appear to be any way to predict that occurrence of a collision before it happens, and prevent the intersection occurring using these custom callbacks. How else could I smooth this collision, using the data supplied by btManifoldPoint?

Leave a Reply