Original Post
Hello, I am making an application in C++ with OpenGL, by the moment my program loads the worls and it's objects so now I'm trying to implement the collision detection with the walls. The method I am aplying now is to calculate the distance between the camera and the line that every wall forms, by the way this system has a great problem and it is that detects a collision all the way the line actually the wall forms, so I tryed to make the distance between a segment and a point, but it doesn't work. First the function "distancia" calculates the distance between the camera and the line that the wall form, then "u" is one formula that I read in a tutorial that it's suposed to return a value between 0 and 1 if the especificated point is inside the segment both points of the wall forms, but it doesn't work. Can someone help me please? Thanks a lot =)
struct segmento {
float x1, z1, x2, z2;
float A, B, C;
};
struct colisiones {
segmento *colision;
int numcolisiones;
};
float distancia(float x, float z, segmento *origen) {
return (origen->A*z+origen->B*x+origen->C)/sqrt( (pow(origen->A, 2) + pow(origen->B, 2)) );
}
int comprobar_colision(float x, float z, colisiones *origen) {
float u;
for (int a = 0; a < origen->numcolisiones; a++) {
if (fabs(distancia(x, z, &origen->colision[a])) < 0.2) {
float x1 = origen->colision[a].x1;
float x2 = origen->colision[a].x2;
float z1 = origen->colision[a].z1;
float z2 = origen->colision[a].z2;
u = ( (x-x1)*(x2-x1)+(z-z1)*(z2-z1) ) / pow((x2-x1),2)+pow((z2-z1),2);
if (u>0 && u<1) return 1;
}
}
return 0;
}