com.brackeen.javagamebook.math3D
Class Vector3D

java.lang.Object
  extended bycom.brackeen.javagamebook.math3D.Vector3D
All Implemented Interfaces:
Transformable
Direct Known Subclasses:
PointLight3D

public class Vector3D
extends java.lang.Object
implements Transformable

The Vector3D class implements a 3D vector with the floating-point values x, y, and z. Vectors can be thought of either as a (x,y,z) point or as a vector from (0,0,0) to (x,y,z).


Field Summary
 float x
           
 float y
           
 float z
           
 
Constructor Summary
Vector3D()
          Creates a new Vector3D at (0,0,0).
Vector3D(float x, float y, float z)
          Creates a new Vector3D with the specified (x, y, z) values.
Vector3D(Vector3D v)
          Creates a new Vector3D with the same values as the specified Vector3D.
 
Method Summary
 void add(float x, float y, float z)
          Adds the specified (x, y, z) values to this vector.
 void add(Transform3D xform)
          Adds the specified transform to this vector.
 void add(Vector3D v)
          Adds the specified vector to this vector.
 void addRotation(Transform3D xform)
          Rotates this vector with the angle of the specified transform.
 void divide(float s)
          Divides this vector by the specified value.
 boolean equals(float x, float y, float z)
          Checks if this Vector3D is equal to the specified x, y, and z coordinates.
 boolean equals(java.lang.Object obj)
          Checks if this Vector3D is equal to the specified Object.
 float getDistance(Vector3D v)
          Gets the distance between this vector and the specified vector.
 float getDistanceSq(Vector3D v)
          Gets the distance squared between this vector and the specified vector.
 float getDotProduct(Vector3D v)
          Returns the dot product of this vector and the specified vector.
 float length()
          Returns the length of this vector as a float.
 void multiply(float s)
          Multiplies this vector by the specified value.
 void normalize()
          Converts this Vector3D to a unit vector, or in other words, a vector of length 1.
 void rotateX(float angle)
          Rotate this vector around the x axis the specified amount.
 void rotateX(float cosAngle, float sinAngle)
          Rotate this vector around the x axis the specified amount, using pre-computed cosine and sine values of the angle to rotate.
 void rotateY(float angle)
          Rotate this vector around the y axis the specified amount.
 void rotateY(float cosAngle, float sinAngle)
          Rotate this vector around the y axis the specified amount, using pre-computed cosine and sine values of the angle to rotate.
 void rotateZ(float angle)
          Rotate this vector around the z axis the specified amount.
 void rotateZ(float cosAngle, float sinAngle)
          Rotate this vector around the y axis the specified amount, using pre-computed cosine and sine values of the angle to rotate.
 void setLength(float newLength)
          Sets the length of this Vector3D
 void setTo(float x, float y, float z)
          Sets this vector to the specified (x, y, z) values.
 void setTo(Vector3D v)
          Sets the vector to the same values as the specified Vector3D.
 void setToCrossProduct(Vector3D u, Vector3D v)
          Sets this vector to the cross product of the two specified vectors.
 void subtract(float x, float y, float z)
          Subtracts the specified (x, y, z) values to this vector.
 void subtract(Transform3D xform)
          Subtracts the specified transform to this vector.
 void subtract(Vector3D v)
          Subtracts the specified vector from this vector.
 void subtractRotation(Transform3D xform)
          Rotates this vector with the opposite angle of the specified transform.
 java.lang.String toString()
          Converts this Vector3D to a String representation.
 
Methods inherited from class java.lang.Object
getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

x

public float x

y

public float y

z

public float z
Constructor Detail

Vector3D

public Vector3D()
Creates a new Vector3D at (0,0,0).


Vector3D

public Vector3D(Vector3D v)
Creates a new Vector3D with the same values as the specified Vector3D.


Vector3D

public Vector3D(float x,
                float y,
                float z)
Creates a new Vector3D with the specified (x, y, z) values.

Method Detail

equals

public boolean equals(java.lang.Object obj)
Checks if this Vector3D is equal to the specified Object. They are equal only if the specified Object is a Vector3D and the two Vector3D's x, y, and z coordinates are equal.


equals

public boolean equals(float x,
                      float y,
                      float z)
Checks if this Vector3D is equal to the specified x, y, and z coordinates.


setTo

public void setTo(Vector3D v)
Sets the vector to the same values as the specified Vector3D.


setTo

public void setTo(float x,
                  float y,
                  float z)
Sets this vector to the specified (x, y, z) values.


add

public void add(float x,
                float y,
                float z)
Adds the specified (x, y, z) values to this vector.


subtract

public void subtract(float x,
                     float y,
                     float z)
Subtracts the specified (x, y, z) values to this vector.


add

public void add(Vector3D v)
Adds the specified vector to this vector.

Specified by:
add in interface Transformable

subtract

public void subtract(Vector3D v)
Subtracts the specified vector from this vector.

Specified by:
subtract in interface Transformable

multiply

public void multiply(float s)
Multiplies this vector by the specified value. The new length of this vector will be length()*s.


divide

public void divide(float s)
Divides this vector by the specified value. The new length of this vector will be length()/s.


length

public float length()
Returns the length of this vector as a float.


normalize

public void normalize()
Converts this Vector3D to a unit vector, or in other words, a vector of length 1. Same as calling v.divide(v.length()).


toString

public java.lang.String toString()
Converts this Vector3D to a String representation.


rotateX

public void rotateX(float angle)
Rotate this vector around the x axis the specified amount. The specified angle is in radians. Use Math.toRadians() to convert from degrees to radians.


rotateY

public void rotateY(float angle)
Rotate this vector around the y axis the specified amount. The specified angle is in radians. Use Math.toRadians() to convert from degrees to radians.


rotateZ

public void rotateZ(float angle)
Rotate this vector around the z axis the specified amount. The specified angle is in radians. Use Math.toRadians() to convert from degrees to radians.


rotateX

public void rotateX(float cosAngle,
                    float sinAngle)
Rotate this vector around the x axis the specified amount, using pre-computed cosine and sine values of the angle to rotate.


rotateY

public void rotateY(float cosAngle,
                    float sinAngle)
Rotate this vector around the y axis the specified amount, using pre-computed cosine and sine values of the angle to rotate.


rotateZ

public void rotateZ(float cosAngle,
                    float sinAngle)
Rotate this vector around the y axis the specified amount, using pre-computed cosine and sine values of the angle to rotate.


add

public void add(Transform3D xform)
Adds the specified transform to this vector. This vector is first rotated, then translated.

Specified by:
add in interface Transformable

subtract

public void subtract(Transform3D xform)
Subtracts the specified transform to this vector. This vector translated, then rotated.

Specified by:
subtract in interface Transformable

addRotation

public void addRotation(Transform3D xform)
Rotates this vector with the angle of the specified transform.

Specified by:
addRotation in interface Transformable

subtractRotation

public void subtractRotation(Transform3D xform)
Rotates this vector with the opposite angle of the specified transform.

Specified by:
subtractRotation in interface Transformable

getDotProduct

public float getDotProduct(Vector3D v)
Returns the dot product of this vector and the specified vector.


setToCrossProduct

public void setToCrossProduct(Vector3D u,
                              Vector3D v)
Sets this vector to the cross product of the two specified vectors. Either of the specified vectors can be this vector.


getDistanceSq

public float getDistanceSq(Vector3D v)
Gets the distance squared between this vector and the specified vector.


getDistance

public float getDistance(Vector3D v)
Gets the distance between this vector and the specified vector.


setLength

public void setLength(float newLength)
Sets the length of this Vector3D