Game Development Reference
In-Depth Information
simple, consisting of only three lines of executable code. Every time the method is called, the
updatePositionAndVelocity method is called on the SpringODE object to update the velocity
and location of the spring. The update is performed using the Runge-Kutta ODE solver. Once
the new values of velocity and location are obtained, the display is updated to show the new
position of the spring.
class GameUpdater implements ActionListener {
public void actionPerformed(ActionEvent event) {
// Use the ODE solver to update the location of
// the spring.
double dt = 0.05;
spring.updatePositionAndVelocity(dt);
// Update the display
updateDisplay();
}
}
As we can see from this example, once the SpringODE class is written, it's a simple process
to incorporate a spring into a game simulation. Play around with the Spring Simulator. Change
the values of the mass, spring constant, damping coefficient, and initial location and see what
happens to the motion of the spring.
Exercises
1.
A bullet is shot horizontally from a rifle at the same time a ball is released from a person's hand towards the
ground. The bullet and ball start at the same vertical distance from the ground. If the only force acting on the ball
and bullet is due to gravity, which object will strike the ground first?
2.
A soccer player is standing 5 meters straight in front of the opponent's goal when she shoots the ball. The initial
velocity of the ball is 12 m/s and the ball is initially directed upward at a 45-degree angle. The top of the goal
is 2.44 m (8 ft ) high. Assuming that the only external force on the ball is due to gravity, will the ball sail over
the goal?
The SpringODE class written in this section does not model the force of gravity, but the Spring Simulator uses
a spring that hangs vertically where gravity would be a factor. Modify the SpringODE class to include gravity in
the equations of motion.
3.
Rotational Motion
The first part of this chapter dealt with translational motion where an object moves linearly
through the air, on the ground, or under water. There is another general type of motion known
as rotational motion , which is the motion of spinning objects. Rotational motion is very important
to game programming. A golfer can use spin to increase the distance a golf shot travels or to
Search WWH ::




Custom Search