Collision Detection

Started by
11 comments, last by Daricon 23 years, 11 months ago
me? i thought it was simple enough, but maybe not...say somewhere in the object struct or class you have a variable like

float CollisionRadius;

then assign it a value so that the distance between the center of the object and the farthest edge is contained in the circle, like

Object1.CollisionRadius = 5;

do that for all objects...ie

Object2.CollisionRadius = 5;

then compare their distance

using the distance formula (i think this is it)

Dist = sqrt((Object1.x - Object2.x)^2 + (Object1.y - Object2.y)^2)

and add the radii:

Sum = Object1.CollisionRadius + Object2.CollisionRadius;

Then if Dist < Sum, a collision has occured, else they haven't hit each other




<(o)>

Edited by - aDasTRa on 4/26/00 11:29:52 PM
<(o)>
Advertisement
aDastra: you''re correct, but what you described is about what Whatever already described in his 2nd post.

Daricon, that''s correct, X and Y are just the separation distances between the centers of the two circles. Also, I thought of a simple alternative to the code I gave earlier: If you detect a collision, you could actually just switch the two circles'' velocities. Here''s the code:

if(collisionDetected){
FLOAT tempX = circle1.velX;
FLOAT tempY = circle1.velY;
circle1.velX = circle2.velX;
circle1.velY = circle2.velY;
circle2.velX = tempX;
circle2.velY = tempY;
}

Note the following limitations: your collision will be 100% elastic, which looks particularly retarded in head-on collisions. Also, my previous code could be modified easily to simulate circles of different masses, whereas this "pseudo-physics" solution can only treat them as equal masses.
Eric:

I tried that new code. It seems to work pretty well in some cases, but it gets funky in others. I think the problem has to do with this part:
circle1.velX += unitSepX * collisionVelocity;circle1.velY += unitSepY * collisionVelocity; circle2.velX -= unitSepX * collisionVelocity;circle2.velY -= unitSepY * collisionVelocity;  


Let's say that circle1 is on the left and has a positive X velocity and circle 2 is one the right and has a neagtive one. This is assuming that +Xvelocity moves the circle to the right and -Xvelocity to the left. Forget about Y velocity for a second. It works in this case and they both bounce off each other (a little slower) in opposite directions. The funky stuff starts if circle2 in left with a +Xvel and circle1 is on the right with a -Xvel. They sort of eat into each other and don't bounce off right.

How did you get past this? I haven't done it yet, but I guess I will see where the circles are before the collision and then have some if statements to decide whether I do
circle1.velX += unitSepX * collisionVelocity; 

or
circle1.velX -= unitSepX * collisionVelocity; 

and the same for the other three lines.

Anyway, thanks Eric. You've been most helpful.

::Daricn::

Edited by - Daricon on 4/27/00 8:35:50 PM

This topic is closed to new replies.

Advertisement