Skip to content

Latest commit

 

History

History
643 lines (280 loc) · 7.21 KB

File metadata and controls

643 lines (280 loc) · 7.21 KB

Home > server > Vector2

Vector2 class

Represents a 2D vector.

Signature:

export default class Vector2 extends Float32Array implements Vector2Like 

Extends: Float32Array

Implements: Vector2Like

Remarks

All vector methods result in mutation of the vector instance. This class extends Float32Array to provide an efficient way to create and manipulate a 2-dimensional vector. Various convenience methods are provided for common vector operations.

Constructors

Constructor

Modifiers

Description

(constructor)(x, y)

Constructs a new instance of the Vector2 class

Properties

Property

Modifiers

Type

Description

length

readonly

number

The length of the vector.

magnitude

readonly

number

The magnitude of the vector. Alias for length.

squaredLength

readonly

number

The squared length of the vector.

squaredMagnitude

readonly

number

The squared magnitude of the vector. Alias for squaredLength.

x

number

The x-component of the vector.

y

number

The y-component of the vector.

Methods

Method

Modifiers

Description

add(vector2)

Adds a vector to the current vector.

angle(vector2)

Returns the angle between two vectors.

ceil()

Rounds each component of the vector up to the nearest integer.

clone()

Returns a new vector with the same components as the current vector.

copy(vector2)

Copies the components of a vector to the current vector.

create()

static

Creates a new Vector2 instance.

distance(vector2)

Calculates the distance between the current vector and another vector.

divide(vector2)

Divides the current vector by another vector.

dot(vector2)

Calculates the dot product of the current vector and another vector.

equals(vector2)

Checks if the current vector is approximately equal to another vector.

exactEquals(vector2)

Checks if the current vector is exactly equal to another vector.

floor()

Rounds each component of the vector down to the nearest integer.

invert()

Inverts the components of the current vector.

lerp(vector2, t)

Linearly interpolates between the current vector and another vector.

max(vector2)

Sets each component of the vector to the maximum of the current vector and another vector.

min(vector2)

Sets each component of the vector to the minimum of the current vector and another vector.

multiply(vector2)

Multiplies each component of the current vector by the corresponding component of another vector.

negate()

Negates each component of the vector.

normalize()

Normalizes the current vector.

randomize(scale)

Randomizes the components of the current vector.

rotate(vector2, angle)

Rotates the current vector around an origin.

round()

Rounds each component of the vector to the nearest integer.

scale(scale)

Scales the current vector by a scalar value.

scaleAndAdd(vector2, scale)

Scales the current vector by a scalar value and adds the result to another vector.

subtract(vector2)

Subtracts a vector from the current vector.

toString()

Returns a string representation of the vector in x,y format.

transformMatrix2(matrix2)

Transforms the current vector by a matrix2.

transformMatrix3(matrix3)

Transforms the current vector by a matrix3.

transformMatrix4(matrix4)

Transforms the current vector by a matrix4.

zero()

Sets each component of the vector to zero.