Learn Create Your Own Split Screen

Next Page

As that last example clearly showed, we should also apply the same idea to the x-velocity.

We do this at the point we're handling the vertical bounce, and we simply reduce the x-velocity by the bounciness factor as well. If we wanted to be fussy, we might have a separate bounciness factor for horizontal behaviour, perhaps related to the roughness of the surface. But for simplicity we'll use the same factor for both.

this.velocity_x= this.velocity_x * this.bounciness // also slow horizontally

Now our tennis ball really is bouncing like a real-world ball.

We could get even fancier and add some air-resistance so it even slows down while in flight, but this is a pretty accurate simulation as it stands.