Return To CS 110 Home

Colour Theme   Font Size Options
 
   
   
   
Bouncing Ball

Interactive Programs

Overview

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 systems.

Explore

  1. Get the program
    • Download, unzip, and start the project:
      Visual Studio: Bounce.zip - this version is for Visual Studio 2015, but Visual Studio 2019 will offer to upgrade it for you.
      XCode: Bounce_Xcode_6.zip - this version is for Xcode 6, but should still work in newer versions.
      Code::Blocks: Bounce_CB.zip - this version is for Code::Blocks 20 on 64-bit Windows, but should still work in newer versions.
      Repl.it: Bounce on Repl.it - the 3D is streamed over the internet, so it's a bit slow... but it works!
      (Click "Fork" in the upper right to edit your own copy.)
      Hercules: Not supported (but not impossible...)

    • 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:
      a table with a ball positioned above it

      Initial simulation screen.

      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.

  2. 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 handle events.
      • 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.

  3. 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:
      1. Calculate how much time has passed since the last physics update.
      2. Save the position of the ball for future reference
      3. 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.

      4. 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.

Exercise

Using what you learned in Explore parts 2 and 3 see if you can:

Fun Graphics Links