For my upcoming 3D strategy title, I really needed something to display terrain. In my game, there won't be any closeup views from the side, therefore, I don't need a fancy CLOD algorithm. I decided to use a plain old heightmap with brute force rendering. It allows me to offload 100% of the computations to the graphics card and the implementation is easy as pie.
However, things got quite a bit more complicated when I had to tackle collision detection. Determining the height at a random point within the heightmap was still easy going, but writing a bullet-proof ray-to-heightmap collision detector turned out to be a real nightmare.
If you understand a heightmap as a dented rectangle floating in space, rays could travel below the heightmap without touching it and there wouldn't be a clear concept of 'below' and 'above'. The most elegant solution I could find was to make the collision detection system see the heightmap as if its borders extended into infinity at their respective height levels. Or, in mathematical terms, the heightmap now resembles a solid plane that divides space into two sides.
So, for anyone wrestling with heightmap collision detection himself, here's a complete, tested heightmap class with methods to determine the terrain's height at any given point and a ray vs. heightmap intersection detector that gives stable results even if the ray shoots into the ground outside of the heightmap. It does NOT render terrain, it's a pure utility class for managing the height values and performing collision detection.
Post new comment