Terrain Collision
Added terrain collision last week, but as I’m not using heightfields I had to use
something slightly different from the norm.
First I tried a pure triangle mesh based approach, I knew this would be slow as
hell and use a ton of memory but I wanted to have a working baseline to compare
against.
Initially for physics I used Havok, but as I don’t have $100,000 laying around to waste on a
physics engine I only had access to the binary version–and Havok only supplies
libs for VS2008, not VS2010 which I what I am using, although I was able to get
the 2008 libs working. After getting the basic triangle mesh collision up and running
in Havok I decided I didn’t much care for not having access to the source so I switched
to Bullet.
I’d used Bullet before so it was easy to get it switched over, and once I had the triangle
mesh collision set up I gave it a trial run.
The triangle mesh collision used approximatly one gig of memory, although generation speed
for the btBvhTriangleMeshShape was fairly quick. I created a task
to generate collision and spread the work across the cores which made generation faster.
I added spheres and boxes that I could drop onto the terrain to test the accuracy and
performance of the collision detection.
A gig of memory for terrain collision was obviously out of the question so I began testing
convex hulls.
Bullet has a btConvexHullShape which takes an array of
vertices in floating point format. This worked and reduced memory usage by more than
half. Still wasn’t good enough though.
I wrote my own convex hull shape which I called btCompressedConvexHullShape,
as the name implies it uses compressed verts(about 1/4th the memory per vert).
I also started using Bullets utility class btShapeHull. This class takes in
an array of vertices and produces a convex tri mesh with a greatly reduced number of
vertices. Feed it 2000 verts and get back a 14 vert convex mesh, that type of thing.
I feed the results of the btShapeHull back into btCompressedConvexHullShape or
a btConvexTriangleMeshShape(favoring btCompressedConvexHullShape as they
both seem to produce the same results and it uses less memory).
Memory usage for the physics simulation was greatly reduced at this point, down to
about 100 megs. There are still a few optimizations I’d like to do, mostly to reduce the allocations
taking place in the btShapeHull step, but overall the performance and
memory usage is fairly good at this point.
I’ve also got the physics simulation running as it’s own task, with adding and
removing of objects done asynchronously. This helps because as you move through
the world a great many terrain chunks(each as a convex hull) are being added and removed.
Collision seems to be fairly accurate as long as I don’t have it too far off
from the visual representation.
My gravity is currently just set using Bullets built in system, which is directional.
This means if I navigate to the side of the planet I can start dropping objects
and watch them bounce along through mountains and valleys for miles as they
travel along the edge of the planet.
Need to add a character control system soon.