Visit the "Config" tab. You should see something like this:
At the moment, these 2 sliders will not have any visible effect. But as you
start to implement the misssing pieces in your skeleton programs, you will
find them useful for debugging. Finally check that the "About" tab shows
the information from your customized "AboutMe" class.
[3] Edit the Particle2D class
In order to get the particle moving in the simulation model, you must implement
the "doUpdate" method of Particle2D to update the particle's postion (xPosition,yPosition)
and velocity (xVelocity,yVelocity) variables, given the components of a force
(xForce, yForce) being applied for an interval (timeStep). You can assume
that the time interval is short enough that a linear solution is OK. The
units of distance are pixels and the units of time are seconds.
Your implementation will probably include lines like this:
xVelocity= ...fill this in... ;
yVelocity= ...fill this in... ;
xPosition= ...fill this in... ;
yPosition= ...fill this in... ;
Once this is working, your particle should start travelling diagonally down
to the right and leave the window after about 6 seconds of elapsed model
time. You will see a thin red line that traces out the path the particle
has followed. Your window should look something like this after about 6 seconds
of model time:
If you are having trouble, a simple debugging technique you might find useful
is to slow down the model update rate from the default 10 Hz to 1 Hz (on
the "Config" tab), and then add some text output that is printed for each
update. For example:
System.out.println("The new velocity is " + xVelocity
+ "(x) , " + yVelocity + "(y)");
When you have uniform motion working correctly, turn on some gravity at the
"Config" tab to generate a non-zero yForce and check the results.
Complete your editing of Particle2D.java by implementing the "getXMomentum",
"getYMomentum" and "applyImpulse" methods, following the directions in the
comments. You will not be able to check that these operate correctly until
you complete the next part of the assignment, but at least check that they
compile correctly.
[4] Edit the CollisionManager class
In order to get the particle to bounce off the walls of its container, you
will now implement two methods of the CollisionManager class. Both of these
methods have the same name (nextCollisionTime) but take different arguments:
this is legal in most Object-Oriented (OO) languages and is actually a powerful
feature usually known as "overloading".
Start by implementing the version of "nextCollisionTime" that takes single
Particle2D and Wall2D objects as inputs, according to the specifications
in the comments. Refer to the Wall2D.java that you downloaded earlier for
information on using Wall2D objects. Hint: you will probably find the getXNorm(
) and getYNorm() methods useful.
Next implement the other version of "nextCollisionTime" that takes a set
of particles and walls as inputs. This version should loop over (particle,wall)
pairs and then delegate most of its work to the first version, eg, with method
calls like:
Particle2D particle = ... ;
Wall2D wall = ... ;
...
double when= nextCollisionTime(particle,wall);
The sets of particles and walls are provided as "Iterators". Refer to
this
page for general documetation on using the Java Iterator class. Iterators
are a common OO technique for traversing the contents of a "container class"
(a
LinkedList
in this case). In addition to returning the next collision time (if any),
this method must remember the (particle,wall) pair that are responsible for
the next collision: store these in the variables bouncedParticle and bouncedWall.
If you forget to do this, you will get an error message when you run your
program:
Missing references to particle and/or wall for
last collision
Once you are correctly calculating collision times, your program should look
something like this when it runs:
The hollow circles represent the locations where the particle makes contact
with a wall (according to your implementation of the CollisionManager) and
therefore receives an impulse "kick". Remember to double-check your Particle2D
"getXMomentum", "getYMomentum" and "applyImpulse" methods now if your program
is not working correctly. The debugging technique suggested above will also
be helpful here.
Both of these implementations can potentially be quite inefficient. But,
in general (and in this case also), you should concentrate on a clear and
correct implementation. Optimization can come later, when and where it is
needed.