Game Development Reference
In-Depth Information
Listing 11-1. Vector3.java, a Vector in 3D
package
com.badlogic.androidgames.framework.math;
import
android.opengl.Matrix;
import
android.util.FloatMath;
public class
Vector3 {
private static final float
[]
matrix
=
new float
[16];
private static final float
[]
inVec
=
new float
[4];
private static final float
[]
outVec
=
new float
[4];
public float
x, y, z;
The class starts with a couple of private static final float arrays. We'll need them later on when
we implement the new
rotate()
method of our
Vector3
class. Just remember that the
matrix
member has 16 elements and the
inVec
and
outVec
members each have 4 elements. We create
those three arrays so that we don't have to create them later on, all the time. This will keep the
garbage collector happy. Just remember that by doing so, the
Vector3
class is not thread safe!
The
x
,
y
, and
z
members defined next should be self-explanatory. They store the actual
components of the vector:
public
Vector3() {
}
public
Vector3(
float
x,
float
y,
float
z) {
this
.x = x;
this
.y = y;
this
.z = z;
}
public
Vector3(Vector3 other) {
this
.x = other.x;
this
.y = other.y;
this
.z = other.z;
}
public
Vector3 cpy() {
return new
Vector3(x, y, z);
}
public
Vector3 set(
float
x,
float
y,
float
z) {
this
.x = x;
this
.y = y;
this
.z = z;
return this
;
}
public
Vector3 set(Vector3 other) {
this
.x = other.x;
this
.y = other.y;