**Problem statement:**

This is an assignment about the simulation of the motion of balls in a frictionless medium (almost like ice). There are balls of radius each, where is fixed and much smaller than 1. Each ball has the same unit mass (this matters when two balls meet). At time zero, the balls are placed in random locations in a unit square or unit circle and are assigned random initial velocities (associate a vector with each ball; it may be a good idea if the velocities sum up to ). You can consider either of the following situations:

- the terrain is a unit square. The balls start moving around with constant speed until they meet another ball or until they hit the boundary.
- the terrain is the infinite plane. In such a case assume that centers of balls are connected with invisible strings of length 2. The balls start moving around with constant speed until they meet another ball or until one of the ropes emanating from its center gets stretched. If you make sure that the sum of the initial velocity vectors is zero, then the pack of balls will not wander off to infinity (conservation of momentum).

In either case, a collision/stretch event causes the balls to change direction and velocity according to the laws of physics. That is, they instantly change velocity and move in the direction given by the new velocity vector. There is a section further on that helps you with some of the mathematical problems related to collisions, velocities, and geometry.

You have to animate the motion of the balls on a graphic screen up to some finite time. You may choose either of the above options.

**Howto:**

The simulation has to be driven by events.
An event is an impact. For each ball, you have to keep track of the
next event, and the next events are kept in a *heap*, ordered according
to the times of the events (smallest on top). Deleting the top
of the heap drives your simulation. As you make an event happen, you must
recompute the next collision(s) for the ball(s) involved
in the event. This may affect some other events, and you may wish to
implement the operation "cancel". Also, new events are
inserted in the future event heap. You should keep the state of your system
in one data structure--this includes for each ball, say,
its last known position, the corresponding time, and its velocity; also,
you may have for each ball pointers to other balls or to the
heap so that you can implement cancel very easily. In fact, it is a good
idea to have to-and-from pointers between the items in the
state and items in the heap.

Continuous uninterrupted time simulation is usually performed by letting jump ahead by small deltas. At these multiples of delta, you'll draw a screen. Drawing a screen is treated as any other event--it is something you'll put on the heap. When the draw-screen event comes off the heap, the screen is drawn. For "impact" type events, the screen is not drawn, for otherwise, you would lose the continuity effect. For better performance, you do not redraw the screen. Just erase the old balls (draw again with background colour) and draw new balls. That should give you a juicy real-time simulation on most systems and add some animation/colour to your life!

For the graphics part you should use Java applets. You can look at the following applet demos for tips:

You should also look at the html page source.You may face some problems with running Java applets in standard browsers (IE or Netscape). You may be better off testting your programs with /bin/appletviewer on ccsun50. For this you would need to grab a X-terminal/Sun terminal. You may also run it on a windows or linux machine provided JDK is available.

**Submission:**

Submit a single file called **animation.java** by clicking
here. The last date is April 15 (**firm**).
This assignment can be done in groups of two.

**Some kinetics**

Here are a few facts that may help you. If a ball's position at time is , and its velocity is , then at time , it is found at . Its velocity does not change unless a collision occurs or the ball meets one of the boundaries.

For the meeting of two balls, described at time by and respectively, there are three questions:

- will they meet?
- where and when do they meet?
- what are the new velocity vectors after the meeting?

Let us answer these backwards. Assume we know that the balls meet at a given site. At that place, the balls have a common tangent. Picture two coordinate axes at that site, the -axis being aligned with the tangent. The -axis thus goes through the centers of both balls. You can decompose both velocity vectors into their and components: thus and . (Be careful! This is a decomposition with respect to this new system of axes!) Well, here is what happens: the first ball flies off with velocity , and the second one with velocity : they keep their -components, and exchange -components!

Finally, can we determine if, where and when two balls will meet? Let the states of two balls be described by the coordinate velocity pairs and respectively. You can choose of one ball to be and compute the parameters of another ball relative to the first ball as The path of the second ball relative to the first is . The square of the length of this vector is a quadratic in . Look when it is minimal. If that happens at time , no collision will occur (the balls are moving away from each other). If it occurs at time , check if the length at that point is less than : if it is, the balls will hit each other; otherwise, they won't. Suppose you know that the balls hit each other. Then the hitting time is the smallest value greater than for which (a simple quadratic equation in ). At , we know exactly where the two balls will be. The place of impact is the midpoint between the centers. Apply the rules for the impact outlined above, and off you go!

When a rope gets stretched between two balls and , picture two coordinate axes, the -axis being aligned with the rope, and the -axis perpendicular to it at the midpoint of the rope. The -axis thus goes through the centers of both balls. Again, you can decompose both velocity vectors into their and components: thus and . Here is what happens: the first ball flies off with velocity , and the second one with velocity : they keep their -components, and exchange -components! (This is identical to the situation in which they had actually bounced off each other.)

Of course, the balls will not meet if the meeting point is outside the unit square, in which case the balls will hit the boundary. At a boundary a ball will reflect off in a direction such that the angle of reflection is equal to the angle of incidence with the same magnitude of velocity.

**Note:** This assignment is an adaptation of an assignment given
by Luc Devroye at Mcgill University, Canada in his data structures course.