A Circle to Circle collision detection algorithm.
Pygame.org account Comments
Idteller 2011-10-15 06:09:07
Nice code. I recommend defining your screen, or surface as stated below;
SCREEN_WIDTH = 800SCREEN_HEIGHT = 600SCREEN_HEIGHT = 600screen = pygame.display.set_mode((SCREEN_WIDTH,SCREEN_HEIGHT))
Then whenever you refer to the screen height or width you can do so by entering;
screen.get_width() or screen.get_height()This will allow you to modify the screen size if necessary without browsing through code for all screen width and height values. Instead you can just change the screen size at the beginning.
Patric 2014-07-24 12:35:09
I had a look at your collision code and I wondered a bit about this part here in the CollisionDetect() function:
if Circle != Circle2:
Aren't you actually checking the circles with lower indices twice with this approach? My approach in a small game I recently wrote, was to go by the indices of the circles:
for n in range(len(Circles)):
# The whole code before the collision detection
if n < len(Circles) - 1:
if Circles[n].radius + Circles[n + 1].radius < math.hypot(Circles[n].x - Circles[n + 1].x, Circles[n].y - Circles[n + 1].y):
CircleCollide(Circles[n], Circles[n + 1])
What are your thoughts on this?
Named Tuple 2014-07-25 14:17:24
I think he is checking the same circle pairs twice. Going by the indices works, but I think your approach misses non-adjacent circles in the list, i.e. circle 1 and circle 3 won't be checked. A solution is to use the python itertools library as below:
for circle1, circle2 in itertools.combinations(range(20), 2):
#check for circle collision
# if circle collision:
# update circle velocity
Hope this helps! I am making something like this as well right now, maybe I will upload it at some point.