Jump to content
  • Advertisement

Archived

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

moucard

collision detection between two circles

This topic is 5223 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 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 this post


Link to post
Share on other sites
Advertisement
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.

[edit] typo.

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

Share this post


Link to post
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 this post


Link to post
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.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!