Game Development Reference
In-Depth Information
This code is so trivial that you probably don't need any explanation. The only hitch is that
we have to store the same position twice: once as the position member in the
GameObject3D
class, and again within the position member of the
Sphere
instance that's contained in the
GameObject3D
class. This is somewhat ugly but, for the sake of clarity, we'll stick to it.
Deriving a
DynamicGameObject3D
class from this class is also simple. Listing 11-17 shows
the code.
Listing 11-17. DynamicGameObject3D.java, the Dynamic Equivalent to GameObject3D
package
com.badlogic.androidgames.framework;
import
com.badlogic.androidgames.framework.math.Vector3;
public class
DynamicGameObject3D
extends
GameObject3D {
public final
Vector3 velocity;
public final
Vector3 accel;
public
DynamicGameObject3D(
float
x,
float
y,
float
z,
float
radius) {
super
(x, y, z, radius);
velocity =
new
Vector3();
accel =
new
Vector3();
}
}
We again just replace any
Vector2
instance with a
Vector3
instance and smile happily.
In 2D, we had to think hard about the relationship between the graphical representation of our
objects (given in pixels) and the units used within the model of our world. In 3D, we can break
free from this! The vertices of our 3D models that we load from, say, an OBJ file can be defined
in whatever unit system we want. We no longer need to transform pixels to world units and vice
versa. This makes working in 3D a little easier. We just need to train our artist so that he or she
provides us with models that are properly scaled to the unit system of our world.
Summary
Again, we have uncovered a lot of mysteries in the world of game programming. We talked a
little bit about vectors in 3D, which turned out to be as simple to use as their 2D counterparts.
The general theme is: just add a z coordinate! We also took a look at lighting in OpenGL ES.
With the helper classes we wrote to represent materials and light sources, it is pretty simple
to set up the lighting in a scene. For better performance and fewer graphical artifacts, we also
implemented simple mipmapping as part of our
Texture
class. We also explored implementation
of simple Euler and look-at cameras, using very little code and a little help from the
Matrix
class.
Since creation of 3D meshes by hand in code is tedious, we also looked at one of the simplest
and most popular 3D file formats: Wavefront OBJ. We revisited our simple physics model and
transferred it to the realm of 3D, which turned out to be as simple as creating 3D vectors.