So I have been working on my Physics Engine recently, and I've happened upon a rather interesting conundrum. In logic, it is rather simple to detect when an falling entity collides with a box; It's easy tell when the ball hits the ground. However, it is not at all easy to tell when a ball hits another ball, the algorithms just don't work that way. After a little bit of research, I have discovered that no Java library handles anything to do with collision boxes of circles, and I have to write my own. My approach right involves a rather tedious and heavyweight process of calculating the distance from center to center of one ball to every other ball, then comparing that distance to the sum of their respective radii. If that number is greater than the distance, then I must do further calculations to determine the angle of collision. Like the picture on the left, I can use the differences in x and y coordinate values to calculate that angle, but what do I do from there? Well, once I have that angle, I plan to apply the great and powerful Law of Conservation of Momentum, to determine the outcome, and here comes the tricky part. In Physics, many of the collision problems we are required to solve are called elastic collisions, in which no kinetic energy is lost during the collision.
I have noticed, however, that in the simulation, this results in an unreal behavior of bouncing, which makes sense, considering all real-life collisions (including the one one the right) are inelastic. This means that I need to assign some arbitrary dampening value to collisions (in the previous post, this value was set to 10%). So, like I said, this wasn't easy, and I haven't quite got the algorithm working perfectly, yet. But I plan to keep working on it and get it nice and optimized for my presentation.
Thanks for reading!
- Jeff