Predicting the landing position

date: 04 Feb, 2007, actionscript: 3 FP10, complexity: complexity level 4 of 5complexity level 4 of 5complexity level 4 of 5complexity level 4 of 5complexity level 4 of 5, feedback (votes): users score 4.98 of 5 (275 votes)users score 4.98 of 5 (275 votes)users score 4.98 of 5 (275 votes)users score 4.98 of 5 (275 votes)users score 4.98 of 5 (275 votes)

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 don't 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 Open Source Flex SDK

Windows XP command line

date: 18 Feb 2012 - 01:04:56, published by Rahul
Please view this game code
date: 22 Sep 2010 - 01:00:00, published by alex.nino
Hi, thanks for your feedback, have you seem how this algorithm works? the landing position is defined before launching the ball, it is actually impossible landing in the wrong place. This algorithm was tested exhaustively (more than 10 million falls) and it has passed all of them with no problems. could you please send me a screen-shot of that thing you said? cheers.
date: 22 Sep 2010 - 00:28:38, published by john
there are some errors of your program.. please solve it... the ball cant move after it falls to the mid line of the numbers in between
date: 20 May 2010 - 03:54:38
Amazing thx for you professionalism
date: 03 May 2010 - 18:23:20, published by Snow | Dragon
Thats cool. I can bet people what number it will land on now =)
date: 23 Feb 2010 - 14:27:41, published by mika
crazy stuff i'm putting a not for digging that code for when i'll retreat in my bat cave and say good bye to the world ! ;)