It was one of the most interesting experiments I have ever done. It is about of a ball falling down hitting everything on its way down like a pinball game ball. From the begining my biggest question on this project was if it is possible predict the landing position of the ball when its path is not actually predefined, it is begin defined on the fly completely at random in every single frame of animation. Why would we need to know the landing position of it? Well, in most of the cases when we are working in client/server architectures absolutely everything is processed (in terms of records, operations and results) in the server side and the front-end (client application) is not more than a slave user visual interface, which means that if in some scenario the server operations depend on a client request parameterized your application could be easily hacked in some way. All the decisions must be taken in the server side and the client just must show results to the user. The relation between client and server must be always like: the client sends a request to the server and the server tells to the client what to do, the client should never process something internally and send it to the server, it must be irrelevant to the server, otherwise each time the client sends a request, its data must be extremely validated in the server side which make everything more expensive in all senses.

Well, let's speak less and make more. In this example I made an algorithm for detecting collisions and reactions, the key of it is detecting as fast as possible all scenarios in future frames for tracing the path from the departing point to the landing point, the objective is find the landing position and not build the whole path. Most of the algorithms out there for processing circle collisions are using the heavy functions Math.sin, Math.cos and Math.atan, these functions are extremely expensive in terms of cpu time and we should avoid use them as much as we can, and unfortunately sometimes using pre calculated stored values doesn't give us the accuracy our projects needs, however there are some good ways for solving these problems, take a look in the source code to find more.

At this moment I have achieved resolve up to 1250 paths for the ball landing position on each ENTER_FRAME event without affecting the application performance (its frame rate is 40fps), every time it has to look at future frames for detecting the landing position, the algorithm could look up to 850 steps/frames forward but the nice part of all this is that we just need to execute it once of these 1250 when the user press the SPACE BAR :), so it will look always smooth and we never will have performance problems with it on any machine.

Take a look on it. At anytime if you want to choose a ball landing number just click everywhere on the screen and then once you have chosen your number press SPACE BAR to initialize the animation but at that point the program will already know everything about where and how the ball will land.

One day someone told me, you should use reverse physics rather than looking at future steps. Reverse physics are really cool but unfortunately there is not a good approach for applying them in this example. Do you have any idea about how many possible depart points we have for making it works completely at random? Define its patterns is insane.

download sourcecode unfortunately I didn't have the chance for commenting better this source code (this is a bit old), just feel free for asking whatever you want about it. Ahh! and dont forget to add the argument -target-player=10.0.0 when compiling, when I exported the code from AS2 I made it for running in Flash Player 10, all you need is the Open Source Flex SDK

Windows XP command line