Game Development Reference
In-Depth Information
And that's our shiny Vector2 class, which we can use to represent positions, velocities,
distances, and directions in the code that follows. To get a feeling for your new class, we'll use it
in a simple example.
A Simple Usage Example
Here's a proposal for a simple test:
ï?®
We create a sort of cannon represented by a triangle that has a fixed
position in our world. The center of the triangle will be at (2.4,0.5).
ï?®
Each time we touch the screen, we want to rotate the triangle to face the
touch point.
ï?®
Our view frustum will show the region of the world between (0,0) and
(4.8,3.2). We do not operate in pixel coordinates, but instead define our own
coordinate system, where one unit equals one meter. Also, we'll be working
in landscape mode.
There are a couple of things we need to think about. We already know how to define a triangle in
model space—we can use a Vertices instance for this. Our cannon should point to the right at an
angle of 0 degrees in its default orientation. Figure 8-4 shows the cannon triangle in model space.
Figure 8-4. The cannon triangle in model space
When we render that triangle, we simply use glTranslatef() to move it to its place in the world
at (2.4,0.5).
We also want to rotate the cannon so that its tip points in the direction of the point on the screen
that we last touched. For this, we need to figure out the location of the last touch event in the
world. The GLGame.getInput().getTouchX() and getTouchY() methods will return the touch point
in screen coordinates, with the origin in the top-left corner. The Input instance will not scale the
events to a fixed coordinate system, as it did in Mr. Nom. Intead, we need to convert these touch
coordinates to world coordinates. We already did this in the touch handlers in Mr. Nom and
the Canvas -based game framework; the only difference this time is that the coordinate system
extents are a little smaller, and our world's y axis is pointing upward. Here's the pseudocode
showing how we can achieve the conversion in a general case, which is nearly the same as in
the touch handlers of Chapter 5:
 
Search WWH ::




Custom Search