Sunday 20 January 2013

Simple Circle Collision Detection

Circle are pretty popular. From basketballs and billiard balls to bullseye targets and frisbees, there are plenty of games where circles play an important role.
But unless we're dealing with soap bubbles, we need a way to tell the computer when the circles are colliding.

If we take the Equation of the Circle (with "r" being the radius and "C" the center of the circle) we can easily determine if a point (P) is inside the circle:

( Px - Cx )2 + ( Py - Cy )2 <= r2





But if we want to detect the collision between 2 circles, it's more intuitive to calculate the distance between the centers (C1 and C2) first and then check if it's smaller than the sum of the radius (r1 and r2):

distance = √(( C2x - C1x)2 + ( C2y - C1y)2)

if (  distance  <=  r1 + r2 ){
     // The circles are colliding
}








0 comments:

Post a Comment

 

Break-a-Loop Copyright © 2013 | Template design by O Pregador | Powered by Blogger Templates