Archived

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

G a m e D e v

collision detection with circle

Recommended Posts

johnb    351
quote:
Original post by G a m e D e v
How do I detect a collision with two circles? How do I tell what angle they hit?


If the circles have radiuses r1 and r2, then they hit when their centres are a distance (r1 + r2) apart. You can work out the distance using Pythagoras''s theorem, i.e.

dist = sqrt((x1 - x2)2 + (y1 - y2)2)

where they are at (x1, y1) and (x2, y2)

And they are overlapping, i.e. they have hit, when

dist < r1 + r2

The vector

(x1 - x2)
(y1 - y2)

gives the direction between the circles. There are various angles you can measure, e.g. the angle between this direction and the x axis is given by

angle = arctan ((y1 - y2) / (x1 - x2))

Alternately you can just use the vector as this gives you the direction in a format that can be easily used elsewhere. E.g .the force between the circles if they bounce apart will be along this direction.

Share this post


Link to post
Share on other sites