Game Development Reference
In-Depth Information
this
.z = other.z;
return this
;
}
Like
Vector2
, our
Vector3
class has a couple of constructors and setters and a
cpy()
method, so
that we can easily clone vectors or set them from components calculated in our program.
public
Vector3 add(
float
x,
float
y,
float
z) {
this
.x += x;
this
.y += y;
this
.z += z;
return this
;
}
public
Vector3 add(Vector3 other) {
this
.x += other.x;
this
.y += other.y;
this
.z += other.z;
return this
;
}
public
Vector3 sub(
float
x,
float
y,
float
z) {
this
.x -= x;
this
.y -= y;
this
.z -= z;
return this
;
}
public
Vector3 sub(Vector3 other) {
this
.x -= other.x;
this
.y -= other.y;
this
.z -= other.z;
return this
;
}
public
Vector3 mul(
float
scalar) {
this
.x *= scalar;
this
.y *= scalar;
this
.z *= scalar;
return this
;
}
The various
add()
,
sub()
, and
mul()
methods are just an extension of what we had in our
Vector2
class with an additional z coordinate. They implement what we discussed a few pages
ago. Straightforward, right?
public float
len() {
return
FloatMath.
sqrt
(x * x + y * y + z * z);
}
public
Vector3 nor() {
float
len = len();