# How can I extend btCollisionAlgorithm to enable collision with a voxel terrain?

I am using Bullet, and am attempting to make a collision algorithm that generates contact points off of a cube-based terrain along with the appropriate collision response. I also plan to extend this to include non-box shapes too, however that is not vital at the moment. I've found that using a triangle mesh is too much of a RAM hog for large maps.

I have tried the procedure outlined by Byte56 here, however, I have a number of questions regarding implementing this with Bullet:

• How do you generate a collision shape for the world? Do you use a custom shape? What do you set m_shapeType to be on it?
• Or do you still use a box shape the size of the world?
• How do you ensure that contact points are freed?
• How exactly do you modify processCollision?

What I've done:

• I've created a terrainShapethat extendsbtBoxShape, the only difference being thatm_shapeType = CUSTOM_CONVEX_SHAPE_TYPE, in order for me to be able to register a new collision algorithm with the dispatcher for objects with only this shape.
• I've extended the btRigidBody class in a way similar to Byte56 in his question (see the link in the 2nd paragraph), however checkCollisionWith(CollisionObject * co) returns true if any voxel in the AABB of co is not air.
• I've extended the btCollisionAlgorithm class, in a way similar to btCompoundCollisionAlgorithm, with processCollision doing the following:

1. Check the colliding objects passed as arguments, and determine which is the terrain, and which is the entity.
2. Clear the manifolds any child algorithms.
3. Call resultOut->setPersistantManifold(resultOut)
4. Generate new box shapes and transforms in the AABB occupied by the colliding entity, and then call m_dispatcher->findAlgorithm. Store the shape, transform and found algorithm in a Child Algorithm struct for each voxel within the AABB.
5. Iterate over all child algorithms, calling proccessCollision.
6. Iterate over all child algorithms, removing any now outside of the AABB of the colliding entity. (calling ~btCollisionAlgorithm() then m_dispatcher->freeCollisionAlgorithm())
7. Call resultOut->refreshContactPoints().

What works: processCollision is called whenever the player's AABB intersects with non-air voxels.

What doesn't: the collision response is just plain weird... The player entity starts levitating upwards. If it walks into something, it bounces off violently. Sometimes, it is unable to keep moving on one axis after walking into something. What I suspect is going on is that contact points are not being released after collision response, possibly due to the player entity always being in the world object's AABB. I am curious to see if I am barking up the right tree with respect to processCollision?

• Did you sort this out at all? I'm having a very similar issue. – timoxley Mar 7 '13 at 8:40
• A bit late, sorry, I didn't see your question. If you haven't figured this out already, have you tried the debug drawing of your physics bodies? The AABB you're using to detect the collision does not bound the physics body you're using. So by the time you detect the collision, your object is already far inside of it. This explains why your collision reactions are so strange. – MichaelHouse Mar 30 '13 at 1:59
• See my latest answer to my question: gamedev.stackexchange.com/questions/27405/… – MichaelHouse Mar 31 '13 at 0:35

I started creating custom collision meshes for each chunk of voxel terrain. I do that with the BvhTriangleMeshShape`. This works pretty well: