Ball-in-Box

For this project I took the basic ball in box code and re-wrote it as using a Ball class. This allowed me to add a second ball to the movie while using the same source code for each. Both of these balls are started at random places on the screen, and their trajectory angles are also determined randomly. A reset button was added whose function is to start the movie over, allowing the user to see the balls react to being dropped from different heights and angles.

I decided to make the project look more like a real world situations, so I added two walls, a ground and a sky. Furthering the realism of the project required that gravity to be simulated in the environment.

To add the simulation of gravity, I used the ‘good enough’ gravity script provided in the ‘Game Design De-Mystified’ book. This script works well for the bouncing balls, but to achieve true realism, energy must be lost on each bounce. I added a diminishGravity function, who’s job is to reduce the amount of artificial gravity over time. This makes the balls appear to lose energy over time, and eventually they fall to rest on the ground. This worked fine, except the balls movement in the x-axis continued to occur. To fix this problem, I diminished the amount of x-axis movement by reducing, by a small percentage, the amount of horizontal movement every time the ball hits the ground or the wall.

Finally, I made the walls change color each time they are hit with the ball. After doing this, a curious bug showed up. If the blue ball hits the wall first, it will make it change colors, but after the red ball hits a wall, the blue ball will no longer make the wall change color—a quirk that last until the movie is reset by the user.