Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: 3D collision in OpenGL

  1. #1
    Intern Contributor
    Join Date
    Apr 2000
    Location
    Austria
    Posts
    57

    3D collision in OpenGL

    has someone got any idea/code for this?

    creating a little world or terrain,moving through it (with a car,person,ship or some other stuff)...no problem...but how should i detect wether my object hits the ground or not ( the height level of the ground varies on some places,like hills and valleys)

  2. #2
    Senior Member OpenGL Guru
    Join Date
    Feb 2000
    Location
    Sweden
    Posts
    2,982

    Re: 3D collision in OpenGL

    Just compare the position of the object with the height of the landscape (I suppose you have the heightmap somewhere in the memory).

    If you object is at (x,y,z) (y-axis upwards in landscape), you compare y with the heightvalue at (x,z)

    Bob

  3. #3
    Senior Member OpenGL Guru Humus's Avatar
    Join Date
    Mar 2000
    Location
    Stockholm, Sweden
    Posts
    2,345

    Re: 3D collision in OpenGL

    Yeah ... and that might mean that you'll have to calculate the plane equation of your polys that makes the terrains. That's easy done with vector cross product. Then, when you have the eq like A*x + B*y + C*z + D = 0 you just need to check wheter y > -(D + A*x + C*z) / B.

  4. #4
    Senior Member OpenGL Guru
    Join Date
    Feb 2000
    Location
    Sweden
    Posts
    2,982

    Re: 3D collision in OpenGL

    Hmm, can't see why you should use the plane equation to calculate wether or not you are above the terrain. All you have to do is obtain the height value of the terrain and the heightposition of the object and compare these two. Just two loads and one compare, nothing more.

  5. #5
    Senior Member OpenGL Guru Humus's Avatar
    Join Date
    Mar 2000
    Location
    Stockholm, Sweden
    Posts
    2,345

    Re: 3D collision in OpenGL

    The terrain is not necassary flat, it can be built of sloping triangles (like sebastian said: "the height level of the ground varies on some places,like hills and valleys") and in that case you need the plane eq to obtain the y coord for the ground at the viewers (x,z).

  6. #6
    Senior Member OpenGL Guru
    Join Date
    Feb 2000
    Location
    Sweden
    Posts
    2,982

    Re: 3D collision in OpenGL

    Let me explain what I mean here. You most certainly have a heightmap of the landscape. If you are at position p=(x,y,z), just look in the heightmap at element heightmap[x][y]. If heigtmap says the height at this position is 50, and you are at height 40, you are placed below the ground.

  7. #7
    Intern Contributor
    Join Date
    Apr 2000
    Location
    Austria
    Posts
    57

    Re: 3D collision in OpenGL

    Thanks guys for getting a headache about my problem...

    you are right,comparing the value of the heightmap to the y-coord of my object is definetly no problem,i have already implemented it 2 hours ago.

    but i want to have this stuff to be more realistic,so i think i'll have to turn on my brain in the next days.

    there's another question i have:
    do you want/have you got time to test my little game?i'd really appreciate it :-) !!!
    i've never seen it run on another 'puter,so i would be glad to see some differences.
    i'm using VC++ 6,the game is written in "old windows"..
    NOTE: the game is in a really early stage,so
    don't run away if you see it!
    when moving around in the scene,you should notice the changes of the object's height(very very basic)

    if you wanna help me,just post here.
    i think i will put some download on my site on monday or tuesday..

    thanx in advance,have a nice weekend...

    [This message has been edited by sebastian (edited 04-14-2000).]

  8. #8
    Advanced Member Frequent Contributor
    Join Date
    Feb 2000
    Location
    France
    Posts
    880

    Re: 3D collision in OpenGL

    Let's sum up...

    Bob was right and Humus was too...
    Sebastian has done what Bob said and for more realism, he should do what Humus said.

    By the way, Sebastian, if you post a link here, loads of people will try your soft !

    Eric

  9. #9
    Advanced Member Frequent Contributor
    Join Date
    Feb 2000
    Location
    London
    Posts
    503

    Re: 3D collision in OpenGL

    Bob, I think the misunderstanding arises from different assumptions. You're assuming a very high resolution heightfield (or very low resolution collision detection).

    Suppose you're using 10-metre sampling, so each "pixel" of your heightmap represents a 10x10 metre area of terrain. For a polygonal terrain rendering scheme, these sample points are the vertices of triangles.

    The point is that the heightfield values only represent 'true' height at the exact sampling points, e.g. at {0,0}, {10,20}, {430,60} etc. At intermediate points (e.g. {5.1, 3.7}) you have to interpolate the true height from the three vertices of the triangle you're over, which is where the plane equations come in.

  10. #10
    Senior Member OpenGL Guru Humus's Avatar
    Join Date
    Mar 2000
    Location
    Stockholm, Sweden
    Posts
    2,345

    Re: 3D collision in OpenGL

    Yes, Eric & MikeC are right ... but i did never mean Bob was wrong. Which method is best varies with the need. A heightmap can increase the performance a lot, but could need a lot of memory if the terrain is built of many polys or is heavily sloping at times. The method i proposed will always give an exact y value, but it may need some extra calculations.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •