CS 200 - Lab 25

Bouncing Particles

In this lab, you will implement a simulation of particles bouncing around in a container.  The program is a frame-by-frame animation, like Lab 22 and Assignment 5.

You can run my solution to get an idea of what the program will look like when it runs:


Getting started

If you have a solution open in Visual Studio, close it (File->Close Solution).

Download lab25.zip.  Import it into Visual Studio (File->Import...).

Add your code to the source file called Bounce.c.

Your task

You will need to add fields to the struct Particle type, and implement the init_particle, render_particle, and update_particle functions.

For each particle, your program will need to keep track of the particle's position, direction/velocity (x and y), and color.

The init_particle function should choose a random initial position, direction, and color for a single particle.

The render_particle function should draw the particle on the screen using its current position and color.

The update_particle function should move the particle one step by its current x and y direction/velocity.  If the particle hits an edge of the screen, you should reverse its direction appropriately.


Change the definition of the NUM_PARTICLES constant to change the number of particles simulated.

Use floating point values to keep track of each particle's position and direction/velocity.

Set the initial x and y components of each particle's direction/velocity to be a random double value between 0.0 and 1.5.  You can use the following code to generate the value:

(rand() % 1500) / 1000.0

To generate a random color, generate a random integer value between 0 and 15.