Usually we script the ball movement by frame-based. We give the ball a speed of (pixels-shift/frame); and then we will recheck our movie in the next frame. If two balls over-lapped, we write our script to change the speed of these over-lapped balls. This is collision in Flash.
The problem is : we can not catch the exact time when the balls collide. When two balls fly in high speed, and their positions are checked in the next frame, they might not "over-lap" with each other. But they in fact have collide with each other between these two frame. We missed the collision.
Even we catch the "overlap" of these two balls in this frame, they have already hit mili-seconds before we check it. For example , ball_raius is 40, and in this frame their distance is 60. They got catched because (distance<2* ball_radius). Then we calculate the hitting angle. However, this angle is not correct. Ideally, we need to know when (distance==2* ball_radius), that is accurately the collision time when the distance is just 80 not 60.
So, we need math calculation. ¡@
OK, the blue ball goes with its xspeed, yspeed; and the red balls goes by its xspeed,yspeed; After t frame, the x of the blue balls will be x+xspeed*t, and the y will be y+yspeed*t; The x, y of red ball can be obtained by the same equation. Now we try to solve the equation when two balls collide with distance=2* ball_radius:
At last we will get an square equation like: t^2+b*t+c=0; Then we get t;
Usually there will be two answer for this t. Once is the time they just before over-lapping. The other is the time they just separate from overlapping.
Here is the fla by the math I described above.
download the zip file of this fla
However, I never use this accurate algorithm in my collision movie of snoocker. The reason:
1. It is complex and will add CPU constrain.
2. When we do multiple collisions, there is already many "in-accuracy", so I dont need this accuracy.