pygame is

pygame.org is
Circle Collisions

# Circle Collisions - 2.0.0

### Description

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!

### Screenshot

click to view original size

### Releases

 Circle Collisions - 2.0.0 - Feb 3, 2008

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:
Circle.speedx = abs(Circle.speedx)
Circle.speedx = -abs(Circle.speedx)
Circle.speedy = abs(Circle.speedy)
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)
spotlight

our projects
pygame.org welcomes all python game, art, music, sound, video and multimedia projects. If they use pygame or not.

recent releases
Mar 28, 2015

Mar 27, 2015

Mar 25, 2015

Mar 24, 2015

Mar 20, 2015

Mar 18, 2015

Mar 17, 2015

Mar 12, 2015

Feb 24, 2015

... more!

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