Friday, December 21, 2007

Trigonometry Based Collision/Bounce Algorithm

During the creation of the “Idiot-Prove collision bounce” article, I’ve come to realize the limitation of directly interfacing with the Horizontal & Vertical speed of an object to assign motion; I find it to be rather barbaric.

A better approach will be to assign motion based on Direction [Angle], and Speed. I find it to be much more elegant and human.




My approach on deploying this function is through the use of trigonometry, I do not know if there are other techniques, but I do know trigonometry will suffice.

Using the power of Sine, Cosine and Tangent, I realize that not only can I assign motion based on Direction and Speed, but because of the ability to move by angle, one can also detect collision angle and hence, bounce by angle!
We are talking about accurate bounce effect here !

The interface you are seeing is a rather limited example of what trigonometry can do, you can click anywhere in the interface to change the ball’s angle of motion in a random manner, the four “short” walls can, by means of trigonometry “predict” the ball’s “out of screen” location and move to intercept, quite a clever thing for the walls to do have I not understand trigonometry.

You can download the source code here, if you have any questions, feel free to contact me.

3 comments:

Anonymous said...

genius! haha

mj

Anonymous said...

Ya, computer games is all about math.

Reminds me of a computer game i got in my very first computer.

Anyway, try using vectors. Then you can do a 3D one.

flashMyPassion said...

not able to access the source ...specified page is not loading ... :(