Well I've been working the last few days on 'Crystal Balls'; I added platforms and scrolling. The balls properly bounce off the platforms; this was something I severely underestimated...
Let me first explain what I initially did:
A circle is defined by the formula x^2+y^2=r^2. So when you want to test a platform against a circle, you can basically fill in the four lines of the platform in above formula; to get the intersection of a vertical line, you basically calculate y = +/- sqrt(radius^2 + x^2). When the resulting y is between the start and end point of the line, you have an intersection. So far so good, I was perfectly able to find intersection points.
Next, I needed to make the ball bounce. This means that I wanted to reflect the direction vector against 'something'. This 'something' is basically the normal at the intersection point. A normal can be calculated by taking a vector from the centre of the circle to the intersection point, and then swap the x/y components and negate the x component.
This gave decent results, but I got major problems: The direction that the ball bounced to was frequently very odd; and often the ball would get stuck in the platform. This is of course caused by the fact that you find a 'collision' only when there's in fact already an intersection; if the reflected vector is just a tiny bit wrong, the next frame will again result in an intersection.
So, I added a delay in the collision detection: After a 'bounce', a ball is not checked for 3 or 4 frames. This resolved most of the sticky balls problems.
But, the direction was still odd. So, instead of calculating the normal based on a single intersection, I now base it on two: Since a platform is a convex shape, it's only logical that each intersection happens twice: Once where the outline of the ball enters the platform shape, and once where it leaves it. Theoretically, you could also have a case where the ball exactly touches the platform, but this is rare, and I ignore it.
So, I look for intersections. Once I have found two (possibly by different lines of the platform, the ball could have hit a corner), I create the normal by simply connecting the two intersection points. This resolved almost all direction problems. Sticky balls are rare now, and they bounce in a realistic manner.
The code contains lots of floating point stuff, and I expected that I would need to replace that by fixed point stuff. For four balls and 8 platforms this is not neccessary however; the game needs much more time for drawing the balls (using pixel displacement and some color scaling; these are glass balls) than for calculating 'physics'.
Well I just wanted to describe the problem and the current solution. Thought it might be interesting.
Believe me, physics are always harder than you expect at first. 
- Jacco.
Let me first explain what I initially did:
A circle is defined by the formula x^2+y^2=r^2. So when you want to test a platform against a circle, you can basically fill in the four lines of the platform in above formula; to get the intersection of a vertical line, you basically calculate y = +/- sqrt(radius^2 + x^2). When the resulting y is between the start and end point of the line, you have an intersection. So far so good, I was perfectly able to find intersection points.
Next, I needed to make the ball bounce. This means that I wanted to reflect the direction vector against 'something'. This 'something' is basically the normal at the intersection point. A normal can be calculated by taking a vector from the centre of the circle to the intersection point, and then swap the x/y components and negate the x component.
This gave decent results, but I got major problems: The direction that the ball bounced to was frequently very odd; and often the ball would get stuck in the platform. This is of course caused by the fact that you find a 'collision' only when there's in fact already an intersection; if the reflected vector is just a tiny bit wrong, the next frame will again result in an intersection.
So, I added a delay in the collision detection: After a 'bounce', a ball is not checked for 3 or 4 frames. This resolved most of the sticky balls problems.
But, the direction was still odd. So, instead of calculating the normal based on a single intersection, I now base it on two: Since a platform is a convex shape, it's only logical that each intersection happens twice: Once where the outline of the ball enters the platform shape, and once where it leaves it. Theoretically, you could also have a case where the ball exactly touches the platform, but this is rare, and I ignore it.
So, I look for intersections. Once I have found two (possibly by different lines of the platform, the ball could have hit a corner), I create the normal by simply connecting the two intersection points. This resolved almost all direction problems. Sticky balls are rare now, and they bounce in a realistic manner.
The code contains lots of floating point stuff, and I expected that I would need to replace that by fixed point stuff. For four balls and 8 platforms this is not neccessary however; the game needs much more time for drawing the balls (using pixel displacement and some color scaling; these are glass balls) than for calculating 'physics'.
Well I just wanted to describe the problem and the current solution. Thought it might be interesting.
- Jacco.