With monday.comâ€™s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Hi!

I'm currently working on a 3D car racing game in C adn DirectX but having trouble with the collision detection. I am using OOBs to detect collisions between the cars but cannot determine the intersection point. I am currently working on a 2D level and ignoring the y component. Admittedly I am not a very strong C programmer! I would appreciate any advice on this subject, any alternative methods of implementing collision detection or any good sites. Thank you very much!

I'm currently working on a 3D car racing game in C adn DirectX but having trouble with the collision detection. I am using OOBs to detect collisions between the cars but cannot determine the intersection point. I am currently working on a 2D level and ignoring the y component. Admittedly I am not a very strong C programmer! I would appreciate any advice on this subject, any alternative methods of implementing collision detection or any good sites. Thank you very much!

int Collision_Test(int x1, int y1, int w1, int h1,

int x2, int y2, int w2, int h2)

{

// this function tests if the two rects overlap

// get the radi of each rect

int width1 = (w1>>1) - (w1>>3);

int height1 = (h1>>1) - (h1>>3);

int width2 = (w2>>1) - (w2>>3);

int height2 = (h2>>1) - (h2>>3);

// compute center of each rect

int cx1 = x1 + width1;

int cy1 = y1 + height1;

int cx2 = x2 + width2;

int cy2 = y2 + height2;

// compute deltas

int dx = abs(cx2 - cx1);

int dy = abs(cy2 - cy1);

// test if rects overlap

if (dx < (width1+width2) && dy < (height1+height2))

return(1);

else

// else no collision

return(0);

} // end Collision_Test

One way of doing it might be to represent each of the rectangles using a transformation matrix which transforms a unit box. This matrix should take into account the orientation and location of the car, and the scale of the bounding box.

Then to do the collision detection between car A and car B, transform A's matrix by the inverse of B's matrix. You will then have a matrix which defines A in terms of B. Next transform a unit box by this matrix. This box represents car A in relation to car B. So you can now do four highly optimised individual line intersection tests, by testing the four lines of this box against an axis aligned unit box at the origin.

If you're having trouble understanding my description let me know.

In 3d, my first sentence isn't correct: sometimes its a line on object A hitting a line on object B or a vertex on object A sticking into a face on object B.

we are testing object A against object B. First we would want to make sure that A and B are both expressed in the same base. You

could transform all vertices to worldspace, but as mentioned above, it would be smarter to transform the object with the least

vertices to the base of that with the most vertices.

Once all vertices are in the same space, we would have to :

- repeat for all edges of A

- repeat for all polygons of B

- P = plane equation build from three points of polygon from B

- find point of intersection between line A and plane P, will call this point c. Check if c is between the two vertices of A. (0 < (c - A0)/(A1-A0) < 1)

- for all two vertices of B take angle between c and these vertices. Accumulate all these angles.

- if the last sum is 2*pi or 360 degrees, the objects collide. (edge from A collides in poly from B at point c)

This will only work for convex polygons though ..

as far as maths is concerned, it's all really basic :

- finding plane equation P(a,b,c,d) from V1(x,y,z) V2(x,y,z) and V3(x,y,z) :

P(a,b,c) = normalize( V2-V1 dot V3-V1)

P(d) = - (P(a,b,c) dot V1)

- finding the intersection between P(a,b,c,d) and the linepiece through V1(x,y,z) and V2(x,y,z)

temp = - ( (p dot v1 + d) / (p dot (v2-v1)) )

if temp < 0 or temp > 1 then intersection before V1 or after V2 = no collision .. fast bail ..

C(x,y,z) = V1 + temp * (v2-v1)

- is C(x,y,z) inside the original polygon ? (it is in the plane for sure ..) the polygon is v1(x,y,z) upto vn(x,y,z)

for (i=0, sum=0; i < n; i++)

{

sum += arccos( (v[i] - C) dot (v[i+1] - C))

}

if sum = 2*pi -> collision !!

All Courses

From novice to tech pro — start learning today.

http://www.gamedev.net/reference/list.asp?categoryid=45