# Need Help With Terrain Mesh Collisions

• 06-10-2012
DCLXVI47
Need Help With Terrain Mesh Collisions
Im proficient with c++ and and OpenGL(at a basic Level), I am trying to get deeper into 3d drawing. So I have created simple FPS movement. My camera works fine, and I have created a basic Terrain out of triangles. The problem I have not been able to solve is, how to handle ground collisions. I want the set the camera at player height above the ground and run around realisticly even on hills and valleys.
I have searched for days and none of the solutions Ive seen have helped. I am struggling to understand how to find the point where the actors possition intersects with the ground. My understanding of trigonometry is pretty weak, so can anybody help me out?

Thank you in advance.
• 06-11-2012
ledow
The answer: If you want realism, you have to code it.

The naive way would be to just put the viewer at the height of the nearest vertex to them. That needs no calculations but will result in jumpy step-like movement up and down as they traverse different heights.

There's an intermediate way of calculating the intersection of a line and a plane that needs quite good knowledge of matrix maths.

The proper way is to expand the problem to the collision of two objects and have a primitive collision engine resolve the difference for you.

Either way, it's a lot more than can be answered in a small post. Try the usual sources of OpenGL tutorials (e.g. NeHe Productions: Collision Detection) and look around at the maths behind what OpenGL is actually doing all the time. I can also recommend the book "Physics for Game Developers" which covers things like this as well as making basic physics engines in C++.

Personally, for a quick "cheap" answer, I'd find the nearest three/four vertex heights of the ground and average them in a quick-cheap calculation that, if I could be bothered, would average them biased towards the nearest one. Chances are it would be fine for 99% of things, but the proper way is to learn about plane intersection matrix maths.
• 06-11-2012
DCLXVI47
I tried to average the points for my first attempt. But like you said, it was jumpy. I actually have that book, I just am having trouble getting this to work.
Ill keep trying.
Thanks for the info ledow
• 06-11-2012
VirtualAce
You need to determine the height at px,py within cell C. You can do this by determining the triangle the player is in. Each quad is composed of two triangles. Once you figure out which triangle the player is in you can the perform a bilinear interpolation on the heights (usually y value) of the vertices. Place the player at y + some_height where some_height is the distance from the player's eyes to his feet and you will be able to walk over the terrain. Note that a more advanced version of this will pitch the player's view to match the slope they are on. As well the slope should be taken into account when moving the player around.

You can download source code for this from the web site for several books by Frank Luna. In the books he explains how to do exactly what you want.

http://www.d3dcoder.net/

Don't worry about the underlying API. The concept is the same in Direct3D as it is in OpenGL. The only difference is the 'handedness' of the coordinate system and the API calls you might make.
• 06-15-2012
gegoggigog
I found barycentric corrdinates to be of use when calculating weigths for interpolating over a triangle. :)
Barycentric coordinate system (mathematics) - Wikipedia, the free encyclopedia
• 06-15-2012
VirtualAce
Uh yeah...isn't that what I said.
• 06-16-2012
DCLXVI47
Thanks Im checking out the links right now
• 06-16-2012
gegoggigog
Quote:

Originally Posted by VirtualAce
Uh yeah...isn't that what I said.

I would say yes and no.