Class Vector

java.lang.Object
org.bukkit.util.Vector
All Implemented Interfaces:
java.lang.Cloneable, ConfigurationSerializable
Direct Known Subclasses:
BlockVector

public class Vector
extends java.lang.Object
implements java.lang.Cloneable, ConfigurationSerializable
Represents a mutable vector. Because the components of Vectors are mutable, storing Vectors long term may be dangerous if passing code modifies the Vector later. If you want to keep around a Vector, it may be wise to call clone() in order to get a copy.
  • Field Summary

    Fields 
    Modifier and Type Field Description
    protected double x  
    protected double y  
    protected double z  
  • Constructor Summary

    Constructors 
    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.
  • Method Summary

    Modifier and Type Method Description
    Vector add​(Vector vec)
    Adds a vector to this one
    float angle​(Vector other)
    Gets the angle between this vector and another in radians.
    Vector clone()
    Get a new vector.
    Vector copy​(Vector vec)
    Copies another vector
    Vector crossProduct​(Vector o)
    Calculates the cross product of this vector with another.
    static Vector deserialize​(java.util.Map<java.lang.String,​java.lang.Object> args)  
    double distance​(Vector o)
    Get the distance between this vector and another.
    double distanceSquared​(Vector o)
    Get the squared distance between this vector and another.
    Vector divide​(Vector vec)
    Divides the vector by another.
    double dot​(Vector other)
    Calculates the dot product of this vector with another.
    boolean equals​(java.lang.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.
    Vector getCrossProduct​(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 Vector getMaximum​(Vector v1, Vector v2)
    Gets the maximum components of two vectors.
    Vector getMidpoint​(Vector other)
    Gets a new midpoint vector between this vector and another.
    static Vector getMinimum​(Vector v1, Vector v2)
    Gets the minimum components of two vectors.
    static 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​(Vector min, Vector max)
    Returns whether this vector is in an axis-aligned bounding box.
    boolean isInSphere​(Vector origin, double radius)
    Returns whether this vector is within a sphere.
    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.
    Vector midpoint​(Vector other)
    Sets this vector to the midpoint between this vector and another.
    Vector multiply​(double m)
    Performs scalar multiplication, multiplying all components with a scalar.
    Vector multiply​(float m)
    Performs scalar multiplication, multiplying all components with a scalar.
    Vector multiply​(int m)
    Performs scalar multiplication, multiplying all components with a scalar.
    Vector multiply​(Vector vec)
    Multiplies the vector by another.
    Vector normalize()
    Converts this vector to a unit vector (a vector with length of 1).
    java.util.Map<java.lang.String,​java.lang.Object> serialize()
    Creates a Map representation of this class.
    Vector setX​(double x)
    Set the X component.
    Vector setX​(float x)
    Set the X component.
    Vector setX​(int x)
    Set the X component.
    Vector setY​(double y)
    Set the Y component.
    Vector setY​(float y)
    Set the Y component.
    Vector setY​(int y)
    Set the Y component.
    Vector setZ​(double z)
    Set the Z component.
    Vector setZ​(float z)
    Set the Z component.
    Vector setZ​(int z)
    Set the Z component.
    Vector subtract​(Vector vec)
    Subtracts a vector from this one.
    BlockVector toBlockVector()
    Get the block vector of this vector.
    Location toLocation​(World world)
    Gets a Location version of this vector with yaw and pitch being 0.
    Location toLocation​(World world, float yaw, float pitch)
    Gets a Location version of this vector.
    java.lang.String toString()
    Returns this vector's components as x,y,z.
    Vector zero()
    Zero this vector's components.

    Methods inherited from class java.lang.Object

    finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • x

      protected double x
    • y

      protected double y
    • z

      protected double z
  • Constructor Details

    • Vector

      public Vector()
      Construct the vector with all components as 0.
    • Vector

      public Vector​(int x, int y, int z)
      Construct the vector with provided integer components.
      Parameters:
      x - X component
      y - Y component
      z - Z component
    • Vector

      public Vector​(double x, double y, double z)
      Construct the vector with provided double components.
      Parameters:
      x - X component
      y - Y component
      z - Z component
    • Vector

      public Vector​(float x, float y, float z)
      Construct the vector with provided float components.
      Parameters:
      x - X component
      y - Y component
      z - Z component
  • Method Details

    • add

      public Vector add​(Vector vec)
      Adds a vector to this one
      Parameters:
      vec - The other vector
      Returns:
      the same vector
    • subtract

      public Vector subtract​(Vector vec)
      Subtracts a vector from this one.
      Parameters:
      vec - The other vector
      Returns:
      the same vector
    • multiply

      public Vector multiply​(Vector vec)
      Multiplies the vector by another.
      Parameters:
      vec - The other vector
      Returns:
      the same vector
    • divide

      public Vector divide​(Vector vec)
      Divides the vector by another.
      Parameters:
      vec - The other vector
      Returns:
      the same vector
    • copy

      public Vector copy​(Vector vec)
      Copies another vector
      Parameters:
      vec - The other vector
      Returns:
      the same vector
    • length

      public double length()
      Gets the magnitude of the vector, defined as sqrt(x^2+y^2+z^2). The value of this method is not cached and uses a costly square-root function, so do not repeatedly call this method to get the vector's magnitude. NaN will be returned if the inner result of the sqrt() function overflows, which will be caused if the length is too long.
      Returns:
      the magnitude
    • lengthSquared

      public double lengthSquared()
      Gets the magnitude of the vector squared.
      Returns:
      the magnitude
    • distance

      public double distance​(Vector o)
      Get the distance between this vector and another. The value of this method is not cached and uses a costly square-root function, so do not repeatedly call this method to get the vector's magnitude. NaN will be returned if the inner result of the sqrt() function overflows, which will be caused if the distance is too long.
      Parameters:
      o - The other vector
      Returns:
      the distance
    • distanceSquared

      public double distanceSquared​(Vector o)
      Get the squared distance between this vector and another.
      Parameters:
      o - The other vector
      Returns:
      the distance
    • angle

      public float angle​(Vector other)
      Gets the angle between this vector and another in radians.
      Parameters:
      other - The other vector
      Returns:
      angle in radians
    • midpoint

      public Vector midpoint​(Vector other)
      Sets this vector to the midpoint between this vector and another.
      Parameters:
      other - The other vector
      Returns:
      this same vector (now a midpoint)
    • getMidpoint

      public Vector getMidpoint​(Vector other)
      Gets a new midpoint vector between this vector and another.
      Parameters:
      other - The other vector
      Returns:
      a new midpoint vector
    • multiply

      public Vector multiply​(int m)
      Performs scalar multiplication, multiplying all components with a scalar.
      Parameters:
      m - The factor
      Returns:
      the same vector
    • multiply

      public Vector multiply​(double m)
      Performs scalar multiplication, multiplying all components with a scalar.
      Parameters:
      m - The factor
      Returns:
      the same vector
    • multiply

      public Vector multiply​(float m)
      Performs scalar multiplication, multiplying all components with a scalar.
      Parameters:
      m - The factor
      Returns:
      the same vector
    • dot

      public double dot​(Vector other)
      Calculates the dot product of this vector with another. The dot product is defined as x1*x2+y1*y2+z1*z2. The returned value is a scalar.
      Parameters:
      other - The other vector
      Returns:
      dot product
    • crossProduct

      public Vector crossProduct​(Vector o)
      Calculates the cross product of this vector with another. The cross product is defined as:
      • x = y1 * z2 - y2 * z1
      • y = z1 * x2 - z2 * x1
      • z = x1 * y2 - x2 * y1
      Parameters:
      o - The other vector
      Returns:
      the same vector
    • getCrossProduct

      public Vector getCrossProduct​(Vector o)
      Calculates the cross product of this vector with another without mutating the original. The cross product is defined as:
      • x = y1 * z2 - y2 * z1
      • y = z1 * x2 - z2 * x1
      • z = x1 * y2 - x2 * y1
      Parameters:
      o - The other vector
      Returns:
      a new vector
    • normalize

      public Vector normalize()
      Converts this vector to a unit vector (a vector with length of 1).
      Returns:
      the same vector
    • zero

      public Vector zero()
      Zero this vector's components.
      Returns:
      the same vector
    • isInAABB

      public boolean isInAABB​(Vector min, Vector max)
      Returns whether this vector is in an axis-aligned bounding box.

      The minimum and maximum vectors given must be truly the minimum and maximum X, Y and Z components.

      Parameters:
      min - Minimum vector
      max - Maximum vector
      Returns:
      whether this vector is in the AABB
    • isInSphere

      public boolean isInSphere​(Vector origin, double radius)
      Returns whether this vector is within a sphere.
      Parameters:
      origin - Sphere origin.
      radius - Sphere radius
      Returns:
      whether this vector is in the sphere
    • getX

      public double getX()
      Gets the X component.
      Returns:
      The X component.
    • getBlockX

      public int getBlockX()
      Gets the floored value of the X component, indicating the block that this vector is contained with.
      Returns:
      block X
    • getY

      public double getY()
      Gets the Y component.
      Returns:
      The Y component.
    • getBlockY

      public int getBlockY()
      Gets the floored value of the Y component, indicating the block that this vector is contained with.
      Returns:
      block y
    • getZ

      public double getZ()
      Gets the Z component.
      Returns:
      The Z component.
    • getBlockZ

      public int getBlockZ()
      Gets the floored value of the Z component, indicating the block that this vector is contained with.
      Returns:
      block z
    • setX

      public Vector setX​(int x)
      Set the X component.
      Parameters:
      x - The new X component.
      Returns:
      This vector.
    • setX

      public Vector setX​(double x)
      Set the X component.
      Parameters:
      x - The new X component.
      Returns:
      This vector.
    • setX

      public Vector setX​(float x)
      Set the X component.
      Parameters:
      x - The new X component.
      Returns:
      This vector.
    • setY

      public Vector setY​(int y)
      Set the Y component.
      Parameters:
      y - The new Y component.
      Returns:
      This vector.
    • setY

      public Vector setY​(double y)
      Set the Y component.
      Parameters:
      y - The new Y component.
      Returns:
      This vector.
    • setY

      public Vector setY​(float y)
      Set the Y component.
      Parameters:
      y - The new Y component.
      Returns:
      This vector.
    • setZ

      public Vector setZ​(int z)
      Set the Z component.
      Parameters:
      z - The new Z component.
      Returns:
      This vector.
    • setZ

      public Vector setZ​(double z)
      Set the Z component.
      Parameters:
      z - The new Z component.
      Returns:
      This vector.
    • setZ

      public Vector setZ​(float z)
      Set the Z component.
      Parameters:
      z - The new Z component.
      Returns:
      This vector.
    • equals

      public boolean equals​(java.lang.Object obj)
      Checks to see if two objects are equal.

      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.

      Overrides:
      equals in class java.lang.Object
    • hashCode

      public int hashCode()
      Returns a hash code for this vector
      Overrides:
      hashCode in class java.lang.Object
      Returns:
      hash code
    • clone

      public Vector clone()
      Get a new vector.
      Overrides:
      clone in class java.lang.Object
      Returns:
      vector
    • toString

      public java.lang.String toString()
      Returns this vector's components as x,y,z.
      Overrides:
      toString in class java.lang.Object
    • toLocation

      public Location toLocation​(World world)
      Gets a Location version of this vector with yaw and pitch being 0.
      Parameters:
      world - The world to link the location to.
      Returns:
      the location
    • toLocation

      public Location toLocation​(World world, float yaw, float pitch)
      Gets a Location version of this vector.
      Parameters:
      world - The world to link the location to.
      yaw - The desired yaw.
      pitch - The desired pitch.
      Returns:
      the location
    • toBlockVector

      public BlockVector toBlockVector()
      Get the block vector of this vector.
      Returns:
      A block vector.
    • getEpsilon

      public static double getEpsilon()
      Get the threshold used for equals().
      Returns:
      The epsilon.
    • getMinimum

      public static Vector getMinimum​(Vector v1, Vector v2)
      Gets the minimum components of two vectors.
      Parameters:
      v1 - The first vector.
      v2 - The second vector.
      Returns:
      minimum
    • getMaximum

      public static Vector getMaximum​(Vector v1, Vector v2)
      Gets the maximum components of two vectors.
      Parameters:
      v1 - The first vector.
      v2 - The second vector.
      Returns:
      maximum
    • getRandom

      public static Vector getRandom()
      Gets a random vector with components having a random value between 0 and 1.
      Returns:
      A random vector.
    • serialize

      public java.util.Map<java.lang.String,​java.lang.Object> serialize()
      Description copied from interface: ConfigurationSerializable
      Creates a Map representation of this class.

      This class must provide a method to restore this class, as defined in the ConfigurationSerializable interface javadocs.

      Specified by:
      serialize in interface ConfigurationSerializable
      Returns:
      Map containing the current state of this class
    • deserialize

      public static Vector deserialize​(java.util.Map<java.lang.String,​java.lang.Object> args)