Game Development Reference
In-Depth Information
Gravitational acceleration is typically represented by a lowercase g , and we will use that
convention throughout this topic.
One characteristic of gravitational force is that it is always “turned on.” Every object on
Earth is always subject to Earth's gravitational force. Gravitational force and its corresponding
acceleration will be a part of almost every physical model that we will develop in this topic and
that you will use in your game programming. Gravitational force determines the trajectories of
projectiles and is a major influence in the motion and performance of cars, boats, airplanes,
and rockets.
The Gravity Game
Throughout this topic, we will develop sample games that will demonstrate how to code up the
various physical models being discussed. Since we just learned about gravitational force, the
first game we will write will be the Gravity Game. Like all of the games in this topic, the Gravity
Game is written in Java, but much of the syntax should be familiar to someone who programs
in C, C++, or C#. The full source code for all of the games presented in the topic can be down-
loaded from the Apress website at www.apress.com . You will find C and C# versions of all the
games at the Apress website as well.
The Gravity Game, as the name would suggest, is intended to demonstrate the effects of
gravity. A sample screen shot of the game is shown in Figure 3-1. The objective of the game is
to release a ball at just the right time so it falls into a rectangular box that is sliding across the
ground. The box begins at the left-hand side of the display. When the Start button is pressed,
the box begins to move to the right. The ball starts at a height of 120 m above the ground. When
the Drop button is pressed, the ball is released and begins to fall to the ground. If the ball falls
into the box, the message “You Win” appears in the Results text field. If you miss the box, the
message “Try Again” appears.
Figure 3-1. Gravity Game screen shot
There are two user inputs that can be adjusted. The first is the speed that the box will move
across the ground. The user can also select the planet where the simulation will take place. The
choices are “Earth,” “Moon,” or “Jupiter.” The gravitational acceleration will be different
depending on which planet is selected. The ball will fall faster on Jupiter than it will on the moon.
Search WWH ::




Custom Search