Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Xnin

Very simple collision detection?

This topic is 5729 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi I want to add some collision detection to my game (top view car game) but im not sure how i can go about doing it. How do i check if a car has hit some object? Ive read allot of tutorials and they seem to be too difficult. All i want is the most simple method to doing it. If anyone knows of tutorials or knows of a way please post it. No need to be realistic or anything like that Thanks

Share this post


Link to post
Share on other sites
Advertisement
Well, you can model all your objects as circles in a 2d plane. Then, you can test to see if they intersect by checking if the distance between 2 objects is less than the sum of their radii.

Check out this thread for a little more information.

If circles are too simplistic, you can model them as rectangles also...it will make collision detection a little more complex though.

Share this post


Link to post
Share on other sites
So u mean, instead of drawing my car as a rectangle the texture mapping it, i must draw a circle the map a texture over it? And the n calculate distances, radii etc...? If thats the case then whats the function that draws circles in opengl? And what about TMapping a circle? How will that be done?

Thanks for your help btw

Share this post


Link to post
Share on other sites
No no, you can still draw it as a rectangle, but treat it as a circle for collision detection purposes. It won''t be the most accurate thing in the world, but it''ll be a good start.

Share this post


Link to post
Share on other sites
Yep, i thought so. Thanks allot for your mighty quick replies :D

argh...Whats wrong with my speeling today?

[edited by - xnin on March 31, 2003 1:16:53 AM]

Share this post


Link to post
Share on other sites
Here''s some simple collision detection I use in my code, not tested it a lot, but it works for sure. Also you''ll be missing comments, but it isn''t so hard to figure out yourself.

X, Y, Z = coordinates of object
W, H, D = width, height and depth (x,y,z) of collision box
R = radius of collision sphere

these are indexed 1 or 2 depending on the object.


Marty



int CCollisionDetect:ointSphere(float X1, float Y1, float Z1,
float X2, float Y2, float Z2, float R2)
{
if ((float) sqrt((X1-X2)*(X1-X2) +
(Y1-Y2)*(Y1-Y2) +
(Z1-Z2)*(Z1-Z2)) > R2) {return 0;}
return 1;
}

int CCollisionDetect:ointBox( float X1, float Y1, float Z1,
float X2, float Y2, float Z2, float W2, float D2, float H2)
{
if (X1 > X2 + (float)(W2/2)) {return 0;}
if (X1 < X2 - (float)(W2/2)) {return 0;}
if (Y1 > Y2 + (float)(D2/2)) {return 0;}
if (Y1 < Y2 - (float)(D2/2)) {return 0;}
if (Z1 > Z2 + (float)(H2/2)) {return 0;}
if (Z1 < Z2 - (float)(H2/2)) {return 0;}
return 1;
}

int CCollisionDetect::SphereSphere( float X1, float Y1, float Z1, float R1,
float X2, float Y2, float Z2, float R2)
{
return PointSphere(X1,Y1,Z1,X2,Y2,Z2,(R1+R2));
}

//box2 shouldn''t fit in box1!

int CCollisionDetect::BoxBox( float X1, float Y1, float Z1, float W1, float D1, float H1,
float X2, float Y2, float Z2, float W2, float D2, float H2)
{
if (PointBox(X1+(float)(W1/2), Y1+(float)(D1/2), Z1+(float)(H1/2),
X2, Y2, Z2, W2, D2, H2)) {return 1;}
if (PointBox(X1-(float)(W1/2), Y1-(float)(D1/2), Z1-(float)(H1/2),
X2, Y2, Z2, W2, D2, H2)) {return 1;}
if (PointBox(X1-(float)(W1/2), Y1+(float)(D1/2), Z1+(float)(H1/2),
X2, Y2, Z2, W2, D2, H2)) {return 1;}
if (PointBox(X1+(float)(W1/2), Y1-(float)(D1/2), Z1+(float)(H1/2),
X2, Y2, Z2, W2, D2, H2)) {return 1;}
if (PointBox(X1+(float)(W1/2), Y1+(float)(D1/2), Z1-(float)(H1/2),
X2, Y2, Z2, W2, D2, H2)) {return 1;}
if (PointBox(X1-(float)(W1/2), Y1-(float)(D1/2), Z1+(float)(H1/2),
X2, Y2, Z2, W2, D2, H2)) {return 1;}
if (PointBox(X1+(float)(W1/2), Y1-(float)(D1/2), Z1-(float)(H1/2),
X2, Y2, Z2, W2, D2, H2)) {return 1;}
if (PointBox(X1-(float)(W1/2), Y1+(float)(D1/2), Z1-(float)(H1/2),
X2, Y2, Z2, W2, D2, H2)) {return 1;}
return(0);
}

