Skip to main content
GameDev.net gamedev.net
🔒 Locked

Problem with collision detection

Started by edgarllorente Jun 25, 2009 at 5:06 PM 1 replies 750+ views
Original Post
edgarllorente
edgarllorente
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.
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;
}
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 =)
Zakwayda
Zakwayda
Although it's a bit messy, your code looks correct to me. The only thing I'm wondering about is this:
origen->A*z+origen->B*x+origen->C
Are you sure that A corresponds to the z axis and B to the x axis?

Also, this sort of thing is a lot easier to read, write, and debug when you use a proper vector class, and factor out common code such as dot products and so on. Also, this is just my opinion, but when you have a 3-d simulation with a strong 2-d component, I find it much more intuitive to make the z axis 'up' so that the simulation takes place in the XY plane rather than the XZ plane (I find this can also simplify things code-wise).
medevilenemy
medevilenemy
I concur with jyk, besides messy code it looks ok. Though I do think that method is a bit more complex than it needs to be. No worries, though. jyk, for one, has written a lot of infinitely useful math code which makes all sorts of physical stuff so much easier (collisions, rotation, etc).
There was a saying we had in college: Those who walk into the engineering building are never quite the same when they walk out.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.