Cloneable
, ConfigurationSerializable
BlockVector
public class Vector extends Object implements Cloneable, ConfigurationSerializable
clone()
in order to get a copy.Modifier and Type | Field | Description |
---|---|---|
protected double |
x |
|
protected double |
y |
|
protected double |
z |
Constructor | Description |
---|---|
Vector() |
Construct the vector with all components as 0.
|
Vector(double x,
double y,
double z) |
Construct the vector with provided double components.
|
Vector(float x,
float y,
float z) |
Construct the vector with provided float components.
|
Vector(int x,
int y,
int z) |
Construct the vector with provided integer components.
|
Modifier and Type | Method | Description |
---|---|---|
@NotNull Vector |
add(@NotNull Vector vec) |
Adds a vector to this one
|
float |
angle(@NotNull Vector other) |
Gets the angle between this vector and another in radians.
|
void |
checkFinite() |
Check if each component of this Vector is finite.
|
@NotNull Vector |
clone() |
Get a new vector.
|
@NotNull Vector |
copy(@NotNull Vector vec) |
Copies another vector
|
@NotNull Vector |
crossProduct(@NotNull Vector o) |
Calculates the cross product of this vector with another.
|
static @NotNull Vector |
deserialize(@NotNull Map<String,Object> args) |
|
double |
distance(@NotNull Vector o) |
Get the distance between this vector and another.
|
double |
distanceSquared(@NotNull Vector o) |
Get the squared distance between this vector and another.
|
@NotNull Vector |
divide(@NotNull Vector vec) |
Divides the vector by another.
|
double |
dot(@NotNull Vector other) |
Calculates the dot product of this vector with another.
|
boolean |
equals(Object obj) |
Checks to see if two objects are equal.
|
int |
getBlockX() |
Gets the floored value of the X component, indicating the block that
this vector is contained with.
|
int |
getBlockY() |
Gets the floored value of the Y component, indicating the block that
this vector is contained with.
|
int |
getBlockZ() |
Gets the floored value of the Z component, indicating the block that
this vector is contained with.
|
@NotNull Vector |
getCrossProduct(@NotNull Vector o) |
Calculates the cross product of this vector with another without mutating
the original.
|
static double |
getEpsilon() |
Get the threshold used for equals().
|
static @NotNull Vector |
getMaximum(@NotNull Vector v1,
@NotNull Vector v2) |
Gets the maximum components of two vectors.
|
@NotNull Vector |
getMidpoint(@NotNull Vector other) |
Gets a new midpoint vector between this vector and another.
|
static @NotNull Vector |
getMinimum(@NotNull Vector v1,
@NotNull Vector v2) |
Gets the minimum components of two vectors.
|
static @NotNull Vector |
getRandom() |
Gets a random vector with components having a random value between 0
and 1.
|
double |
getX() |
Gets the X component.
|
double |
getY() |
Gets the Y component.
|
double |
getZ() |
Gets the Z component.
|
int |
hashCode() |
Returns a hash code for this vector
|
boolean |
isInAABB(@NotNull Vector min,
@NotNull Vector max) |
Returns whether this vector is in an axis-aligned bounding box.
|
boolean |
isInSphere(@NotNull Vector origin,
double radius) |
Returns whether this vector is within a sphere.
|
boolean |
isNormalized() |
Returns if a vector is normalized
|
double |
length() |
Gets the magnitude of the vector, defined as sqrt(x^2+y^2+z^2).
|
double |
lengthSquared() |
Gets the magnitude of the vector squared.
|
@NotNull Vector |
midpoint(@NotNull Vector other) |
Sets this vector to the midpoint between this vector and another.
|
@NotNull Vector |
multiply(double m) |
Performs scalar multiplication, multiplying all components with a
scalar.
|
@NotNull Vector |
multiply(float m) |
Performs scalar multiplication, multiplying all components with a
scalar.
|
@NotNull Vector |
multiply(int m) |
Performs scalar multiplication, multiplying all components with a
scalar.
|
@NotNull Vector |
multiply(@NotNull Vector vec) |
Multiplies the vector by another.
|
@NotNull Vector |
normalize() |
Converts this vector to a unit vector (a vector with length of 1).
|
@NotNull Vector |
rotateAroundAxis(@NotNull Vector axis,
double angle) |
Rotates the vector around a given arbitrary axis in 3 dimensional space.
|
@NotNull Vector |
rotateAroundNonUnitAxis(@NotNull Vector axis,
double angle) |
Rotates the vector around a given arbitrary axis in 3 dimensional space.
|
@NotNull Vector |
rotateAroundX(double angle) |
Rotates the vector around the x axis.
|
@NotNull Vector |
rotateAroundY(double angle) |
Rotates the vector around the y axis.
|
@NotNull Vector |
rotateAroundZ(double angle) |
Rotates the vector around the z axis
|
@NotNull Map<String,Object> |
serialize() |
Creates a Map representation of this class.
|
@NotNull Vector |
setX(double x) |
Set the X component.
|
@NotNull Vector |
setX(float x) |
Set the X component.
|
@NotNull Vector |
setX(int x) |
Set the X component.
|
@NotNull Vector |
setY(double y) |
Set the Y component.
|
@NotNull Vector |
setY(float y) |
Set the Y component.
|
@NotNull Vector |
setY(int y) |
Set the Y component.
|
@NotNull Vector |
setZ(double z) |
Set the Z component.
|
@NotNull Vector |
setZ(float z) |
Set the Z component.
|
@NotNull Vector |
setZ(int z) |
Set the Z component.
|
@NotNull Vector |
subtract(@NotNull Vector vec) |
Subtracts a vector from this one.
|
@NotNull BlockVector |
toBlockVector() |
Get the block vector of this vector.
|
@NotNull Location |
toLocation(@NotNull World world) |
Gets a Location version of this vector with yaw and pitch being 0.
|
@NotNull Location |
toLocation(@NotNull World world,
float yaw,
float pitch) |
Gets a Location version of this vector.
|
String |
toString() |
Returns this vector's components as x,y,z.
|
@NotNull Vector |
zero() |
Zero this vector's components.
|
public Vector()
public Vector(int x, int y, int z)
x
- X componenty
- Y componentz
- Z componentpublic Vector(double x, double y, double z)
x
- X componenty
- Y componentz
- Z componentpublic Vector(float x, float y, float z)
x
- X componenty
- Y componentz
- Z component@NotNull public @NotNull Vector add(@NotNull @NotNull Vector vec)
vec
- The other vector@NotNull public @NotNull Vector subtract(@NotNull @NotNull Vector vec)
vec
- The other vector@NotNull public @NotNull Vector multiply(@NotNull @NotNull Vector vec)
vec
- The other vector@NotNull public @NotNull Vector divide(@NotNull @NotNull Vector vec)
vec
- The other vector@NotNull public @NotNull Vector copy(@NotNull @NotNull Vector vec)
vec
- The other vectorpublic double length()
public double lengthSquared()
public double distance(@NotNull @NotNull Vector o)
o
- The other vectorpublic double distanceSquared(@NotNull @NotNull Vector o)
o
- The other vectorpublic float angle(@NotNull @NotNull Vector other)
other
- The other vector@NotNull public @NotNull Vector midpoint(@NotNull @NotNull Vector other)
other
- The other vector@NotNull public @NotNull Vector getMidpoint(@NotNull @NotNull Vector other)
other
- The other vector@NotNull public @NotNull Vector multiply(int m)
m
- The factor@NotNull public @NotNull Vector multiply(double m)
m
- The factor@NotNull public @NotNull Vector multiply(float m)
m
- The factorpublic double dot(@NotNull @NotNull Vector other)
other
- The other vector@NotNull public @NotNull Vector crossProduct(@NotNull @NotNull Vector o)
o
- The other vector@NotNull public @NotNull Vector getCrossProduct(@NotNull @NotNull Vector o)
o
- The other vector@NotNull public @NotNull Vector normalize()
@NotNull public @NotNull Vector zero()
public boolean isInAABB(@NotNull @NotNull Vector min, @NotNull @NotNull Vector max)
The minimum and maximum vectors given must be truly the minimum and maximum X, Y and Z components.
min
- Minimum vectormax
- Maximum vectorpublic boolean isInSphere(@NotNull @NotNull Vector origin, double radius)
origin
- Sphere origin.radius
- Sphere radiuspublic boolean isNormalized()
@NotNull public @NotNull Vector rotateAroundX(double angle)
This piece of math is based on the standard rotation matrix for vectors in three dimensional space. This matrix can be found here: Rotation Matrix.
angle
- the angle to rotate the vector about. This angle is passed
in radians@NotNull public @NotNull Vector rotateAroundY(double angle)
This piece of math is based on the standard rotation matrix for vectors in three dimensional space. This matrix can be found here: Rotation Matrix.
angle
- the angle to rotate the vector about. This angle is passed
in radians@NotNull public @NotNull Vector rotateAroundZ(double angle)
This piece of math is based on the standard rotation matrix for vectors in three dimensional space. This matrix can be found here: Rotation Matrix.
angle
- the angle to rotate the vector about. This angle is passed
in radians@NotNull public @NotNull Vector rotateAroundAxis(@NotNull @NotNull Vector axis, double angle) throws IllegalArgumentException
Rotation will follow the general Right-Hand-Rule, which means rotation will be counterclockwise when the axis is pointing towards the observer.
This method will always make sure the provided axis is a unit vector, to
not modify the length of the vector when rotating. If you are experienced
with the scaling of a non-unit axis vector, you can use
rotateAroundNonUnitAxis(Vector, double)
.
axis
- the axis to rotate the vector around. If the passed vector is
not of length 1, it gets copied and normalized before using it for the
rotation. Please use normalize()
on the instance before
passing it to this methodangle
- the angle to rotate the vector around the axisIllegalArgumentException
- if the provided axis vector instance is
null@NotNull public @NotNull Vector rotateAroundNonUnitAxis(@NotNull @NotNull Vector axis, double angle) throws IllegalArgumentException
Rotation will follow the general Right-Hand-Rule, which means rotation will be counterclockwise when the axis is pointing towards the observer.
Note that the vector length will change accordingly to the axis vector
length. If the provided axis is not a unit vector, the rotated vector
will not have its previous length. The scaled length of the resulting
vector will be related to the axis vector. If you are not perfectly sure
about the scaling of the vector, use
rotateAroundAxis(Vector, double)
axis
- the axis to rotate the vector around.angle
- the angle to rotate the vector around the axisIllegalArgumentException
- if the provided axis vector instance is
nullpublic double getX()
public int getBlockX()
public double getY()
public int getBlockY()
public double getZ()
public int getBlockZ()
@NotNull public @NotNull Vector setX(int x)
x
- The new X component.@NotNull public @NotNull Vector setX(double x)
x
- The new X component.@NotNull public @NotNull Vector setX(float x)
x
- The new X component.@NotNull public @NotNull Vector setY(int y)
y
- The new Y component.@NotNull public @NotNull Vector setY(double y)
y
- The new Y component.@NotNull public @NotNull Vector setY(float y)
y
- The new Y component.@NotNull public @NotNull Vector setZ(int z)
z
- The new Z component.@NotNull public @NotNull Vector setZ(double z)
z
- The new Z component.@NotNull public @NotNull Vector setZ(float z)
z
- The new Z component.public boolean equals(Object obj)
Only two Vectors can ever return true. This method uses a fuzzy match to account for floating point errors. The epsilon can be retrieved with epsilon.
public int hashCode()
@NotNull public @NotNull Vector clone()
public String toString()
@NotNull public @NotNull Location toLocation(@NotNull @NotNull World world)
world
- The world to link the location to.@NotNull public @NotNull Location toLocation(@NotNull @NotNull World world, float yaw, float pitch)
world
- The world to link the location to.yaw
- The desired yaw.pitch
- The desired pitch.@NotNull public @NotNull BlockVector toBlockVector()
public void checkFinite() throws IllegalArgumentException
IllegalArgumentException
- if any component is not finitepublic static double getEpsilon()
@NotNull public static @NotNull Vector getMinimum(@NotNull @NotNull Vector v1, @NotNull @NotNull Vector v2)
v1
- The first vector.v2
- The second vector.@NotNull public static @NotNull Vector getMaximum(@NotNull @NotNull Vector v1, @NotNull @NotNull Vector v2)
v1
- The first vector.v2
- The second vector.@NotNull public static @NotNull Vector getRandom()
@NotNull public @NotNull Map<String,Object> serialize()
ConfigurationSerializable
This class must provide a method to restore this class, as defined in
the ConfigurationSerializable
interface javadocs.
serialize
in interface ConfigurationSerializable
Copyright © 2020. All rights reserved.