Skip to content

Instantly share code, notes, and snippets.

@oneup
Created July 8, 2009 17:27
Show Gist options
  • Save oneup/142991 to your computer and use it in GitHub Desktop.
Save oneup/142991 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
#
# check for collisions between two circles
# we can compare the squared radius/distance instead of comparing squareroots because of math.
def circles_collide (x1, y1, radius1, x2, y2, radius2)
# compare the distance to combined radii
distance_x = x2 - x1
distance_y = y2 - y1
radii = radius1 + radius2
# if the distance between circles is smaller than the sum of their radii, they collide
if ( dx * dx ) + ( dy * dy ) < (radii * radii)
return true
else
return false
# via http://cgp.wikidot.com/circle-to-circle-collision-detection
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment