Problem statement:
This is a project 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 the screen size. 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:
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 (perhaps on your mobile phone?) 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 or a full stretch of a string. For each ball, you have to keep track of the next event. Note that sorting will be an overkill, because you just have to keep track of the next event (minimum time). What you really need is data type called a priority queue.
Of course, you can implement a priority queue using a flat list, but it will be a tad inefficient. You may find it worthwhile to read up about a data structure called heap (especially binary heaps) to keep track of the next events.
The next events may be 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 colour, 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! You may add more spice to your project
by building a provision for hitting a ball with your mouse, and giving it a new direction
and velocity!
For the graphics part you can use Java applets. You can look at the following applet demos for tips:
You should also look at the html page source (press Ctrl-U in your browser).You may face some problems with running Java applets in standard browsers (IE or Netscape). You may be better off testting your programs with /usr/bin/appletviewer on CSC Linux machines. You may also run it on a windows or linux machine provided JDK is available.
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:
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 is an adaptation of an assignment given
by Luc Devroye at Mcgill University, Canada in a course.