Physics-Based Bounce Effect - Page 2
       by kirupa  |  10 February 2009

In the previous page, you saw the animation that I will be deconstructing, and you also received an overview of the math behind falling objects. In this page, let's pick up from where we left off and look at the math behind objects that bounce up after hitting the ground.

Physics and Bouncing Objects
Our ball's movement is actually made up of two separate sequences. The first is the falling sequence which I described above. The second is the rising sequence, the bounce, and let's look at that now.

When your ball hits the ground and bounces, the following diagram describes what is at play on the way up:

Before, gravity was your friend. It helped move your ball in the correct direction. Now, though, gravity is actually fighting against you. You have the upward initial speed from your bounce competing with the downward pressure of gravity. Fortunately, the equation I showed you above is flexible enough to work in this situation as well:

This time, we don't get rid of the initial speed portion of the equation. Instead, we actually have an initial speed. This initial speed is the final speed of the ball as it fell earlier. How do we calculate the final speed of the ball as it hits the ground? This requires another equation, and this comes from the series of equations linking Kinetic Energy and Potential Energy:

The speed of an object under constant acceleration is the square root of two times gravity (aka acceleration) and height. This result makes up the initial speed, and you can plug this value into the equation that is shown just a few paragraphs ago.

Putting these two equations together, you have everything you need for defining the bounce. There is a slight catch though, so let's look at that next.

Conservation of Energy
One thing you need to keep in mind is conservation of energy. In an ideal world, a ball you drop from a certain height will keep bouncing back to that exact height every time. In other words, your ball will be bouncing forever. This is clearly not the case in the real world. The reason is that, each time the ball hits the ground, some of the energy from the impact is converted into other things such as heat and sound. Not everything goes back into making the ball bounce.

With each subsequent bounce, your ball has a bit less energy than it did in the bounce that preceded it. That is the real world. Flash simulates an ideal world. What you will need to do is force some energy decay to cause your ball to not bounce indefinitely. There are numerous areas where you can do that, but the one that I will interfere with is the initial speed as you are about to bounce up:

Each time the ball is about to bounce up (stages II and IV), I decay the value of the initial speed by a certain amount. This gives you the effect of your ball bouncing with each subsequent bounce being weaker than the one that preceded it. In the physical world, the amount of energy your ball has with each bounce is decreased when it hits the ground. This decay indirectly gives us the same effect in an ideal world. That is all there is to the physics behind all of this.

Ok, now that you have a primer on how a ball's bounce theoretically works, let's look at the code and see how the world of physics maps with how we do things in Flash.

Onwards to the next page!

1 | 2 | 3 | 4




SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple.