# How to load a terrain of connected caves into bullet-phisics engine?

I have a terrain that can not be represented as height map. Say a mountain with caves. I have it in one large .obj file. How do I load such a terrain into bullet-phisics engine?

Or I shall generate height map, load it as a terrain and then load my caves as a seprate obj file somehow cutting it out from generated terrain?

-

See my answer to a similar question I asked. Basically one option (that worked well for me) is to implement your own collision listener into Bullet Physics. All you really need to start is a function of your own that tells you whether a specific point in your world is solid. Then as you can see in my answer, you'll just need to override a few objects, and you're done. Most of the code can be copied from the original terrain collision listener, then slightly modified for your game. This is a fairly powerful way to get collision handling working, but it might not be the simplest.

-
Wount I loose in speed and performance If I woul use my terrain Impl instead of Bullets one? – myWallJSON Jun 8 '12 at 11:31
That's possible, but you have to realize that you're attempting to do something more complex than what bullet offers. With increased complexity comes decreased performance. I still get excellent performance with collision detection in my game. You won't know until you try. – Byte56 Jun 8 '12 at 14:23

Bullet does support arbitrary meshes; I'm sure Byte56's answer will work but you'll definitely be doing more work than you need to and it'll perform worse than Bullet's built-in solution.

Have a look at the btBvhTriangleMeshShape and btTriangleIndexVertexArray classes; you'll need to load in your obj file to get its vertices and indices, create a btTriangleIndexVertexArray containing those, then create a btBvhTriangleMeshShape from this. That'll give you the collision shape, then it's just a case of attaching that to a rigid body.

Here are Bullet's reference pages for the two classes; there should be enough google fuel in there for you!

http://bulletphysics.com/Bullet/BulletFull/classbtBvhTriangleMeshShape.html

http://bulletphysics.com/Bullet/BulletFull/classbtTriangleIndexVertexArray.html

Bear in mind that Bullet makes it hard to figure out who owns what memory; in this case it expects you to manage both the btTriangleIndexVertexArray you pass into the shape, and the vertex and index arrays you pass to that; it won't take a copy or take ownership of those pointers. So either keep them around as values on the stack or as class members, or make sure to keep hold of the pointers and delete them in the correct order.

-