#### Archived

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

# Collision Detection

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

## Recommended Posts

I need help with circle collision detection! I don't really know how to get started. I know if use the pythagorean (sp?) theroem, I can figure out the length between two circles and then see if they are touching, but I have no idea where to go with the whole physics of it! Does anyone have any code that would do what I need, or at least get me in the right direction? If not that, any links? ::Daricon:: Edited by - Daricon on 4/25/00 8:16:17 PM

Little help?

::Daricon::

##### Share on other sites
Here ya go:

inline float VectorLength2d(pvector2d v)
{
return (float)sqrt(v[0]*v[0] + v[1]*v[1]);
}

sqrt isn''t real fast, but it''ll do the job

##### Share on other sites
To make the code above a little easier to read, the formula is:

Length=sqrt(X*X + Y*Y);

Silly me, I didn''t read the whole problem. To see if 2 circles are touching, you do this:

//gets distance petween Pos1 and Pos2 and stores it into Vector
VectorSubtract2d(Vector, Pos1, Pos2);
//gets the length of between Pos1 and Pos2 thru Vector
VectorLength=VectorLength2d(Vector);

{
return true; //collision detected
}

##### Share on other sites
You might not need to do the square root at all. Your final comparison could be something like:

here, VectorLengthSquared = X*X + Y*Y, and TotalHitRadiusSquared would probably be a constant. I just know that sqrt() is pretty computationally expensive.

Daricon, I think you were also wondering what to do after a collision has occurred. I assume your two circles have velocities, which need to change after the collision. Here''s some code:

if(collisionDetected){
// the actual length is needed now
FLOAT VectorLength = sqrt(VectorLengthSquared);

// make separation distances into unit vectors
// you might consider checking that VectorLength != 0 1st
FLOAT unitSepX = X / VectorLength;
FLOAT unitSepY = Y / VectorLength;

// This is how fast the circles are hitting each other.
// basically, I''m calculating 2 dot products and adding
// them.
FLOAT collisionVelocity =
(unitSepX * -circle1.velX +
unitSepY * -circle1.velY) +
(unitSepX * circle2.velX +
unitSepY * circle2.velY) +

// include this if you want a slightly inelastic collision
collisionVelocity *= .75;

// you may have to change the signs here
circle1.velX += unitSepX * collisionVelocity;
circle1.velY += unitSepY * collisionVelocity;
circle2.velX -= unitSepX * collisionVelocity;
circle2.velY -= unitSepY * collisionVelocity;
}

##### Share on other sites
The simplest things are always the hardest to find when you''re not looking . I made that up just now.

That''s a very good idea Eric. I will use that method in my next game, or possibly incorporate it into my current game to see the speed improvement :D.

##### Share on other sites
I guess I should have called the post "Collision Response" or something like that.

WhatEver:
Now let me get this straight, is the pvector2d a type that is already defined or something? If it''s not, what is it then? And the same thing for VectorSubtract2d(). Is that something MSVC is going to know about?

Eric:
Where are you defining X and Y? I''m a little confused! Does X = circle2.X - circle1.X, and the same for Y? That''s about all I can guess. That''s a good idea about comparing leaving the length squared until you really need it.

This has been good stuff so far. Thanks you two. Please try to get back to me!

::Daricon::

##### Share on other sites
Daricon, pvector2d is a typedef from a float*. Like this:

typedef float vector2d[2];
typedef float *pvector2d;

I declared all my vector routines.

##### Share on other sites
ummm, why don't you just save the radius length of each particular bounding sphere or circle. Then compare the distance between the two objects with the sum of their radii. If the radii sum is more, no collision has occured. If it is less, then a collision MAY have occured. You could preform further tests to increase accuracy, or just say "screw it, they hit each other" and run with that...

<(o)>

Edited by - aDasTRa on 4/26/00 11:09:57 PM

##### Share on other sites
I''m bad with math terms, can you show me what you mean in code form? I''m always open to faster ways of doing things .

• 10
• 33
• 29
• 9
• 15