Game Development Reference
In-Depth Information
starter activity reside within the package com.badlogic.androidgames.gladvanced . Some of the
classes will be part of our framework and will go into the respective framework packages.
Vectors in 3D
In Chapter 8, we discussed vectors and their interpretation in 2D. As you might have guessed,
all of the things we discussed there still hold in 3D space as well. All we need to do is to add one
more coordinate to our vector, namely the z coordinate.
The operations we looked at with vectors in 2D can be easily transferred to 3D space. We
specify vectors in 3D with a statement like the following:
v = (x,y,z)
Addition in 3D is carried out as follows:
c = a + b = (a.x, a.y, b.z) + (b.x, b.y, b.z) = (a.x + b.x, a.y + b.y, a.z + b.z)
Subtraction works exactly the same way:
c = a - b = (a.x, a.y, b.z) - (b.x, b.y, b.z) = (a.x - b.x, a.y - b.y, a.z - b.z)
Multiplying a vector by a scalar works like this:
a' = a * scalar = (a.x * scalar, a.y * scalar, a.z * scalar)
Measuring the length of a vector in 3D is also quite simple; we just add the z coordinate to the
Pythagorean equation:
|a| = sqrt(a.x * a.x + a.y * a.y + a.z * a.z)
Based on this, we can also normalize our vectors to unit length again:
a' = (a.x / |a|, a.y / |a|, a.z / |a|)
All of the interpretations of vectors we talked about in Chapter 8 hold in 3D as well:
ï?®
Positions are just denoted by a normal vector's x, y, and z coordinates.
ï?®
Velocities and accelerations can also be represented as 3D vectors. Each
component then represents a certain quantity of the attribute on one axis,
such as meters per second (m/s) in the case of velocity or meters per
second per second (m/s 2 ) for acceleration.
ï?®
We can represent directions (or axes) as simple 3D unit vectors. We did that
in Chapter 8 when we used the rotation facilities of OpenGL ES.
ï?®
We can measure distances by subtracting the starting vector from the end
vector and measuring the resulting vector's length.
One more operation that can be rather useful is rotation of a 3D vector around a 3D axis. We
used this principle earlier via the OpenGL ES glRotatef() method. However, we can't use it
to rotate one of the vectors that we'll use to store positions or directions of our game objects,
because it only works on vertices that we submit to the GPU. Luckily, there's a Matrix class
in the Android API that allows us to emulate what OpenGL ES does on the GPU. Let's write a
Vector3 class that implements all of these features. Listing 11-1 shows the code, which we'll
again explain along the way.
 
Search WWH ::




Custom Search