Class: Vec2
Defined in: math/vector2.ts:23
Static operations on Vector2. Mutating operations (set, add,
subtract, multiply, multiplyComponents, divide, normalize,
floorComponents, rotate, perpendicular, negate) mutate their
target argument in place and return it, rather than allocating a new
Vector2. Read-only operations (magnitude, magnitudeSquared, dot,
cross, distanceTo, equals, toString, toFloat32Array) and clone
never mutate their arguments.
Constructors
Constructor
new Vec2():
Vec2
Returns
Vec2
Accessors
down
Get Signature
get
staticdown():Vector2
Defined in: math/vector2.ts:52
A unit vector pointing downward (0, 1). A fresh vector is created on every access, so it's always safe to mutate.
Returns
left
Get Signature
get
staticleft():Vector2
Defined in: math/vector2.ts:60
A unit vector pointing left (-1, 0). A fresh vector is created on every access, so it's always safe to mutate.
Returns
one
Get Signature
get
staticone():Vector2
Defined in: math/vector2.ts:36
A vector with components of 1 (1, 1). A fresh vector is created on every access, so it's always safe to mutate.
Returns
right
Get Signature
get
staticright():Vector2
Defined in: math/vector2.ts:68
A unit vector pointing right (1, 0). A fresh vector is created on every access, so it's always safe to mutate.
Returns
up
Get Signature
get
staticup():Vector2
Defined in: math/vector2.ts:44
A unit vector pointing upward (0, -1). A fresh vector is created on every access, so it's always safe to mutate.
Returns
zero
Get Signature
get
staticzero():Vector2
Defined in: math/vector2.ts:28
A zero vector (0, 0). A fresh vector is created on every access, so it's always safe to mutate.
Returns
Methods
add()
staticadd(target,value):Vector2
Defined in: math/vector2.ts:78
Adds another vector into target, mutating it in place.
Parameters
target
The vector to mutate.
value
The vector to add.
Returns
target, for chaining.
clone()
staticclone(vector):Vector2
Defined in: math/vector2.ts:189
Creates a deep copy of a vector.
Parameters
vector
The vector to copy.
Returns
A new Vector2 with the same component values.
cross()
staticcross(a,b):number
Defined in: math/vector2.ts:264
Calculates the 2D (scalar) cross product of two vectors.
Parameters
a
The first vector.
b
The second vector.
Returns
number
The scalar cross product of the two vectors.
distanceTo()
staticdistanceTo(a,b):number
Defined in: math/vector2.ts:227
Calculates the distance between two vectors.
Parameters
a
The first vector.
b
The second vector.
Returns
number
The distance between the two vectors.
divide()
staticdivide(target,scalar):Vector2
Defined in: math/vector2.ts:130
Divides target by a scalar value, mutating it in place.
Parameters
target
The vector to mutate.
scalar
number
The scalar value to divide by.
Returns
target, for chaining.
dot()
staticdot(a,b):number
Defined in: math/vector2.ts:254
Calculates the dot product of two vectors.
Parameters
a
The first vector.
b
The second vector.
Returns
number
The dot product of the two vectors.
equals()
staticequals(a,b):boolean
Defined in: math/vector2.ts:208
Checks if two vectors are equal.
Parameters
a
The first vector.
b
The second vector.
Returns
boolean
True if the vectors have the same components, false otherwise.
floorComponents()
staticfloorComponents(target):Vector2
Defined in: math/vector2.ts:177
Rounds target's components down to the nearest integer, mutating it in place.
Parameters
target
The vector to mutate.
Returns
target, for chaining.
magnitude()
staticmagnitude(vector):number
Defined in: math/vector2.ts:152
Calculates the magnitude (length) of a vector.
Parameters
vector
The vector.
Returns
number
The magnitude of the vector.
magnitudeSquared()
staticmagnitudeSquared(vector):number
Defined in: math/vector2.ts:143
Calculates the squared magnitude of a vector. This is faster than Vec2.magnitude as it avoids the square root.
Parameters
vector
The vector.
Returns
number
The squared magnitude of the vector.
multiply()
staticmultiply(target,scalar):Vector2
Defined in: math/vector2.ts:104
Multiplies target by a scalar value, mutating it in place.
Parameters
target
The vector to mutate.
scalar
number
The scalar value to multiply by.
Returns
target, for chaining.
multiplyComponents()
staticmultiplyComponents(target,value):Vector2
Defined in: math/vector2.ts:117
Multiplies target's components by another vector's components, mutating it in place.
Parameters
target
The vector to mutate.
value
The vector to multiply components with.
Returns
target, for chaining.
negate()
staticnegate(target):Vector2
Defined in: math/vector2.ts:288
Negates both of target's components, mutating it in place.
Parameters
target
The vector to mutate.
Returns
target, for chaining.
normalize()
staticnormalize(target):Vector2
Defined in: math/vector2.ts:162
Normalizes target to unit length in the same direction, mutating it in place.
Parameters
target
The vector to mutate.
Returns
target, for chaining.
Throws
An error if target has zero length, since its direction is undefined.
perpendicular()
staticperpendicular(target):Vector2
Defined in: math/vector2.ts:274
Rotates target -90 degrees (y, -x) in place, making it perpendicular to
its original direction.
Parameters
target
The vector to mutate.
Returns
target, for chaining.
rotate()
staticrotate(target,angleInRadians):Vector2
Defined in: math/vector2.ts:237
Rotates target by a given angle in radians, mutating it in place.
Parameters
target
The vector to mutate.
angleInRadians
number
The angle in radians to rotate the vector.
Returns
target, for chaining.
subtract()
staticsubtract(target,value):Vector2
Defined in: math/vector2.ts:91
Subtracts another vector from target, mutating it in place.
Parameters
target
The vector to mutate.
value
The vector to subtract.
Returns
target, for chaining.
toFloat32Array()
statictoFloat32Array(vector):Float32Array
Defined in: math/vector2.ts:217
Converts a vector to a glsl-compatible float32 array.
Parameters
vector
The vector.
Returns
Float32Array
The 2d vector array (e.g. [5, 3] for { x: 5, y: 3 }).
toString()
statictoString(vector):string
Defined in: math/vector2.ts:198
Returns a string representation of a vector.
Parameters
vector
The vector.
Returns
string
A string in the format "(x, y)" with components rounded to 1 decimal place.