James Bennet
James Bennet

Reputation: 603

OpenGL Terrain Colission Detection

Okay, I have some procedurally generated terrain (based loosely on http://www.swiftless.com/terraintuts.html)

Ive got a teapot "plane" which you can fly around in (third person camera)

Basically, the aim is to fly through the valleys etc... without crashing.

What I cant work out is how to calculate whether you have collided with the terrain or not?

Any ideas?

Upvotes: 2

Views: 4393

Answers (2)

user97370
user97370

Reputation:

A terrain map is particularly easy to test for collision against, since a terrain map is a map from a 2d point (x, y) to the height of the terrain, TERRAIN[x, y] at that point. Given your teapot plane at (t_x, t_y, t_z), just test its height against the height of the terrain at (t_x, t_y). That is, if t_z < TERRAIN[x, y], then you've crashed. You can test multiple points (perhaps the corners of a cube centered at the center of the teapot) against the terrain to give a more accurate result. Depending on the coarseness of your terrain map, you might pick a nearby grid-point, or interpolate linearly to compute the height of the terrain at an arbitary point.

Upvotes: 3

chrisburke.io
chrisburke.io

Reputation: 1507

There are entire books dedicated to collision detection, but in a simple game I am working on I handle collision detection by creating a radius for each entity which defines its bounding sphere. In every frame, all entities are compared against all other entities to find the distance between them, and if the distance is less than the combined radii of the two entities then a collision is recorded.

To do this I have each class (for example rocket, monster, player, tree etc) subclass the class Entity which holds logic for when collisions occur, meaning that when a rocket is fired it can kill a monster, but only burn a tree (assuming I have rocket and tree entities).

A good book I follow which explains a lot of this in much greater detail is:

http://www.amazon.co.uk/Beginning-OpenGL-Game-Programming-Second/dp/159863528X/ref=dp_ob_title_bk

Upvotes: 0

Related Questions