Original Post
hello everyone. this is my first post on the gamedev forums. ive recently started to code a smaller 3d application, and looked around the net after collision detection mechanisms. i have found this, and tried to implement the code in C (as the application is written in C. now my problem comes from the checkPointInTriangle functions return part (also here, a bit modified but still C plus plus). ive made a C code but im not sure if it does the same: it sometimes even gives wrong results, for example for the triangle: (1,0,0) (0,1,1) (0,0,1) and point (1,1,1) it does not return 0. thank you
#include <stdio.h>
typedef float VECTOR[3];
#define VECTOR_DOT(v1,v2) (v1[0]*v2[0]+v1[1]*v2[1]+v1[2]*v2[2])
#define VECTOR_SUB(vector,v1,v2) vector[0]= v1[0]-v2[0]; vector[1]= v1[1]-v2[1]; vector[2]= v1[2]-v2[2];
typedef unsigned int uint32;
#define in(a) (*((uint32*)&a))
int check_point_in_triangle(VECTOR point, VECTOR v0, VECTOR v1, VECTOR v2){
VECTOR e1;
VECTOR e2;
VECTOR_SUB(e1,v1,v0);
VECTOR_SUB(e2,v2,v0);
float a= VECTOR_DOT(e1,e1);
float b= VECTOR_DOT(e1,e2);
float c= VECTOR_DOT(e2,e2);
float ac_bb= (a*c)-(b*b);
VECTOR vp;
VECTOR_SUB(vp,point,v0);
float d= VECTOR_DOT(vp,e1);
float e= VECTOR_DOT(vp,e2);
float x= (d*c)-(e*b);
float y= (e*a)-(d*b);
float z= x+y-ac_bb;
return ((in(z)& ~(in(x)|in(y)))& 0x80000000);
}