ray intersects tri new func.

Started by
3 comments, last by Pinky 24 years, 7 months ago
I forgot add this vars to the func:

PR_POINT edge1, edge2, tvec, pvec, qvec;
double det,inv_det;

------------------
Pinky.
ICQ 42026534

Pinky.
ICQ 42026534
Advertisement
The problem with this is you are not using world coordinates, but rather local vertex coordinates. If you want to add any translations, rotation, or scaling to the object it won't work.

Also if you design objects in 3D Studio you may have translations applied to them which transform the basic objects from local to world space. In that case your coordinates won't be correct by looking at the values in the PR_VERTEX structures.

I guess if it works for you and makes you happy, use it

Chris

Author of Power Render (http:/www.powerrender.com)
I am using the func to test for collistion with the terrain, I do not apply rotation etc
to the terrain, so it works.

It is very easy to alter the func, to check the translated version inside PR_VERTEX.

In fact this is what I am doing when I do collision with other objects in the world.

------------------
Pinky.
ICQ 42026534

Pinky.
ICQ 42026534
Hi

I found a func the check if ray intersects a tri at http://www.acm.org/jgt/papers/MollerTrumbore97/.
U can compile this func as inline.
T big advantage to me is that I converted it to check for collision at world space.

Here is the PR ver I created:
---------------------------------------------
#define EPSILON (double)(0.000001)

#define SIGN(x) (int)((x)/fabs(x))

#define CROSS(dest,v1,v2) \
dest.x=(double)v1.y*(double)v2.z-(double)v1.z*(double)v2.y; \
dest.y=(double)v1.z*(double)v2.x-(double)v1.x*(double)v2.z; \
dest.z=(double)v1.x*(double)v2.y-(double)v1.y*(double)v2.x;

#define DOT(v1,v2) ((double)v1.x*(double)v2.x+(double)v1.y*(double)v2.y+(double)v1.z*(double)v2.z)

#define SUB(dest,v1,v2) \
dest.x=(double)v1.x-(double)v2.x; \
dest.y=(double)v1.y-(double)v2.y; \
dest.z=(double)v1.z-(double)v2.z;


//#define TEST_CULL
inline bool bRayIntersectTriangle(
PR_RAY& ray,
PR_VERTEX& vert0, PR_VERTEX& vert1, PR_VERTEX& vert2,
float *t, double *u, double *v)
{
/* 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, ray.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 false;

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

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

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

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

/* 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 false;
inv_det = 1.0 / det;

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

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

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

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

/* calculate t, ray intersects triangle */
*t = DOT(edge2, qvec) * inv_det;
#endif
if((*t)<=ray.len&&(*t)>=0) return true; else return false;
}


------------------
Pinky.
ICQ 42026534

Pinky.
ICQ 42026534
Why do you need triangle collisions for the terrain? It's much faster to look at the height value at (x,z) on the terrain and see if the object is below the height.

I have a new example in PR that uses the terrain engine and mixes it with object collision, and the two methods are completely separate. For example I check for all collisions with buildings and then make sure the player (or other moving object) is above the ground as well.


Author of Power Render (http:/www.powerrender.com)

This topic is closed to new replies.

Advertisement