#### Archived

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

# collision detection between two circles

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

## Recommended Posts

Hi to everyone. My problem today is to perform a small collision detection between 2 circles. I know when the two circles touch. Say K1 is the center of the firstm R1 is the range and K2, R2 the same for the second circle. Say as well that D(K1, K2) is the distance between the 2 circle centers. Then I know that they touch (or one circle partially hides the other) when: D(K1, K2) <= R1 + R2. So far so good. But my problem lies in that I don''t want the second ball (say the first is not moving, the second follows the cursor) to move inside the first ball. I want it to stick to its perimeter (but still be able to move away). I just don''t want it to partially hide. Any ideas on how I can do this?

##### Share on other sites
If a collision occurs:

p1,p2 = position of ball 1,2 :vector (point)
r1,r2 = radius of ball 1,2 : float

v1 : vector
//get a normalised direction vector
v1 = p2 - p1 :vector from p1 to p2
length = sqrt(v1.x * v1.x + v1.y * v1.y);

//normalise
v1.x = v1.x / length
v1.y = v1.y / length

//now move the position of ball 2 to edge
p2 = v1 * (r1+r2) + p1.

 typo.

[edited by - SoulSpectre on June 3, 2004 6:19:13 AM]

##### Share on other sites
If one circle is stationary and the other is moving:

Get the next movement of the circle.
Work out what the next position will be.
If the distance between the centres is less than the two radii then they''ll overlap.
Scale the movement so that it will bring the circle to the edge of the other circle.
Make the movement!

If both circles are moving you can either treat them in turn as above or work out their collision point. It is simpler to just assume one circle is stationary.

##### Share on other sites
Thank you for your answers. Both were very good and insightful. I managed to get the first solution working. Only to find out that I didn''t understand my problem that well. I needed another thing as well but with some modifications in my source I managed to get it working! Thank you both again! You just made me a happy man.

1. 1
Rutin
40
2. 2
3. 3
4. 4
5. 5

• 18
• 20
• 14
• 14
• 9
• ### Forum Statistics

• Total Topics
633368
• Total Posts
3011531
• ### Who's Online (See full list)

There are no registered users currently online

×