Package com.simsilica.mathd
Class Vec3d
java.lang.Object
com.simsilica.mathd.Vec3d
- All Implemented Interfaces:
Cloneable
A vector composed of 3 double-precision components, used to represent
locations, offsets, velocities, and directions in 3-dimensional space.
Methods with names ending in "Local" modify the current instance. They are used to avoid creating temporary vectors.
The conventional order of components is (X, Y, Z).
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Vec3dShared instance of the +X direction (1,0,0).static final Vec3dShared instance of the +Y direction (0,1,0).static final Vec3dShared instance of the +Z direction (0,0,1).doubleThe first (X) component.doubleThe 2nd (Y) component.doubleThe 3rd (Z) component.static final Vec3dShared instance of the all-zero vector (0,0,0). -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfinal Vec3dadd(double vx, double vy, double vz) Adds specified amounts to the vector's components and returns the sum as a new instance.final Vec3dAdds the argument and returns the sum as a new instance.final Vec3daddLocal(double vx, double vy, double vz) Adds specified amounts to the vector's components and returns the (modified) current instance.final Vec3dAdds the argument and returns the (modified) current instance.final Vec3dclone()Creates a copy.final Vec3dCalculates a cross product with the argument and returns the product as a new instance.final Vec3ddivideLocal(Vec3d v) Divides component-wise by the argument and returns the (modified) current instance.final doubleReturns the dot (or inner) product with the argument.booleanTests for exact equality with the argument, distinguishing -0 from 0.doubleget(int i) Returns the indexed component.inthashCode()Returns a hash code.booleanisFinite()Tests for finite components.final doublelength()Returns the length (or magnitude).final doublelengthSq()Returns the square of the length.final Vec3dmultLocal(double s) Multiplies by the scalar argument and returns the (modified) current instance.final Vec3dMultiplies component-wise by the argument and returns the (modified) current instance.final Vec3dNormalize this vector in place.final Vec3dset(double x, double y, double z) Sets all 3 components to specified values.set(int i, double d) Alters the indexed component.final Vec3dCopies all 3 components from the argument, converting them to double precision.final Vec3dCopies all 3 components from the argument.final Vec3dSubtracts the argument and returns the difference as a new instance.final Vec3dSubtracts the argument and returns the (modified) current instance.toString()Returns a string representation of the vector, which is unaffected.Creates a single-precision copy of the current instance.
-
Field Details
-
UNIT_X
Shared instance of the +X direction (1,0,0). Do not modify! -
UNIT_Y
Shared instance of the +Y direction (0,1,0). Do not modify! -
UNIT_Z
Shared instance of the +Z direction (0,0,1). Do not modify! -
ZERO
Shared instance of the all-zero vector (0,0,0). Do not modify! -
x
public double xThe first (X) component. -
y
public double yThe 2nd (Y) component. -
z
public double zThe 3rd (Z) component.
-
-
Constructor Details
-
Vec3d
public Vec3d()Instantiates an all-zero vector (0,0,0). -
Vec3d
public Vec3d(double x, double y, double z) Instantiates a vector with the specified components.- Parameters:
x- the desired X componenty- the desired Y componentz- the desired Z component
-
Vec3d
Instantiates a copy of the argument.- Parameters:
v- the Vec3d to copy (not null, unaffected)
-
Vec3d
Instantiates a double-precision copy of the argument.- Parameters:
v- the input Vector3f (not null, unaffected)
-
-
Method Details
-
toVector3f
Creates a single-precision copy of the current instance.- Returns:
- a new Vector3f
-
hashCode
public int hashCode()Returns a hash code. If two vectors have identical values, they will have the same hash code. The current instance is unaffected. -
equals
Tests for exact equality with the argument, distinguishing -0 from 0. Ifois null, false is returned. Either way, the current instance is unaffected. -
isFinite
public boolean isFinite()Tests for finite components. The vector is unaffected.- Returns:
- false if any component is infinite or NaN, otherwise true
-
set
Sets all 3 components to specified values.- Parameters:
x- the desired X componenty- the desired Y componentz- the desired Z component- Returns:
- the (modified) current instance (for chaining)
-
set
Copies all 3 components from the argument.- Parameters:
v- the Vec3d to copy (not null, unaffected)- Returns:
- the (modified) current instance (for chaining)
-
set
Copies all 3 components from the argument, converting them to double precision.- Parameters:
v- the Vector3f to copy (not null, unaffected)- Returns:
- the (modified) current instance (for chaining)
-
clone
Creates a copy. The current instance is unaffected. -
get
public double get(int i) Returns the indexed component. The vector is unaffected.- Parameters:
i- 0, 1, or 2- Returns:
- the X component if i=0, the Y component if i=1, or the Z component if i=2
- Throws:
IndexOutOfBoundsException- ifiis not 0, 1, or 2
-
set
Alters the indexed component.- Parameters:
i- which component to set: 0 → the X component, 1 → the Y component, 2 → the Z componentd- the desired component value- Returns:
- the (modified) current instance (for chaining)
- Throws:
IllegalArgumentException- ifiis not 0, 1, or 2
-
add
Adds the argument and returns the sum as a new instance. The current instance is unaffected.- Parameters:
v- the vector to add (not null, unaffected)- Returns:
- a new Vec3d
-
add
Adds specified amounts to the vector's components and returns the sum as a new instance. The current instance is unaffected.- Parameters:
vx- the amount to add to the X componentvy- the amount to add to the Y componentvz- the amount to add to the Z component- Returns:
- a new Vec3d
-
subtract
Subtracts the argument and returns the difference as a new instance. The current instance is unaffected.- Parameters:
v- the vector to subtract (not null, unaffected)- Returns:
- a new Vec3d
-
addLocal
Adds the argument and returns the (modified) current instance.It IS safe for
vandthisto be the same object.- Parameters:
v- the vector to add (not null, unaffected unless it'sthis)- Returns:
- the (modified) current instance (for chaining)
-
addLocal
Adds specified amounts to the vector's components and returns the (modified) current instance.- Parameters:
vx- the amount to add to the X componentvy- the amount to add to the Y componentvz- the amount to add to the Z component- Returns:
- the (modified) current instance (for chaining)
-
subtractLocal
Subtracts the argument and returns the (modified) current instance.It IS safe for
vandthisto be the same object.- Parameters:
v- the vector to subtract (not null, unaffected unless it'sthis)- Returns:
- the (modified) current instance (for chaining)
-
multLocal
Multiplies by the scalar argument and returns the (modified) current instance.- Parameters:
s- the scaling factor- Returns:
- the (modified) current instance (for chaining)
-
multLocal
Multiplies component-wise by the argument and returns the (modified) current instance.It IS safe for
vandthisto be the same object.- Parameters:
v- the scale vector (not null, unaffected unless it'sthis)- Returns:
- the (modified) current instance (for chaining)
-
divideLocal
Divides component-wise by the argument and returns the (modified) current instance.It IS safe for
vandthisto be the same object.- Parameters:
v- the divisor (not null, unaffected unless it'sthis)- Returns:
- the (modified) current instance (for chaining)
-
lengthSq
public final double lengthSq()Returns the square of the length. The current instance is unaffected.- Returns:
- the sum of the squared components (not negative)
-
length
public final double length()Returns the length (or magnitude). The current instance is unaffected.- Returns:
- the root-sum of the squared components (not negative)
-
normalizeLocal
Normalize this vector in place. For a zero vector, the result is undefined.- Returns:
- the (normalized) current instance (for chaining)
-
dot
Returns the dot (or inner) product with the argument. The current instance is unaffected.- Parameters:
v- the vector to multiply (not null, unaffected)- Returns:
- the dot product
-
cross
Calculates a cross product with the argument and returns the product as a new instance. The current instance is unaffected.- Parameters:
v- the right factor (not null, unaffected)- Returns:
thiscrossv(a new Vec3d)
-
toString
Returns a string representation of the vector, which is unaffected. For example, the +X direction vector is represented by:Vec3d[1.0, 0.0, 0.0]
-