Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

jalaine

Some collision detection problems..

This topic is 6541 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi! I''ve been coding a very simple futuristic racing game with openGL, in which the space ship is supposed to float a little above the road. The ship is a triangle and I thought I''d do it so that I have a normal vector in each corner pointing down to the road and then rotate the ship so that the ship stays on the road and moves up and down when there are up- and downhills.. Am I completely wrong? Anyway, how should I do this, and how can I check if there is a road polygon under the ship.. If you have any ideas, I''d be very happy to hear them. Thanks! Jarkko

Share this post


Link to post
Share on other sites
Advertisement
If your terrain triangles always point upwards you can do it quite simply with a ''Point-in-triangle'' test.

Just consider the triangle in 2d (x &z) as you look straight down on it. Finding the height (y) for any x & z within the triangle is also quite easy.

This approach will only work as long as your ground triangles don''t go steeper than 90 degrees. If they do they are no-longer visible when looking straight down from above.

Need more?

Matt



Check out my project at:www.btinternet.com/~Matthew.Bennett

Share this post


Link to post
Share on other sites
Okay.. Let's see.. Could you be more specific? My terrain triangles all are in less than 90 degree angles, but they have different angles. Can I use your approach? What about, how can I find out at what point (x,y,z) a line and a triangle cross eachother (I mean, the line goes through the triangle)?

Thanks for your help, but I think I still need more I'm quite new to this, you know..

If you 'd like to see the game (it might give you some more idea on what I'm trying to do) and the code, you can get it at http://www.jyu.fi/~jalaine/ on the download page.. Or directly here: http://www.jyu.fi/~jalaine/download/game.zip

Thanks,

Jarkko

Edited by - jalaine on June 25, 2000 5:05:03 PM

Share this post


Link to post
Share on other sites
Hi, I'm doing the same thing! Here's some point-in-triangle and triangle-following code. It uses D3D structures, but both D3DVERTEX and D3DVECTOR are just 3D points anyway.

        #include <math.h>

//.....


//////////////////////////////////////////////////////////////////////////

// Function to get slope of a line


inline float Slope(D3DVERTEX p1, D3DVERTEX p2)
{
return (p1.z-p2.z)/(p1.x-p2.x);
}

//////////////////////////////////////////////////////////////////////////

// Checks if B is between A and C


inline bool IsBetween(float a, float b, float c)
{
// check for between-ness

if( a <= b && b <= c ) return true;
if( a >= b && b >= c ) return true;

// nothing, bail
return false;
}

//////////////////////////////////////////////////////////////////////////

// Checks whether point D is within triangle ABC


bool WithinTriangle(D3DVERTEX a, D3DVERTEX b, D3DVERTEX c, D3DVERTEX d)
{
// slopes of involved segments

float ab = Slope(a,b),
ac = Slope(a,c),
ad = Slope(a,d),
bc = Slope(b,c),
bd = Slope(b,d);

// do first check on slopes

if( !IsBetween(ab, ad, ac) ) return false;

// second check

if( !IsBetween(ab, bd, bc) ) return false;

// all OK, return true
return true;
}

//////////////////////////////////////////////////////////////////////////

// Returns the Y component of an X,Z position on a triangle's
//plane. The triangle is defined by points a, b and c. The
//triangle's normal vector is calculated into r.


FLOAT FollowTriangle(FLOAT X, FLOAT Z, D3DVERTEX a, D3DVERTEX b, D3DVERTEX c, D3DVECTOR *r)
{
FLOAT X1 = a.x, X2 = b.x, X3 = c.x,
Y1 = a.y, Y2 = b.y, Y3 = c.y,
Z1 = a.z, Z2 = b.z, Z3 = c.z,

A = Y1 * (Z2-Z3) + Y2 * (Z3 - Z1) + Y3 * (Z1 - Z2),
B = Z1 * (X2-X3) + Z2 * (X3 - X1) + Z3 * (X1 - X2),
C = X1 * (Y2-Y3) + X2 * (Y3 - Y1) + X3 * (Y1 - Y2),

D = -(A*X1 + B*Y1 + C*Z1),

Distance = sqrt((A*A) + (B*B) + (C*C));

r->x = A/Distance + X1;
r->y = B/Distance + Y1;
r->z = C/Distance + Z1;

return (A*X + C*Z + D)/(-B);
}


So, basically you'd first find what triangle your ship was over with WithinTriangle() and then calculate the Y position of the ship's X and Z coords on that triangle's plane with FollowTriangle(). Then you add to that Y value to make the ship float over the triangle. If you need any help, email me.

