Vec3
is a versatile class designed for representing and manipulating 3-dimensional vectors in a Minecraft Bedrock Script API. This class offers a wide range of functionalities, including basic vector arithmetic, vector transformations, and utility functions for calculating properties like distance and angle between vectors.
Vec3
instances are immutable, meaning that operations like add
and subtract
will return a new Vec3
instance rather than modifying the original instance. This is to prevent unexpected behavior and side effects.
⚠️ Setting the x, y, or z component of a vector directly will actually change the original vector.
Constructs a Vec3
object from various input types:
- x: Can be a
Vector3
,Vec3
,Direction
, an array of numbers[x, y, z]
, or a single numeric value representing the x-coordinate. - y: (Optional) The y-coordinate (required if x is a number).
- z: (Optional) The z-coordinate (required if x is a number).
Directional vectors (Up, Down, North, South, East, West) are also supported.
Creates a new Vec3
instance from various inputs, similar to the constructor.
Creates a new direction vector from the given yaw and pitch values (in degrees).
Converts the Vec3
instance to the server's native Vector
class if available, otherwise uses a polyfill.
Returns a new Vec3
instance with the same x, y, z values.
Adds another vector to the current vector and returns the result.
Subtracts another vector from the current vector and returns the result.
Multiplies the current vector by another vector or scalar and returns the result.
Divides the current vector by another vector or scalar and returns the result.
Normalizes the vector to a length of 1.
Returns the length (magnitude) of the vector.
Returns the squared length of the vector.
Computes the cross product with another vector and returns the result.
Calculates the distance to another vector.
Calculates the squared distance to another vector.
Performs linear interpolation between the current vector and another vector.
Performs spherical linear interpolation between the current vector and another vector.
Calculates the dot product with another vector.
Calculates the angle (in radians) between the current vector and another vector.
Projects the current vector onto another vector.
Reflects the vector against a given normal.
Sets the x, y, or z component of the vector, respectively.
Calculates the shortest distance from the vector to a line segment.
Checks if the current vector is equal to another vector.
Returns a string representation of the vector. Format being either short
"x, y, z" or long
"Vec3(x, y, z)" default being long
with ", "
as a separator.
import Vec3 from './Vec3';
// Creating a new Vec3 instance
let vector = Vec3.from(1, 2, 3);
// Performing operations
let addedVector = vector.add(Vec3.from(1, 0, 0));
let length = vector.length();
This class is essential for vector manipulations in a Minecraft server, providing a comprehensive set of tools for handling 3D vector mathematics.