Package de.arstwo.twotil.math
Class Vector4f
java.lang.Object
de.arstwo.twotil.math.Vector4f
- All Implemented Interfaces:
Cloneable
A 3D Vector implentation (Quaternion) using floats.
Describing this as a Quaternion allows for faster tranformation in 3D space using an appropriate Matrix4f. A vector can either be a direction or a position (point in space). A direction has by definition no origin, while a position is relative to the origin at (0,0,0).
Calculations are done in place, so no additional memory is allocated by operations.
Uses a FloatBuffer which is compatible with OpenGL and similar imeplementations.
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionadd(float x, float y, float z) Adds the given values to this vector.Adds the given vector to this vector.addX(float x) Adds a specific value to the x component of this vector.addY(float y) Adds a specific value to the y component of this vector.addZ(float z) Adds a specific value to the z component of this vector.buffer()Returns the underlying FloatBuffer for direct access.clone()Calculates the cross product between this vector and another.floatCalculates the dot product between this vector and another.booleanbooleanfloatgetX()Returns the X component of this vector.floatgetY()Returns the Y component of this vector.floatgetZ()Returns the Z component of this vector.inthashCode()booleanChecks if this vector is a directional vector.booleanChecks if this vector is a position (point in space) vector.floatlength()Returns the length of this vector.floatReturns the squared length of this vector.Normalizes this vector to a length of 1.scale(float magnitude) Scales this vector by the given magnitude.set(float[] data) Sets all values of this vector to those of a given float array.set(float x, float y, float z) Sets the vector to the given values without changing the W value.Copy constructor.Sets this vector to be a direactional vector.Sets this vector to be a position (point in space) vector.protected Vector4fsetUnchecked(float[] data) Internal function to set the data of this vector without checking it.setX(float x) Sets the x value of this vector.setY(float y) Sets the y value of this vector.setZ(float z) Sets the z value of this vector.setZero()Sets the x,y,z content of this vector to all zero.Sets the x,y,z content of this vector to all zero and turns it into a directional vector.Sets the x,y,z content of this vector to all zero and turns it into a position (point in space).shear(float x, float y, float z) Shears this vector with the given vector components.shearX(float x) Shears this vector on the x-axis only.shearY(float y) Shears this vector on the y-axis only.shearZ(float z) Shears this vector on the z-axis only.sub(float x, float y, float z) Subtracts the given values to this vector.Subtracts the given vector to this vector.subX(float x) Subtracts a specific value to the x component of this vector.subY(float y) Subtracts a specific value to the y component of this vector.subZ(float z) Subtracts a specific value to the z component of this vector.toString()Transforms this vector using the given matrix.Translates this vector using the given matrix.
-
Field Details
-
buffer
-
data
protected final float[] data -
LENGTH
public static final int LENGTH- See Also:
-
SIZE_BYTE
public static final int SIZE_BYTE- See Also:
-
X
public static final int X- See Also:
-
Y
public static final int Y- See Also:
-
Z
public static final int Z- See Also:
-
W
public static final int W- See Also:
-
-
Constructor Details
-
Vector4f
public Vector4f()Create a new empty vector with all values set to 0. -
Vector4f
public Vector4f(float x, float y, float z, boolean isPosition) Creates a new vector with the given values.- Parameters:
x- any floaty- any floatz- any floatisPosition- determines if the vector is a position (point in space) or direction.
-
-
Method Details
-
set
Copy constructor. Creates a new Vector4f with all values set to other.- Parameters:
other- any other Vector4f- Returns:
- a new Vector4f with the same values as other.
-
set
Sets the vector to the given values without changing the W value.- Parameters:
x- any floaty- any floatz- any float- Returns:
- this vector.
-
setX
Sets the x value of this vector.- Parameters:
x- any float- Returns:
- this vector.
-
setY
Sets the y value of this vector.- Parameters:
y- any float- Returns:
- this vector.
-
setZ
Sets the z value of this vector.- Parameters:
z- any float- Returns:
- this vector.
-
set
Sets all values of this vector to those of a given float array.This can be used to copy external data into a Vector4f.
- Parameters:
data- any array of exactly 4 floats.- Returns:
- this vector, with all data set to the external values.
-
setUnchecked
Internal function to set the data of this vector without checking it.- Parameters:
data- any array of exactly 4 floats.- Returns:
- this vector, with all data set to the new values.
-
setZero
Sets the x,y,z content of this vector to all zero.- Returns:
- this vector.
-
setZeroPosition
Sets the x,y,z content of this vector to all zero and turns it into a position (point in space).- Returns:
- this vector.
-
setZeroDirection
Sets the x,y,z content of this vector to all zero and turns it into a directional vector.- Returns:
- this vector.
-
isDirection
public boolean isDirection()Checks if this vector is a directional vector.- Returns:
- true if it is a directional vector, false othewise.
-
isPosition
public boolean isPosition()Checks if this vector is a position (point in space) vector.- Returns:
- true if it is a position vector, false othewise.
-
setIsDirection
Sets this vector to be a direactional vector.- Returns:
- this vector.
-
setIsPosition
Sets this vector to be a position (point in space) vector.- Returns:
- this vector.
-
lengthSquared
public float lengthSquared()Returns the squared length of this vector.- Returns:
- the squared length of this vector.
-
length
public float length()Returns the length of this vector.- Returns:
- the length of this vector.
-
normalize
Normalizes this vector to a length of 1.- Returns:
- this vector.
-
add
Adds the given values to this vector.- Parameters:
x- any floaty- any floatz- any float- Returns:
- this vector.
-
add
Adds the given vector to this vector.- Parameters:
other- any other Vector4f.- Returns:
- this vector.
-
addX
Adds a specific value to the x component of this vector.- Parameters:
x- any float- Returns:
- this vector.
-
addY
Adds a specific value to the y component of this vector.- Parameters:
y- any float- Returns:
- this vector.
-
addZ
Adds a specific value to the z component of this vector.- Parameters:
z- any float- Returns:
- this vector.
-
sub
Subtracts the given values to this vector.- Parameters:
x- any floaty- any floatz- any float- Returns:
- this vector.
-
sub
Subtracts the given vector to this vector.- Parameters:
other- any other Vector4f.- Returns:
- this vector.
-
subX
Subtracts a specific value to the x component of this vector.- Parameters:
x- any float- Returns:
- this vector.
-
subY
Subtracts a specific value to the y component of this vector.- Parameters:
y- any float- Returns:
- this vector.
-
subZ
Subtracts a specific value to the z component of this vector.- Parameters:
z- any float- Returns:
- this vector.
-
shear
Shears this vector with the given vector components.- Parameters:
x- any floaty- any floatz- any float- Returns:
- this vector.
-
shearX
Shears this vector on the x-axis only.- Parameters:
x- any float.- Returns:
- this vector.
-
shearY
Shears this vector on the y-axis only.- Parameters:
y- any float.- Returns:
- this vector.
-
shearZ
Shears this vector on the z-axis only.- Parameters:
z- any float.- Returns:
- this vector.
-
scale
Scales this vector by the given magnitude.- Parameters:
magnitude- any float.- Returns:
- this vector.
-
dot
Calculates the dot product between this vector and another.- Parameters:
other- any other Vector4f- Returns:
- this vector.
-
cross
Calculates the cross product between this vector and another.- Parameters:
other- any other Vector4f- Returns:
- this vector.
-
transform
Transforms this vector using the given matrix.- Parameters:
matrix- any Matrix4f- Returns:
- this vector.
-
translate
Translates this vector using the given matrix.Note that this has no effect if this vector is a directional vector.
- Parameters:
matrix- any Matrix4f- Returns:
- this vector.
-
buffer
Returns the underlying FloatBuffer for direct access.- Returns:
- the underlying FloatBuffer.
-
getX
public float getX()Returns the X component of this vector.- Returns:
- the X component of this vector.
-
getY
public float getY()Returns the Y component of this vector.- Returns:
- the Y component of this vector.
-
getZ
public float getZ()Returns the Z component of this vector.- Returns:
- the Z component of this vector.
-
hashCode
public int hashCode() -
equals
-
equals
-
clone
-
toString
-