
it isn't working right, i'm getiing collisions detected where they shouldn't be and not where they should, the vertices are in ccw front ( .x style).
the collision areas seem to be projecting off from the model like a bow-tie shape (or a few of them)
edit: it's like some collision plains are not being limmited to the triangle and aothers are just not being detected
if i disable the checking if it is in the triangle then it finds every collision correctly (with all the hundreds of extras)
i disabled it checking which side it was on to allow back polygon collision.
int CheckLineTri (float vert1x,float vert2x,float vert3x, float px, float pvx, float vert1y,float vert2y,float vert3y, float py, float pvy, float vert1z,float vert2z,float vert3z, float pz, float pvz ,float normalx, float normaly, float normalz, float &intersectx, float &intersecty,float &intersectz )
{
normalx = (vert2y-vert1y)*(vert3z-vert1z) - (vert2z-vert1z)*(vert3y-vert1y);
normaly = (vert2z-vert1z)*(vert3x-vert1x) - (vert2x-vert1x)*(vert3z-vert1z);
normalz = (vert2x-vert1x)*(vert3y-vert1y) - (vert2y-vert1y)*(vert3x-vert1x);
//p->x p->y p->z is the start point of the line
//v->x v->y v->z is the vector from start point to end point
//p+v gives the end point
float vx=pvx-px;
float vy=pvy-py;
float vz=pvz-pz;
//check the points distance from the plane
float d=-normalx*vert1x-normaly*vert1y-normalz*vert1z;
float dist1 = normalx*(pvx) + normaly*(pvy) + normalz*(pvz) + d;
float dist2 = normalx*px + normaly*py + normalz*pz + d;
// >0 means the point is on the visible side of the plane
// <0 means the point is on the back side of the plane
// if start point is behind the plane, or the end point is in front
// of the plane, ignore the point (can't collide with back of polygon)
if ((dist2<-0.00001 && dist1<-0.00001)||(dist2>0.00001 && dist1>0.00001)) return false;
//use dot product to work out the distance along the line to the intersect point
dist1=-(normalx*vx) - (normaly*vy) - (normalz*vz);
if (dist1!=0) dist1=dist2/dist1;
else dist1=0;
//work out the point of intersection with the plane
intersectx = px + vx*dist1;
intersecty = py + vy*dist1;
intersectz = pz + vz*dist1;
//check if this point lies in the triangle
if (!pointInPoly(intersectx, intersecty, intersectz, vert1x, vert2x, vert3x, vert1y, vert2y, vert3y, vert1z, vert2z, vert3z, normalx, normaly, normalz)) return false;
return true;
}
bool pointInPoly(float px, float py, float pz, float vert1x,float vert2x,float vert3x, float vert1y,float vert2y,float vert3y,float vert1z,float vert2z,float vert3z,float normalx,float normaly,float normalz)
{
//Pretend each side of the polygon is a plane, check which side
//of each plane the point lies (<0 is outside)
//First side
float v3x = vert2x-vert1x;
float v3y = vert2y-vert1y;
float v3z = vert2z-vert1z;
//work out the normal to our new imaginary plane
v3x = normaly*v3z - normalz*v3y;
v3y = normalz*v3x - normalx*v3z;
v3z = normalx*v3y - normaly*v3x;
//work out the last value in the plane equation
double ld = v3x*vert1x + v3y*vert1y + v3z*vert1z;
//use the plane equation on our point and check its side
ld = v3x*px + v3y*py + v3z*pz - ld;
if (ld<-0.00001) return false;
//Second side
v3x = vert3x-vert2x;
v3y = vert3y-vert2y;
v3z = vert3z-vert2z;
v3x = normaly*v3z - normalz*v3y;
v3y = normalz*v3x - normalx*v3z;
v3z = normalx*v3y - normaly*v3x;
ld = v3x*vert2x + v3y*vert2y + v3z*vert2z;
ld = v3x*px + v3y*py + v3z*pz - ld;
if (ld<-0.00001) return false;
//Third side
v3x = vert1x-vert3x;
v3y = vert1y-vert3y;
v3z = vert1z-vert3z;
v3x = normaly*v3z - normalz*v3y;
v3y = normalz*v3x - normalx*v3z;
v3z = normalx*v3y - normaly*v3x;
ld = v3x*vert3x + v3y*vert3y + v3z*vert3z;
ld = v3x*px + v3y*py + v3z*pz - ld;
if (ld<-0.00001) return false;
return true;
}
changing the vertex order doesn't seem to help either
edit: i think i have narrowed it down to the point in poly function:
bool pointInPoly(float px, float py, float pz, float vert1x,float vert2x,float vert3x, float vert1y,float vert2y,float vert3y,float vert1z,float vert2z,float vert3z,float normalx,float normaly,float normalz)
{
//Pretend each side of the polygon is a plane, check which side
//of each plane the point lies (<0 is outside)
//First side
float v3x = vert2x-vert1x;
float v3y = vert2y-vert1y;
float v3z = vert2z-vert1z;
//work out the normal to our new imaginary plane
v3x = normaly*v3z - normalz*v3y;
v3y = normalz*v3x - normalx*v3z;
v3z = normalx*v3y - normaly*v3x;
//work out the last value in the plane equation
double ld = v3x*vert1x + v3y*vert1y + v3z*vert1z;
//use the plane equation on our point and check its side
ld = v3x*px + v3y*py + v3z*pz - ld;
if (ld<-0.00001) return false;
//Second side
v3x = vert3x-vert2x;
v3y = vert3y-vert2y;
v3z = vert3z-vert2z;
v3x = normaly*v3z - normalz*v3y;
v3y = normalz*v3x - normalx*v3z;
v3z = normalx*v3y - normaly*v3x;
ld = v3x*vert2x + v3y*vert2y + v3z*vert2z;
ld = v3x*px + v3y*py + v3z*pz - ld;
if (ld<-0.00001) return false;
//Third side
v3x = vert1x-vert3x;
v3y = vert1y-vert3y;
v3z = vert1z-vert3z;
v3x = normaly*v3z - normalz*v3y;
v3y = normalz*v3x - normalx*v3z;
v3z = normalx*v3y - normaly*v3x;
ld = v3x*vert3x + v3y*vert3y + v3z*vert3z;
ld = v3x*px + v3y*py + v3z*pz - ld;
if (ld<-0.00001) return false;
return true;
}
edit: it seems that there are a few triangles that it detects collisions with ignoring all the others and not limiting the triangle to it's boundaries correctly in certain directions.
http://www.larinar.tk
AMD athlon 64 3000+, 512mb ddr400, abit kv8, 160gb hdd, gigabit lan, ati radeon 9800se 128mb.