Thread: Need Help With Terrain Mesh Collisions

  1. #1
    Registered User DCLXVI47's Avatar
    Join Date
    Jun 2012
    Posts
    3

    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.

  2. #2
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    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.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  3. #3
    Registered User DCLXVI47's Avatar
    Join Date
    Jun 2012
    Posts
    3
    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

  4. #4
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    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.
    Last edited by VirtualAce; 06-11-2012 at 04:41 PM.

  5. #5
    Registered User gegoggigog's Avatar
    Join Date
    Jun 2012
    Location
    Sweden
    Posts
    2
    I found barycentric corrdinates to be of use when calculating weigths for interpolating over a triangle.
    Barycentric coordinate system (mathematics) - Wikipedia, the free encyclopedia

  6. #6
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Uh yeah...isn't that what I said.

  7. #7
    Registered User DCLXVI47's Avatar
    Join Date
    Jun 2012
    Posts
    3
    Thanks Im checking out the links right now

  8. #8
    Registered User gegoggigog's Avatar
    Join Date
    Jun 2012
    Location
    Sweden
    Posts
    2
    Quote Originally Posted by VirtualAce View Post
    Uh yeah...isn't that what I said.
    I would say yes and no.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. SDL not recognizing collisions
    By bijan311 in forum C++ Programming
    Replies: 4
    Last Post: 05-28-2010, 06:54 PM
  2. Collisions does not take affect on the corners
    By bijan311 in forum C++ Programming
    Replies: 4
    Last Post: 05-21-2010, 08:34 PM
  3. clear collisions
    By Benzakhar in forum Game Programming
    Replies: 4
    Last Post: 01-10-2004, 06:22 AM
  4. Help with collisions.
    By Unregistered in forum Game Programming
    Replies: 2
    Last Post: 06-25-2002, 01:00 PM
  5. Collisions help(PLEASE!!! IT'S NOT HARD!!!)
    By SyntaxBubble in forum Windows Programming
    Replies: 5
    Last Post: 11-28-2001, 06:19 PM