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
Modifier and TypeFieldDescriptionstatic final Vec3d
Shared instance of the +X direction (1,0,0).static final Vec3d
Shared instance of the +Y direction (0,1,0).static final Vec3d
Shared instance of the +Z direction (0,0,1).double
The first (X) component.double
The 2nd (Y) component.double
The 3rd (Z) component.static final Vec3d
Shared instance of the all-zero vector (0,0,0). -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionfinal Vec3d
add
(double vx, double vy, double vz) Adds specified amounts to the vector's components and returns the sum as a new instance.final Vec3d
Adds the argument and returns the sum as a new instance.final Vec3d
addLocal
(double vx, double vy, double vz) Adds specified amounts to the vector's components and returns the (modified) current instance.final Vec3d
Adds the argument and returns the (modified) current instance.final Vec3d
clone()
Creates a copy.final Vec3d
Calculates a cross product with the argument and returns the product as a new instance.final Vec3d
divideLocal
(Vec3d v) Divides component-wise by the argument and returns the (modified) current instance.final double
Returns the dot (or inner) product with the argument.boolean
Tests for exact equality with the argument, distinguishing -0 from 0.double
get
(int i) Returns the indexed component.int
hashCode()
Returns a hash code.boolean
isFinite()
Tests for finite components.final double
length()
Returns the length (or magnitude).final double
lengthSq()
Returns the square of the length.final Vec3d
multLocal
(double s) Multiplies by the scalar argument and returns the (modified) current instance.final Vec3d
Multiplies component-wise by the argument and returns the (modified) current instance.final Vec3d
Normalize this vector in place.final Vec3d
set
(double x, double y, double z) Sets all 3 components to specified values.set
(int i, double d) Alters the indexed component.final Vec3d
Copies all 3 components from the argument, converting them to double precision.final Vec3d
Copies all 3 components from the argument.final Vec3d
Subtracts the argument and returns the difference as a new instance.final Vec3d
Subtracts 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. Ifo
is 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
- ifi
is 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
- ifi
is 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
v
andthis
to 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
v
andthis
to 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
v
andthis
to 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
v
andthis
to 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:
this
crossv
(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]
-