PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Bouncing balls

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.
I remember doing something like this when I wanted to make a lot of light rays reflect and bounce around a closed room full of mirrors at random angles. Quite an old program and at that time I wasn't even considering the speed and the number of floating point operations.

But your case seems similar to mine. Then I used angles (0-360 degrees) rather than (x,y) vectors. This makes reflecting about a fixed line much easier, I expect.

Are your platforms at fixed angles? Actually I think it works even if the platforms move. So if the platform is at an angle A, and the ball is moving towards if at angle B, then the resultant angle of the ball is 2*A-B. (Grabbed from my old QBasic program, but should still work). No floating point multiplications needed, just bit-shifting and subtraction for integer angles. It works for both sides of the platform.

Then I suppose you can combine this with vector movement, recalculating after each bounce the movement vector (x=vcos(A/180*Pi),y=vsin(A/180*Pi)) or something.

My mirrors program checked for a hit by checking if the screen color at that point is the mirror color, just a simple method. But I believe then that checking if the ball hits the platform can be faster than using sqrts and intersection points.

If perpendicular distance of center of ball to platform edge >= ball radius, there is a hit. To find perp distance, given the platform line gradient from its angle, perhaps you could inverse and negate it to get gradient of normal, and from there find equation of line from centre of ball with that gradient. Then get intersection point with platform line.

Here I have to find one intersection point and probably use one square root for distance. But you can remove the square root by checking if distance squared greater than precalculated radius of ball squared.

I'm just thinking out loud, hope it works.
There may be a better way to calculate perpendicular distance using vectors... (just as well that i'm being tested on vectors next week)

Given a platform unit vector X (calculatable from angle) and a horizontal vector from the centre of the ball to the platform Y, calculate |XxY| (modulus of X cross Y). That would give the perpendicular distance. Likewise, since there is a square root, we can square both sides and the inequality still holds.

The platform unit vector can be precalculated. The horizontal vector can be calculated by ...

Sorry, second thoughts now. If you know the angle of the platform, and the length of the horizontal vector, then you can calculate perpendicular distance using sine.

So now it is just needed to calculate the horizontal vector, maybe by matching the y-coordinates. From a precalculated equation of the line of the platform, substitute in the y-coordinate of the ball, find the x-coordinate, the a simple subtraction gives length of horizontal vector.

Is this better? I would think so, unless sine is slow. But then again, if it is indeed too slow, some values can be precalculated i suppose. And if we work in radians not degrees, it would save the conversion from degrees to radians, unless we also precalculate that.

Pardon my rambling monologue. Oh and my inequality above should have been <=. (Sorry no smiles in this text because of that =).
Mirek Czerwi&#324;ski · 18 Nov 2001, 11:44 UTC
hmm, I'm not sure If I'm right, but you could try doing something like this:
if you spot an intersection, you should move "back in time" to the point just before the intersection, then update your ball velocity vector and go thru the rest of time using this vector (assuming there are no more collisions)
This should prevent you from getting stuck. Another thing you could do is to make your platforms a bit round on the edges so a ball doesn't just flip x/y vectors after hitting a platform. I don't know what your game is about, but it seems like a good idea to increase playability/game feeling in all type of arcanoid and similar games.
Hope it helps:)
Pat: Working with vectors instead of angles is quite easy, and I used a formula for reflecting the directional vector that looks quite similar to what you used.

BTW, the platforms are all axis aligned. Having random platforms would be no problem for the reflection, but it does make the 'sphere intersection' much cheaper (pure int math, no square root neccessary).

About the square roots: I used them, and I planned to get rid of them, but the game is fast enough, so I won't bother. Besides, the game spends less then 5% in the physics code. I'm working with just 4 large balls that can be split into 36 small balls (maximum) and 4 platforms (maximum); under these circumstances, drawing the balls is the bottleneck. I won't bother optimizing code that is fast enough already. :)

Mirek: Don't you think round corners on the platforms would make things more complicated? :)

Your other idea, about going back in time, is indeed how it 'should be done', I think a pinball game needs that approach. I'm glad I didn't have to do that though, makes things quite a bit more complicated... Basically I settled for a sufficiently accurate approximation (and that's what you always do, isn't it? :) )
Well, those thing would make the whole physics code a bit more complicated... but I believe it could give you a heavily increase in playability... and there's nothing worser then seeing your ball suddenly stuck inside a platform...:) All in all, if you would start with this approach instead testing and fixing your previous code to come to your current solution, you might as well be finished doing it "the right way" by now:)
Another question is, if it really increases your playability, wouldn't it be better to spend few more h to achieve this result? Doesn't seems like a lot of time comparing to the time spent on developing a game...