pygame is
Simple DirectMedia Layer is
Site Swing
Circle Collisions

Circle Collisions - 2.0.0

Ian Mallett (geometrian)



I've been working on this a while. Circles bounce off of each other correctly (I think...). Like my Gravity Simulation, the screenshot cannot convey what this program actually does. Download it and enjoy!


Home Page:


click to view original size


Circle Collisions - 2.0.0 - Feb 3, 2008 account Comments

If you wish to leave a comment with your account, please sign in first.

February 3, 2008 11:48am - Ian Mallett - nickname: (geometrian)
nsiderfred151: Yes, I did notice that, especially when new circles were made inside the wall (I think I corrected that in this release). My solution to the problem was to move the circle back inside the place it should be and then reverse the speed, though I don't think I included it in this release. I think your way works too.

pymike: Thanks! This project was actually test code for collision detection in a 3D engine. Obviously this is not 3D, but I expect developments to come! I think that changing this code to bounce spheres in 3D should be pretty easy.

I was really silly about the collision detection algorithm. The way it is now is correct. Before, I tried to get the point of contact, all the angles involved, bounce the vector of off the tangential line to both circles through the point of contact (using that line's normal as a reference), then normalize the vector to the previous speed by multiplying its reciprocal by a geometrically similar unit vector. Obviously a lot more complicated than sines and cosines. Faster too.
February 3, 2008 9:53am - pymike - nickname: (pymike) - 5/5
This is AWESOME!! Really nice. You should make an abstract space shooter with this code! I'm totally serious. It could be awesome!
February 3, 2008 9:39am - Andy Hanson - nickname: (rhodiumgames)
I pasted wrong: last line is Circle.speedy = -abs(Circle.speedy). Plus there's tabs!
February 3, 2008 9:34am - Andy Hanson - nickname: (rhodiumgames)
Very good, very "working." I set the speed up a little myself and now they sometimes get stuck to the walls; this is probably because it hits a wall, turns around, and then gets hit back into the wall and goes into an eternity of turning around. You can fix this by adding the following code instead of the current wall-hitting code:
#First for-loop undef def CollisionDetect():
for Circle in Circles:
if Circle.x < Circle.radius:
Circle.speedx = abs(Circle.speedx)
elif Circle.x > 800-Circle.radius:
Circle.speedx = -abs(Circle.speedx)
if Circle.y < Circle.radius:
Circle.speedy = abs(Circle.speedy)
elif Circle.y > 600-Circle.radius:
Circle.speedy *= -1

And then it works fine. The circles can only be reflected in the right direction.
Watching these circles is pretty fun. It would be nice to see a game with this kind of physics.
February 3, 2008 1:23am - Ian Mallett - nickname: (geometrian)
Version 2.0.0 released! (Version 1.0.0 was pre-release work)

our projects welcomes all python game, art, music, sound, video and multimedia projects. If they use pygame or not.
recent releases
Feb 21, 2017

Jan 31, 2017

Jan 24, 2017

Jan 18, 2017

Jan 7, 2017

Dec 30, 2016

Dec 8, 2016

Nov 28, 2016

Nov 27, 2016

... more!
for pygame related questions, comments, and suggestions, please see help (lists, irc)