• Advertisement

Archived

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

collisions

This topic is 5574 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

you know in a game like unreal tournament how do they do collision detection? also how do you check collisions with a 3d triangle? also can someone please please show me some base code that opens a window and sets up and uses direct input. i've been trying to get direct input working in my game for a long time but it want work. [edited by - hello_there on October 20, 2002 4:22:09 PM]

Share this post


Link to post
Share on other sites
Advertisement
as far as the dinput goes, check out a little wrapper class I wrote, it''s on my website. here''s a little snippet I don''t really remember where it''s from, but it''s an example of ray-triangle(plane) intersection and finds the point where it intersects.

  
#define EPSILON 0.000001
#define CROSS(dest,v1,v2) \
dest[0]=v1[1]*v2[2]-v1[2]*v2[1]; \
dest[1]=v1[2]*v2[0]-v1[0]*v2[2]; \
dest[2]=v1[0]*v2[1]-v1[1]*v2[0];
#define DOT(v1,v2) (v1[0]*v2[0]+v1[1]*v2[1]+v1[2]*v2[2])
#define SUB(dest,v1,v2)
dest[0]=v1[0]-v2[0]; \
dest[1]=v1[1]-v2[1]; \
dest[2]=v1[2]-v2[2];
/*
We present a clean algorithm for determining whether a ray intersects a triangle.
The algorithm translates the origin of the ray and then changes the base to yield a vector (t u v)T, where t is the
distance to the plane in which the triangle lies and (u,v) represents the coordinates inside the triangle.
*/
int
intersect_triangle(double orig[3], double dir[3],
double vert0[3], double vert1[3], double vert2[3],
double *t, double *u, double *v)
{
double edge1[3], edge2[3], tvec[3], pvec[3], qvec[3];
double det,inv_det;

/* find vectors for two edges sharing vert0 */
SUB(edge1, vert1, vert0);
SUB(edge2, vert2, vert0);

/* begin calculating determinant - also used to calculate U parameter */
CROSS(pvec, dir, edge2);

/* if determinant is near zero, ray lies in plane of triangle */
det = DOT(edge1, pvec);

#ifdef TEST_CULL /* define TEST_CULL if culling is desired */
if (det < EPSILON)
return 0;

/* calculate distance from vert0 to ray origin */
SUB(tvec, orig, vert0);

/* calculate U parameter and test bounds */
*u = DOT(tvec, pvec);
if (*u < 0.0 || *u > det)
return 0;

/* prepare to test V parameter */
CROSS(qvec, tvec, edge1);

/* calculate V parameter and test bounds */
*v = DOT(dir, qvec);
if (*v < 0.0 || *u + *v > det)
return 0;

/* calculate t, scale parameters, ray intersects triangle */
*t = DOT(edge2, qvec);
inv_det = 1.0 / det;
*t *= inv_det;
*u *= inv_det;
*v *= inv_det;
#else /* the non-culling branch */
if (det > -EPSILON && det < EPSILON)
return 0;
inv_det = 1.0 / det;

/* calculate distance from vert0 to ray origin */
SUB(tvec, orig, vert0);

/* calculate U parameter and test bounds */
*u = DOT(tvec, pvec) * inv_det;
if (*u < 0.0 || *u > 1.0)
return 0;

/* prepare to test V parameter */
CROSS(qvec, tvec, edge1);

/* calculate V parameter and test bounds */
*v = DOT(dir, qvec) * inv_det;
if (*v < 0.0 || *u + *v > 1.0)
return 0;

/* calculate t, ray intersects triangle */

*t = DOT(edge2, qvec) * inv_det;
#endif
return 1;
}

I don''t claim this code, but I did use it to write my collision detection stuff. hope it helps.

My Homepage
How many Microsoft employees does it take to screw in a light bulb?
None, they just declare drakness as a new standard.

Share this post


Link to post
Share on other sites

  • Advertisement