Class Vec3d

java.lang.Object
net.minecraft.util.math.Vec3d
All Implemented Interfaces:
Position

public class Vec3d
extends Object
implements Position
An immutable vector composed of 3 doubles.

This vector class is used for representing position, velocity, rotation, color, etc.

This vector has proper hashCode() and equals(Object) implementations and can be used as a map key.

See Also:
Vec3i, Vec3f
  • Field Summary

    Fields
    Modifier and Type Field Description
    double x
    The X coordinate of this vector.
    double y
    The Y coordinate of this vector.
    double z
    The Z coordinate of this vector.
    static Vec3d ZERO
    The zero vector (0, 0, 0).
  • Constructor Summary

    Constructors
    Constructor Description
    Vec3d​(double x, double y, double z)
    Creates a vector of the given coordinates.
    Vec3d​(Vec3f vec)
    Copies the given vector.
  • Method Summary

    Modifier and Type Method Description
    Vec3d add​(double x, double y, double z)
    Returns the sum of this vector and the given vector.
    Vec3d add​(Vec3d vec)
    Returns the sum of this vector and the given vector.
    Vec3d crossProduct​(Vec3d vec)
    Returns the cross product of this vector and the given vector.
    double distanceTo​(Vec3d vec)
    Returns the distance between this vector and the given vector.
    double dotProduct​(Vec3d vec)
    Returns the dot product of this vector and the given vector.
    boolean equals​(Object o)  
    Vec3d floorAlongAxes​(EnumSet<Direction.Axis> axes)
    Applies the floor function to the coordinates chosen by the given axes.
    static Vec3d fromPolar​(float pitch, float yaw)
    Converts pitch and yaw into a direction vector.
    static Vec3d fromPolar​(Vec2f polar)
    Converts pitch and yaw into a direction vector.
    double getComponentAlongAxis​(Direction.Axis axis)
    Returns the coordinate chosen by the given axis.
    double getX()
    Returns the X coordinate.
    double getY()
    Returns the Y coordinate.
    double getZ()
    Returns the Z coordinate.
    int hashCode()  
    boolean isInRange​(Position pos, double radius)
    Checks if the distance between this vector and the given position is less than radius.
    double length()
    Returns the length of this vector.
    double lengthSquared()
    Returns the squared length of this vector.
    Vec3d multiply​(double value)
    Return a vector whose coordinates are the coordinates of this vector each multiplied by the given scalar value.
    Vec3d multiply​(double x, double y, double z)
    Returns a vector whose coordinates are the product of each pair of coordinates in this vector and the given vector.
    Vec3d multiply​(Vec3d vec)
    Returns a vector whose coordinates are the product of each pair of coordinates in this vector and the given vector.
    Vec3d negate()
    Creates a vector with the same length but with the opposite direction.
    Vec3d normalize()
    Normalizes this vector.
    static Vec3d of​(Vec3i vec)
    Copies the given vector.
    static Vec3d ofBottomCenter​(Vec3i vec)
    Creates a vector representing the bottom center of the given block position.
    static Vec3d ofCenter​(Vec3i vec)
    Creates a vector representing the center of the given block position.
    static Vec3d ofCenter​(Vec3i vec, double deltaY)
    Creates a vector representing the center of the given block position but with the given offset for the Y coordinate.
    Vec3d relativize​(Vec3d vec)
    Subtracts this vector from the given vector.
    Vec3d rotateX​(float angle)
    Rotates this vector by the given angle counterclockwise around the X axis.
    Vec3d rotateY​(float angle)
    Rotates this vector by the given angle counterclockwise around the Y axis.
    Vec3d rotateZ​(float angle)
    Rotates this vector by the given angle counterclockwise around the Z axis.
    double squaredDistanceTo​(double x, double y, double z)
    Returns the squared distance between this vector and the given vector.
    double squaredDistanceTo​(Vec3d vec)
    Returns the squared distance between this vector and the given vector.
    Vec3d subtract​(double x, double y, double z)
    Subtracts the given vector from this vector.
    Vec3d subtract​(Vec3d vec)
    Subtracts the given vector from this vector.
    String toString()  
    static Vec3d unpackRgb​(int rgb)
    Converts a packed RGB color into a vector of (red, green, blue).

    Methods inherited from class java.lang.Object

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

    • ZERO

      public static final Vec3d ZERO
      The zero vector (0, 0, 0).
    • x

      public final double x
      The X coordinate of this vector.
    • y

      public final double y
      The Y coordinate of this vector.
    • z

      public final double z
      The Z coordinate of this vector.
  • Constructor Details

    • Vec3d

      public Vec3d​(double x, double y, double z)
      Creates a vector of the given coordinates.
    • Vec3d

      public Vec3d​(Vec3f vec)
      Copies the given vector.
  • Method Details

    • unpackRgb

      @Environment(CLIENT) public static Vec3d unpackRgb​(int rgb)
      Converts a packed RGB color into a vector of (red, green, blue).
      Parameters:
      rgb - the color in the 0xRRGGBB format
      Returns:
      the vector representing the given color; each coordinate has value between 0 and 1
    • ofCenter

      public static Vec3d ofCenter​(Vec3i vec)
      Creates a vector representing the center of the given block position.
    • of

      public static Vec3d of​(Vec3i vec)
      Copies the given vector.
    • ofBottomCenter

      public static Vec3d ofBottomCenter​(Vec3i vec)
      Creates a vector representing the bottom center of the given block position.

      The bottom center of a block position pos is (pos.getX() + 0.5, pos.getY(), pos.getZ() + 0.5).

      See Also:
      ofCenter(Vec3i)
    • ofCenter

      public static Vec3d ofCenter​(Vec3i vec, double deltaY)
      Creates a vector representing the center of the given block position but with the given offset for the Y coordinate.
      Returns:
      a vector of (vec.getX() + 0.5, vec.getY() + deltaY, vec.getZ() + 0.5)
    • relativize

      public Vec3d relativize​(Vec3d vec)
      Subtracts this vector from the given vector.
      Returns:
      the difference between the given vector and this vector
      See Also:
      subtract(Vec3d)
    • normalize

      public Vec3d normalize()
      Normalizes this vector.

      Normalized vector is a vector with the same direction but with length 1. Each coordinate of normalized vector has value between 0 and 1.

      Returns:
      the normalized vector of this vector
    • dotProduct

      public double dotProduct​(Vec3d vec)
      Returns the dot product of this vector and the given vector.
    • crossProduct

      public Vec3d crossProduct​(Vec3d vec)
      Returns the cross product of this vector and the given vector.
    • subtract

      public Vec3d subtract​(Vec3d vec)
      Subtracts the given vector from this vector.
      Returns:
      the difference between this vector and the given vector
      See Also:
      subtract(double, double, double), relativize(Vec3d)
    • subtract

      public Vec3d subtract​(double x, double y, double z)
      Subtracts the given vector from this vector.
      Returns:
      the difference between this vector and the given vector
      See Also:
      relativize(Vec3d)
    • add

      public Vec3d add​(Vec3d vec)
      Returns the sum of this vector and the given vector.
      See Also:
      add(double, double, double)
    • add

      public Vec3d add​(double x, double y, double z)
      Returns the sum of this vector and the given vector.
      See Also:
      add(Vec3d)
    • isInRange

      public boolean isInRange​(Position pos, double radius)
      Checks if the distance between this vector and the given position is less than radius.
    • distanceTo

      public double distanceTo​(Vec3d vec)
      Returns the distance between this vector and the given vector.
      See Also:
      squaredDistanceTo(Vec3d)
    • squaredDistanceTo

      public double squaredDistanceTo​(Vec3d vec)
      Returns the squared distance between this vector and the given vector.

      Can be used for fast comparison between distances.

      See Also:
      squaredDistanceTo(double, double, double), distanceTo(Vec3d)
    • squaredDistanceTo

      public double squaredDistanceTo​(double x, double y, double z)
      Returns the squared distance between this vector and the given vector.

      Can be used for fast comparison between distances.

      See Also:
      squaredDistanceTo(Vec3d), distanceTo(Vec3d)
    • multiply

      public Vec3d multiply​(double value)
      Return a vector whose coordinates are the coordinates of this vector each multiplied by the given scalar value.
      See Also:
      multiply(Vec3d), multiply(double, double, double)
    • negate

      @Environment(CLIENT) public Vec3d negate()
      Creates a vector with the same length but with the opposite direction.
    • multiply

      public Vec3d multiply​(Vec3d vec)
      Returns a vector whose coordinates are the product of each pair of coordinates in this vector and the given vector.
      See Also:
      multiply(double, double, double), multiply(double)
    • multiply

      public Vec3d multiply​(double x, double y, double z)
      Returns a vector whose coordinates are the product of each pair of coordinates in this vector and the given vector.
      See Also:
      multiply(Vec3d), multiply(double)
    • length

      public double length()
      Returns the length of this vector.

      The length of a vector is equivalent to the distance between that vector and the ZERO vector.

      See Also:
      lengthSquared()
    • lengthSquared

      public double lengthSquared()
      Returns the squared length of this vector.

      Can be used for fast comparison between lengths.

      See Also:
      length()
    • equals

      public boolean equals​(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • rotateX

      public Vec3d rotateX​(float angle)
      Rotates this vector by the given angle counterclockwise around the X axis.
      Parameters:
      angle - the angle in radians
    • rotateY

      public Vec3d rotateY​(float angle)
      Rotates this vector by the given angle counterclockwise around the Y axis.
      Parameters:
      angle - the angle in radians
    • rotateZ

      @Environment(CLIENT) public Vec3d rotateZ​(float angle)
      Rotates this vector by the given angle counterclockwise around the Z axis.
      Parameters:
      angle - the angle in radians
    • fromPolar

      @Environment(CLIENT) public static Vec3d fromPolar​(Vec2f polar)
      Converts pitch and yaw into a direction vector.
      Parameters:
      polar - the vector composed of pitch and yaw
      See Also:
      fromPolar(float, float)
    • fromPolar

      @Environment(CLIENT) public static Vec3d fromPolar​(float pitch, float yaw)
      Converts pitch and yaw into a direction vector.
      See Also:
      fromPolar(Vec2f)
    • floorAlongAxes

      public Vec3d floorAlongAxes​(EnumSet<Direction.Axis> axes)
      Applies the floor function to the coordinates chosen by the given axes.
    • getComponentAlongAxis

      public double getComponentAlongAxis​(Direction.Axis axis)
      Returns the coordinate chosen by the given axis.
    • getX

      public final double getX()
      Returns the X coordinate.
      Specified by:
      getX in interface Position
    • getY

      public final double getY()
      Returns the Y coordinate.
      Specified by:
      getY in interface Position
    • getZ

      public final double getZ()
      Returns the Z coordinate.
      Specified by:
      getZ in interface Position