Game Development Reference
In-Depth Information
Figure 4-5. Spring location as a function of time
One great thing about the ODE solver as we have written it is that it is completely general
to virtually any time of initial boundary value ODE. You never have to change the code in the
ODE or ODESolver classes. Whenever you have a need to solve an ODE in your game programming,
simply write the appropriate ODE subclass and invoke the solver in the game code.
Spring Motion Simulator
Let's write a simple program that simulates the motion of a spring and demonstrates how easy
it is to incorporate an ODE subclass object into a simulation. A typical screen shot of the Spring
Simulator is shown in Figure 4-6. A spring with a mass hanging on one end is displayed in the
right-hand side of the GUI. There are text fields to input the mass, spring constant, damping
coefficient, and initial location of the spring. The initial location is the distance the spring is
stretched from its equilibrium position. A Start button starts the spring moving and a Reset
button stops the simulation and returns the spring to its initial position.
Figure 4-6. Spring Simulator screen shot
The Spring Simulator is implemented by the SpringSimulator class. The GUI elements of
the class are very similar to the previous games presented in this topic and won't be discussed
in detail. As always, the full code listing can be downloaded from the Apress website. In addition to
 
Search WWH ::




Custom Search