In the application, you can interact with the circles by dragging them around or "shooting" them like billiard balls. You can also control the number of circles (maximum of 30, though the simulation can probably handle a good deal more), and you can turn gravity on and off. There are a few hiccups in the collisions every now and then, bu overall I am very pleased with how it turned out. With gravity turned on, a high number of circles, and the "drag" setting turned on, I've noticed some buggy behavior once the circles are all settled to the bottom, but oh well.
I know that there are many awesome and fast physics engines written for ActionScript, JavaScript, C++, etc. out there, but I sometimes feel a little cheap using those without having a real understanding of how it all works. So I make my own, and I encourage others to do the same.
I know that there are many awesome and fast physics engines written for ActionScript, JavaScript, C++, etc. out there, but I sometimes feel a little cheap using those without having a real understanding of how it all works. So I make my own, and I encourage others to do the same.
Without further ado:
[Click and drag to interact]
The key to accurate circle collisions is an understanding of the vectors involved. If you're familiar with physics, you know that vectors can be broken down into multiple components with the help of trigonometry.
The following pictures demonstrate the changes in speed and direction when two circular objects collide; in this case, only one circle is initially in motion:
In this image, the first circle's direction and magnitude of velocity (where it's moving and how fast it's moving) are represented by the arrow. The longer the arrow, the faster the movement.
Warning: Math Ahead!
The following pictures demonstrate the changes in speed and direction when two circular objects collide; in this case, only one circle is initially in motion:
![]() |
| The first ball moves toward the second. |
The point of collision.
In this image, the red line represents the tangential angle between the circles, and the blue line represents the centripetal angle (the angle between the two circles). If you've ever seen a "Newton's Cradle" in action, you know that an object will transfer all of its momentum to an object of equal mass when they collide head-on. The key here is that momentum can only be transferred in the centripetal (blue) direction. Any momentum that the first circle has in any other direction is "glancing", and will remain with the first circle after impact.
The components of V along tangential and centripetal lines.
The resultant velocities.
That's the basics of what goes on in this simulation. There is a WHOLE lot more to it; adjusting for collision overlaps, dealing with friction, etc. etc. This simulation doesn't even incorporate circles of different "masses" (calculated by area, generally), but that can be done easily enough. The only difference is that momentum exchanged along the centripetal line is more than just velocity-swapping, and there are some more complicated formulas to deal with that.
If you're interested in this sort of thing or have any questions, shout it out.
Thanks for reading.




I like that you can hold the "string" outside the frame and cause a yo-yo effect.
ReplyDeleteI cannot even tell you how many times I swung the circles around like I was gonna clear that screen. Neat!
ReplyDelete