Game Development Reference
In-Depth Information
a translation by 200 units on the x axis and a translation by 300 units on the y axis. As we are
working in 2D, we simply ignore the z axis and set the translation component to 0. We didn't
specify a z coordinate for our vertices, so these will default to 0. Adding 0 to 0 equals 0, so our
vertices will stay in the x-y plane.
From this point on, the model-view matrix of OpenGL ES encodes a translation by (200,300,0),
which will be applied to all vertices that pass through the OpenGL ES pipeline. If you refer
back to Figure 7-4 , you'll see that OpenGL ES will multiply each vertex with the model-view
matrix first and then apply the projection matrix. Until now, the model-view matrix was set to an
identity matrix (the default of OpenGL ES); therefore, it did not have an effect on our vertices.
Our little glTranslatef() call changes this, and it will move all vertices first before they are
projected.
This is, of course, done on the fly; the values in our Vertices instance do not change at all. We
would have noticed any permanent change to our Vertices instance, because, by that logic, the
projection matrix would have changed it already.
An Initial Example Using Translation
What can we use translation for? Say we want to render 100 Bobs at different positions in our
world. Additionally, we want them to move around on the screen and change direction each
time they hit an edge of the screen (or rather, a plane of our parallel projection view frustum,
which coincides with the extents of our screen). We could do this by having one large Vertices
instance that holds the vertices of the 100 rectangles—one for each Bob—and recalculate the
vertex positions of each frame. The easier method is to have one small Vertices instance that
holds only a single rectangle (the model of Bob) and reuse it by translating it with the model-view
matrix on the fly. Let's define our Bob model:
Vertices bobModel = new Vertices(glGraphics, 4, 12, false , true );
bobModel.setVertices( new float [] { −16, -16, 0, 1,
16, -16, 1, 1,
16, 16, 1, 0,
-16, 16, 0, 0, }, 0, 8);
bobModel.setIndices( new short [] {0, 1, 2, 2, 3, 0}, 0, 6);
So, each Bob is 32×32 units in size. We also texture map him—we'll use bobrgb888.png to see
the extents of each Bob.
Bob Becomes a Class
Let's define a simple Bob class. It will be responsible for holding a Bob instance's position and
advancing his position in his current direction based on the delta time, just like we advanced
Mr. Nom (with the difference being that we don't move in a grid anymore). The update() method
will also make sure that Bob doesn't escape our view volume bounds. Listing 7-12 shows the
Bob class.
 
Search WWH ::




Custom Search