CS151 Labs

Lab 8 will be collected from your subversion repository on Monday, November 21, at 5pm.

Work in the file lab8/lab8.rkt. It is important that this file has exactly this name and location; we will be looking for your work under this name at collection time.

This week, you will write a simple kinematics system. You will use it with the 2htdp/universe system to build a real-time simulation of bouncing balls.

We will use the following data definition for a ball:

;; a ball is a 
;; (make-ball y v e r c)
;; for y (position above ground of the center of the ball in meters) num, 
;;     v (velocity in meters per second) num, 
;;     e (elasticity) num (between 0 and 1), 
;;     r (radius in meters) num,
;;     c color
(define-struct ball (y v e r c))
We only keep track of each ball's height above the ground (y), making the (unrealistic) assumption that the other two components of the ball's position in three-dimensional space (x and z) remain fixed. The ground is at position 0 and perfectly flat and horizontal, and, in your visualization, the ground should be the bottom of the rendered image.

Given the current position y of the ball, the next position should be y + v dt, where dt (the time step) is 1/28, with the common-sense limitation that the ball cannot penetrate the ground. That is to say, a ball's center's position may never allow it to be below ground. The new velocity of the ball at any time step is either

In 2htdp/universe, a time event occurs every 1/28 of a second. At every time step, the simulation must compute a new position and velocity for each ball. You must implement a time-event-handling function (say tick) with type world -> world. You register the time handler with big bang with the expression (on-tick tick), where tick is standing in for whatever that function's name is.

Implement a function finished? : world -> bool to test if the simulation is done. A simulation is done when every ball rests on the ground with velocity 0. You can tell the simulation when to stop in big-bang with the expression (stop-when finished?). A suggestion for the implementation of finished?: look at the documentation for the built-in function andmap.

A world is a list of balls. You can render the world how you like, within reason; in mid-flight, it might look something like this:

Note: when you render the state of the world, you will need to scale the scene to fit in a small window. The model objects in the scene pictured have radii 0.25m, 0.2m and 0.15m from left to right, and they were "dropped" from between around 2 and 3 meters.

Write a renderer, a time-event handler, and a finished-tester, and put them together into a function ;; run-sim : world -> world that calls big-bang on the given world and runs the simulation to completion.