In this exercise you will examine how if and switch control
statements are used to control physics and user interaction in an
interactive 3D program.
The 3D rendering is done with OpenGL - a historically important graphics system that inspired Direct3D. A version of OpenGL is the core graphics system on mobile phones everywhere. The user interface is provided by SDL, a C++ tool that helps with writing interactive programs for multiple operating
- Get the program
- Download, unzip, and start this Visual Studio 2015 project: Bounce.zip
Mac Users: This version works with XCode 6.
Hercules Users: This program does graphics and requires a windowing envronment. Use Visual Studio or XCode for this exercise.
- Take a look at the Solution Explorer.
You will notice that there are two .cpp files and one .h
file. main.cpp has the main() function in it, and
some other functions that control and display the simulation. The
other two files hide away the code to set up the graphics window and
to draw spheres and boxes.
- When you run the program it will take over the screen so read these instructions first:
- The a and d keys move the ball side to side.
- The f key and left mouse button "throw" the ball in the direction of the red line.
- The mouse moves the red line sticking out of the ball.
- The Esc key stops the program.
- Build and run the program. It should look like this:
Play around with the program a bit. As you move the mouse you will
notice a red line. It controls how you throw the ball. It is the
initial force vector.
Initial simulation screen.
- Handling User Interaction
- Whenever a user does something it generates something called an
event. Whether you are programming on a Mac, PC,
or videogame console, you will have to handle events for interactive
programs. This program uses SDL to simplify event handling.
- Events come in different types: Mouse movement, Mouse
button, Keyboard, Quit, etc. You can find a complete list of SDL events here.
- Selection control structures are used to figure out what event is
being sent to the program. This program uses both nested
if/else and switch/case control structures to
- if/else: in Visual Studio use
control + f to search for
Event handler. If you have time, read the comments you
find there to learn a bit about how it works.
- switch/case: in Visual Studio search for
Key handler. Notice that a special constant is used to
represent each key. You can find a complete list of
these constants here.
- Simple Physics with if statements
- In Visual Studio open main.cpp and use
control + F to search for
Physics code. This
function does some simplified physics calculations that bounce the
ball off the top of the table. Use the comments to find the code for
each of these steps:
- Calculate how much time has passed since the last physics update.
- Save the position of the ball for future reference
- Update the ball's physical parameters. Refer to this diagram to see what they are:
Notice that this is a 2D simulation, even though we are showing it with 3D graphics. This means that all coordinates and movement vectors have two components, x and y.
- Apply the force of gravity, F_gravity to the ball's vertical velocity, vel_y.
- Calculate a new position using this stepwise equation:
Positionnew = Positionold + velocity * dt
Notice that this needs to be done for both the x and y axes separately.
- Bounce the ball if it hits the table:
A simplified ball bounce. Notice the conditions for a bounce and the effect.
- if the bottom of the ball was above the table top last time
and it is now below the table
and it is not past either side of the table
- then We assume the table is perfectly level,
reverse the ball's velocity in the y direction
and steal back some energy:
Velocityy after = -Velocityy before * 0.9
then place the ball on top of the table. If we don't do that, the ball
will slowly sink through the table once it stops bouncing. Friction due to rolling is removed from velocity in the y direction.
A better solution would find a new position for the ball by
calculating when and where the ball hit the table, then calculating a
new position for the ball based on its new velocity and the remaining
time. At 30 frames per second this shouldn't matter.
Using what you learned in Explore parts 2 and 3 see if you can:
- Check out the
Key Handler and notice the cases for a and d that move the ball's initial x position, pos_x_init, left and right.
Add cases for the w and s keys to move the ball's initial y position, pos_y_init, up and down.
What happens if you throw the ball hard up at the table?
- Make the ball bounce off the bottom of the table instead of just
Fun Graphics Links