Game Development Reference
In-Depth Information
Note that it doesn't really matter whether we render a triangle at the cannon position or render
a rectangle texture mapped to an image of a cannon—OpenGL ES doesn't really care. We
also have all the matrix operations in the present() method. The truth of the matter is that it is
easier to keep track of OpenGL ES states this way, and we can use multiple view frustums in
one present() call (for example, one view frustum setting up a world in meters, for rendering
our world, and another view frustum setting up a world in pixels, for rendering UI elements). The
impact on performance is not all that big, as described in Chapter 7, so it's acceptable to do it
this way most of the time. Just remember that you can optimize this if the need arises.
Vectors will be your best friends from now on. You can use them to specify virtually everything
in your world. You will also be able to do some very basic physics with vectors. What's a cannon
good for if it can't shoot, right?
A Little Physics in 2D
In this section, we'll discuss a very simple and limited version of physics. Games are all about
being good fakes. They cheat wherever possible in order to avoid potentially heavy calculations.
The behavior of objects in a game does not need to be 100 percent physically accurate; it
just needs to be good enough to look believable. Sometimes you won't even want physically
accurate behavior (that is, you might want one set of objects to fall downward, and another,
crazier, set of objects to fall upward).
Even the original Super Mario Brothers used at least some basic principles of Newtonian
physics. These principles are really simple and easy to implement. Only the absolute minimum
required for implementing a simple physics model for our game objects will be discussed.
Newton and Euler, Best Friends Forever
Our main concern is with the motion physics of so-called point masses . Motion physics
describes the change in position, velocity, and acceleration of an object over time. Point mass
means that all objects are approximated with an infinitesimally small point that has an associated
mass. We do not have to deal with things like torque—the rotational velocity of an object around
its center of mass—because that is a complex problem domain about which more than one
complete book has been written. We just look at these three properties of an object:
� Position : Represented as a vector in some space—in our case, a 2D space.
Usually the position is given in meters.
ï?® Velocity : The object's change in position per second. Velocity is given as
a 2D velocity vector, which is a combination of the unit-length direction
vector in which the object is heading and the speed at which the object will
move, given in meters per second (m/s). Note that the speed just governs
the length of the velocity vector; if you normalize the velocity vector by the
speed, you get a nice unit-length direction vector.
ï?® Acceleration : The object's change in velocity per second. We can represent
this either as a scalar that only affects the speed of the velocity (the length
of the velocity vector) or as a 2D vector, so that we can have different
acceleration in the x and y axes. Here we'll choose the latter, as it allows us
 
Search WWH ::




Custom Search