Java Reference
In-Depth Information
20.5.3.1 The Simulation.startRunning() method
Our calculation loops over time, increasing t by
T each step. This loop is a
convenient place to check the fKillMe instance variable set by the halt()
method. That is,
while (!fKillMe) {
// calculate everything ...
}
Before the loop begins, we need an array to hold our results. There are six
results returned - current simulation time, position, velocity, acceleration, and
kinetic and potential energy. As with the input data array, the client and server
must agree on the order of the array elements in the output data array. Again,
a better solution might be to define a data-only results class that would make
the order unambiguous. However, we stick with the array approach for this
example:
float[] data = new float[6];
The calculations are straightforward. First, we increment an iteration counter and
the time value and store the current time in the output array:
iter++;
time = iter * fDeltaT;
data[0] = time;
=
ω
The position of the oscillating mass is x
A sin (
t )so
data[1] = fAmplitude * (float) Math.sin (fOmega * time);
The velocity is
v = ω
A cos (
ω
t )
data[2] = fOmega * fAmplitude *
(float) Math.cos (fOmega * time);
2 A sin (
2 x
Acceleration is a
=− ω
ω
t )
=− ω
data[3] = -fOmega * fOmega * data[1];
We also calculate and store the kinetic and potential energy as a function of time.
For this example, they could be calculated on the client side from the returned
position and velocity, but we include them here anyway. One is often faced with a
decision about how to divide the work load between client and server. In general,
if there are good reasons for splitting a calculation into client and server pieces,
it usually makes sense to perform as much work on the server as possible, the
thinking being that a server machine is often a high-powered machine with more
resources than a client.
Search WWH ::




Custom Search