lntakitopi@aol.com / http://geocities.com/guanajam/

Edited by - SHilbert on June 25, 2000 7:11:39 PM

Share this post


Link to post
Share on other sites
I just tried the game. It looks nice! Cool effect with the way the ship rotates. Just change the title and fix the Z-buffering problems around the course's edges, and it'll be perfect.
I was going to try & see if I could fix your program just to see if I could do anything with OpenGL (I usually use D3D), but I need OpenGLEngine.h to compile. Could you email it to me or include it in the archive?

lntakitopi@aol.com / http://geocities.com/guanajam/

Edited by - SHilbert on June 25, 2000 7:07:50 PM

Edited by - SHilbert on June 25, 2000 7:09:02 PM

Share this post


Link to post
Share on other sites
Hi!

Thanks for your help, I think I can do it now. About the OpenGLEngine.h, I''ll add it to the zip.. Thanks!

Jarkko

Share this post


Link to post
Share on other sites
Whoops, that tri-testing code doesn''t work. Use this code:
    /* Slope()
-------------------------
Gets the slope between two points.
*/

inline float Slope(VERTEX p1, VERTEX p2)
{
// Avoid divide-by-zeroes by giving the slope a very

// large value in such situations.

if((p1.x-p2.x) == 0) return 10000;

// Otherwise, calculate slope normally.
return (p1.y-p2.y)/(p1.x-p2.x);
}

/* IsBetween()
-------------------------
Checks if one number is between two others.
*/

inline bool IsBetween(float a, float b, float c)
{
// check for between-ness

if( a <= b && b <= c ) return true;
if( a >= b && b >= c ) return true;

// nothing, bail
return false;
}


/* VectorTo()
-------------------------
Creates a normalized vector from one point to another.
*/

VERTEX VectorTo( VERTEX to, VERTEX from )
{
VERTEX temp = from;
temp.x -= to.x;
temp.y -= to.y;
temp.z -= to.z;
float dist = sqrt( temp.x * temp.x +
temp.y * temp.y +
temp.z * temp.z );
temp.x /= dist;
temp.y /= dist;
temp.z /= dist;
return temp;
}

/* FollowTriangle()
-------------------------
Calculates the missing Z value given X & Y values and the
face''s plane.
*/

float FollowTriangle(float X, float Y, FACE *face)
{
// This function uses the plane equation to interpolate

// points. Because your faces already have their normals computed,
// this function will be nice & fast!


float A = face->normal.x,
B = face->normal.y,
C = face->normal.z,

// Solve for D

D = -(A*face->v1.x + B*face->v1.y + C*face->v1.z);

// Solve for Z

return (A*X + B*Y + D)/(-C);
}

/* Dot()
-------------------------
Gets the dot product of two vectors
*/

float Dot( VERTEX v1, VERTEX v2 )
{
return v1.x*v2.x + v1.y * v2.y + v1.z*v2.z;
}

/* WithinTriangle()
-------------------------
Checks if point D is in triangle ABC
*/


bool WithinTriangle(VERTEX a, VERTEX b, VERTEX c, VERTEX d)
{
// Get the tri''s center

VERTEX center;
center.x = (a.x + b.x + c.x) / 3;
center.y = (a.y + b.y + c.y) / 3;
center.z = (a.z + b.z + c.z) / 3;

// Get a vector to the center and from each vertex

VERTEX to_center = VectorTo(d,center),
to_a = VectorTo(d,a),
to_b = VectorTo(d,b),
to_c = VectorTo(d,c);

// If all of the vectors are pointing in the same direction,
// then the point''s probably not in the triangle.

if( ( Dot(to_center, to_a) >= 0 ) &&
( Dot(to_center, to_b) >= 0 ) &&
( Dot(to_center, to_c) >= 0 ) ) return false;

// all OK, return true
return true;
}

...it''s custom-tailored to be compatible with jalaine''s code and the X,Z,Y coordinates. Plus, you have to call GetZPos() first to get a suitable point and THEN call WithinTriangle().

lntakitopi@aol.com | http://geocities.com/guanajam/

Share this post


Link to post
Share on other sites
Thank you very much for your help, I see you even changed your comments to match with my commenting style - great. But I still have to bother you a little more.. What do you mean by GetZPos()? I''ve been working on it for a while, but it still isn''t working.. Thanks!

Jarkko

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!