#### Archived

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

# Collision Detection

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

## Recommended Posts

me? i thought it was simple enough, but maybe not...say somewhere in the object struct or class you have a variable like

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

do that for all objects...ie

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)

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

##### Share on other sites
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.

##### Share on other sites
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

• 17
• 19
• 24
• 10
• 21