Saturday, 1 November 2008

Ball physics - now with 100% more C++, and 50% more awesome

Please excuse the absence - I was on holiday, and then was flooded with exams immediately afterwards. And then afterwards I forgot to post. For a week.

So, here I am again, this time bringing you news of another mini-project of mine. Some of you may remember my python ball physics test; well, I've been working at re-creating it in C++ (using SDL for graphics of course). To date, I have created a Circle class which is affected by gravity, and can detect both circle-wall and circle-circle collisions (unlike in my Python attempt, you can have multiple circles). The circles can be added with right-click, and dragged around with left-click (and this time the ball is still
affected by gravity and collisions whilst being dragged - it's just weighted towards the mouse cursor).

When it detects a wall collision, it bounces realistically - and this time it detects collisions before they happen, so it's impossible for a circle to go through the wall as happened frequently in my Python ball physics simulation. All that remains is adding realistic bouncing for circle-circle collisions; I have a sort of temporary bounce system in place but it's incredibly unrealistic. So, without further ado, here's a screenshot (white lines are the circles' vectors, the red line is connecting the circle that's bei
ng dragged to the mouse cursor):

In this screenshot, the ball that's being dragged is being swung round by the mouse cursor - this was impossible in the Python version where the ball simply locked to your cursor.

Next on the menu is working out how to do proper ball-ball collisions - the detection is all there, but making them bounce properly is actually surprisingly difficult. James and I are working on a system for doing that at the moment, so hopefully we'll have a solution in the near future.

Source code for this project