//sphere shouldn''t fit in box!

int CCollisionDetect::SphereBox( float X1, float Y1, float Z1, float R1,
float X2, float Y2, float Z2, float W2, float D2, float H2)
{
if (PointSphere( X2+(float)(W2/2), Y2+(float)(D2/2), Z2+(float)(H2/2),
X1, Y1, Z1, R1)) {return 1;}
if (PointSphere( X2-(float)(W2/2), Y2-(float)(D2/2), Z2-(float)(H2/2),
X1, Y1, Z1, R1)) {return 1;}
if (PointSphere( X2-(float)(W2/2), Y2+(float)(D2/2), Z2+(float)(H2/2),
X1, Y1, Z1, R1)) {return 1;}
if (PointSphere( X2+(float)(W2/2), Y2-(float)(D2/2), Z2+(float)(H2/2),
X1, Y1, Z1, R1)) {return 1;}
if (PointSphere( X2+(float)(W2/2), Y2+(float)(D2/2), Z2-(float)(H2/2),
X1, Y1, Z1, R1)) {return 1;}
if (PointSphere( X2-(float)(W2/2), Y2-(float)(D2/2), Z2+(float)(H2/2),
X1, Y1, Z1, R1)) {return 1;}
if (PointSphere( X2+(float)(W2/2), Y2-(float)(D2/2), Z2-(float)(H2/2),
X1, Y1, Z1, R1)) {return 1;}
if (PointSphere( X2-(float)(W2/2), Y2+(float)(D2/2), Z2-(float)(H2/2),
X1, Y1, Z1, R1)) {return 1;}
return(0);
}

Share this post


Link to post
Share on other sites
I am quite interested in that.
After a collision has been detected, there should be some sort of feedback. This is collision response, as many of you would probably have got.


Here''s an example: the playing character (FPS) is walking and it hits a wall.
When it hits the walls, the speed "going out" the wall is simply lost (or maybe not) while, when it hits the floor (almost every frame) the speed "falling under it" is converted in foward speed.
Something similar may be necessary for swimming things but this is not the point.

The point is, simply put, have you thought at what you shall do after a collision has been detected? I currently have but I didn''t find a good response so I would like to know other ppl''s ideas.

Thank you!

Share this post


Link to post
Share on other sites
I''ve got static objects and dynamic objects.

- The static ones just have a model a location and a rotation.
- The dynamic ones also have a mass, bouncyness, an axial speed and a rotational speed and can have forces and moments applied on them.

When a collision happenes I calculate the moments and forces that work on the object and apply them to the object. Ofcourse your object will stop when it hits the wall, since the force calculated when the object hits a wall is exactly the force to make it stop.
This is where i use the bouncyness factor. It tells how much kinetic energy is restored when two objects collide.
I haven''t implemented friction yet, but you can easilly multiply the speed by some number smaller than 1 and substract it from the original speed for air/fluid friction. Contact friction is a lot harder though.

Marty

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by Krohm
The point is, simply put, have you thought at what you shall do after a collision has been detected? I currently have but I didn''t find a good response so I would like to know other ppl''s ideas.

Thank you!


Basicly, what im doing is if you hit the wall at a certain velocity then multiply the velocity * - 0.5. This will cause my car to move the opposite direction at half the speed. I know this has nothing to do with physics and stuff but all i want is to get it to work. :D


BTW Marty666, thanks for the code, ill have a look into that

Share this post


Link to post
Share on other sites
I'm not sure if this will work well for cars... But if, for example, a ball hit a wall you would want to get the angle at which it hit the wall, and reflect that around the walls normal:


  
---------
/|*
/ | *
( ) | *
*
*


------- is the wall

( ) is the ball

/ is the balls velocity
| is the normal to the wall
* is the new velocity of the ball

[edited by - James Trotter on April 1, 2003 10:59:09 AM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!