Make your own free website on Tripod.com

Sticky balls and overlapping balls in Flash collision
by ericlin@ms1.hinet.net


I think many have similar frustration about the sticky balls that happens in our collision games such like Pool game.

It is annoying because it happens not always. It happens because the bounce did not separate those two balls away enough. But in what condition will this happen ? What speed or What moving direction will it results in sticky balls ?

I could not find any documents about this ?

So, I approach this problem by sloving a question:

On what condition, will two balls bounce to a distance shorter than the distance during collision ?

The "feature" of flash animation is "frame" based. We dont detect collision by definite Math. We detect collision by checking the relationship of balls for each "quantum" of time. If at the time of "checking", these two balls get overlapped, we consider them "get-Collided".  We do not really calculate the exact time of collision. In Flash movie, the collision is detected almost some time after the true collision time. When we detect the collision, the balls are not just touching each other but overlap moderately with each other. Then we apply the collision algorithm to calculate the after speed of each ball.

Please see the swf below. A red ball moves in that way and we expect it will hit the blue ball. These two balls may get detect in "safe collision" region. At that region, the balls are approaching each other. The bounce algorithm will make them "separate". If the collision is detected in the region of sticky collision, the bounce algorithm will bring them together and sticky balls phenomenon happens.

If  the speed of the balls are slow, collision will be detected almost in the region of safe collision. When the speed of the balls are fast, the chance of sticky collision increases.

In such condition, I dont treat it as a collision. It looks more like a red ball just passing by the blue ball and hardly get caught by the blue ball. By "hardly", I mean it has not get caught.



Here is the Math demonstrating the safe collision and sticky collision.

When a moving ball with the speed of dx and dy hits a stopped ball in a collision parallell to X axis, the stopped ball will bounce to a distance of dx and the moving ball will goes straight up for a distance of dy;

If the collision is a safe one, the distance after collision will be longer. (The hypo of a triangle is longer than the side, and the side is longer than the original distance.).

If the collision is a sticky one, the dx is weird and un-natural. The bounce vector - dx should normally an "expulsion"  now  is "attraction". The distance after collision will depends on the amplitude of dx and dy. There are chances that the distance after bounce will be shorter than the original distance. Then sticky balls happens. After bounce, the moving ball 1 will goes with a speed of  dy and the stopped ball will goes with a speed of dx. That is the "rolling" appearance we see after sticky balls.

It is obvious that, if the bounce vector is "attraction", the collision is invalid.



For collision, I used to change the coordinate system and view the thing from ball 2 by globalToLocal and localToGlobal. I skips those ball passing off me. So I dont get sticky ball. 

After I change my algorithm to a Math one, I get sticky balls noew. so I get to figure the way to get rid of them.

To avoid sticky balls, I add a check to skip such sticky collision. I compare the distance after collisioin and the original distance. If it is shorter,  then the collisioin does not count. Maybe it is enough to check whether the collision vector is attraction one or not. If it is attraction, then the collision does not count.

The condition becomes complicated if we handles multiple balls. If three balls get overlapped when we detect the collision, sequence becomes important. Usually we handles first two of the three balls. After calculation and bounce, the original speed of these two balls will change. Then we handles the third ball. However, the algorithm will take the new (changed) speed for calculation. Thus, which two get handled first will affects the end result.

In the example below, we handle the ball1 and ball 3 first. After collision, we make changes to the speed of ball 3. The resulting speed is  dx=0, dy=0.  However, when we go to check ball 2 and ball 3, we detect that they are overlapped. So, they should bounce off each other. How to bounce ? Both of them are stopped balls with dx=0, dy=0. So they remain overlapped and stand still.

This may also cause sticky balls. However, if balls are moving,  the overlap will release soon after several frames. It causes trouble only if they stop and get overlapped.


I hope this will help in understanding the stickyball phenomnenon.

Eric Lin