vec2
Interface representing a 2D vector. A 2D vector is represented by (x, y) coordinates, and can represent a point in a plane, a directional vector, or other types of data with three ordered dimensions. Vec2 objects are created with the ecs.math.vec2 Vec2Factory, or through operations on other Vec2 objects.
Sourceβ
The Vec2Source interface represents any object that has x and y properties and hence can be used as a data source to create a Vec2. In addition, Vec2Source can be used as an argument to Vec2 algorithms, meaning that any object with {x: number, y: number} properties can be used.
Propertiesβ
Vec2Source has the following enumerable properties:
readonly x: number
Access the x component of the vector.
readonly y: number
Access the y component of the vector.
Factoryβ
fromβ
Create a Vec2 from a Vec2, or other object with x, y properties.
ecs.math.vec2.from({x, y}: {x: number, y: number}) // -> vec2
oneβ
Create a vec2 where all elements are set to one. This is equivalent to vec2.from({x: 1, y: 1})
.
ecs.math.vec2.one() // -> vec2
scaleβ
Create a vec2 with all elements set to the scale value s. This is equivalent to vec2.from({x: s, y: s})
.
ecs.math.vec2.scale(s: number) // -> vec2
xyβ
Create a Vec2 from x, y values. This is equivalent to vec2.from({x, y})
.
ecs.math.vec2.xy(x: number, y: number) // -> vec2
zeroβ
Create a vec2 where all elements are set to zero. This is equivalent to vec2.from({x: 0, y: 0})
.
ecs.math.vec2.zero() // -> vec2
Immutableβ
The following methods perform computations based on the current value of a Vec2, but do not modify its contents. Methods that return Vec2 types return new objects. Immutable APIs are typically safer, more readable, and less error-prone than mutable APIs, but may be inefficient in situations where thousands of objects are allocated each frame.
If garbage collection impacts performance, consider using the Mutable API described below.
cloneβ
Create a new vector with the same components as this vector.
existingVec2.clone() // -> vec2
crossβ
Compute the cross product of this vector and another vector. For 2D vectors, the cross product is the magnitude of the z component of the 3D cross product of the two vectors with 0 as the z component.
existingVec2.cross(v: Vec2Source) // -> vec2
distanceToβ
Compute the Euclidean distance between this vector and another vector.
existingVec2.distanceTo(v: Vec2Source) // -> vec2
divideβ
Element-wise vector division.
existingVec2.divide(v: Vec2Source) // -> vec2
dotβ
Compute the dot product of this vector and another vector.
existingVec2.dot(v: Vec2Source) // -> vec2
equalsβ
Check whether two vectors are equal, with a specified floating point tolerance.
existingVec2.equals(v: Vec2Source, tolerance: number) // -> boolean
lengthβ
Length of the vector.
existingVec2.length() // -> number
minusβ
Subtract a vector from this vector.
existingVec2.minus(v: Vec2Source) // -> vec2
mixβ
Compute a linear interpolation between this vector and another vector v with a factor t such that the result is thisVec * (1 - t) + v * t. The factor t should be between 0 and 1.
existingVec2.mix(v: Vec2Source, t: number) // -> vec2
normalizeβ
Return a new vector with the same direction as this vector, but with a length of 1.
existingVec2.normalize() // -> vec2
plusβ
Add two vectors together.
existingVec2.plus(v: Vec2Source) // -> vec2
scaleβ
Multiply the vector by a scalar.
existingVec2.scale(s: number) // -> vec2
timesβ
Element-wise vector multiplication.
existingVec2.times(v: Vec2Source) // -> vec2
Mutableβ
The following methods perform computations based on the current value of a Vec2, and modify its contents in place. They are parallel to methods in the mutable API above. Methods that return Vec2 types return a reference to the current object for convenient method chaining. Mutable APIs can be more performant than Immutable APIs, but are typically less safe, less readable, and more error-prone.
setDivideβ
Element-wise vector division. Store the result in this Vec2 and return this Vec2 for chaining.
existingVec2.setDivide(v: Vec2Source) // -> vec2
setMinusβ
Subtract a vector from this vector. Store the result in this Vec2 and return this Vec2 for chaining.
existingVec2.setMinus(v: Vec2Source) // -> vec2
setMixβ
Compute a linear interpolation between this vector and another vector v with a factor t such that the result is thisVec * (1 - t) + v * t. The factor t should be between 0 and 1. Store the result in this Vec2 and return this Vec2 for chaining.
existingVec2.setMinus(v: Vec2Source, t: number) // -> vec2
setNormalizeβ
Set the vector to be a version of itself with the same direction, but with length 1. Store the result in this Vec2 and return this Vec2 for chaining.
existingVec2.setNormalize() // -> vec2
setPlusβ
Add two vectors together. Store the result in this Vec2 and return this Vec2 for chaining.
existingVec2.setPlus(v: Vec2Source) // -> vec2
setScaleβ
Multiply the vector by a scalar. Store the result in this Vec2 and return this Vec2 for chaining.
existingVec2.setPlus(s: number) // -> vec2
setTimesβ
Element-wise vector multiplication. Store the result in this Vec2 and return this Vec2 for chaining.
existingVec2.setTimes(v: Vec2Source) // -> vec2
setXβ
Set the Vec2's x component. Store the result in this Vec2 and return this Vec2 for chaining.
existingVec2.setX(v: number) // -> vec2
setYβ
Set the Vec2's y component. Store the result in this Vec2 and return this Vec2 for chaining.
existingVec2.setY(v: number) // -> vec2
Setβ
The following methods set the value of the current Vec2 object without regard to its current content, replacing whatever was there before.
makeOneβ
Set the Vec2 to be all ones. Store the result in this Vec2 and return this Vec2 for chaining.
existingVec2.makeOne() // -> vec2
makeScaleβ
Set the Vec2 to have all components set to the scale value s. Store the result in this Vec2 and return this Vec2 for chaining.
existingVec2.makeScale(s: number) // -> vec2
makeZeroβ
Set the Vec2 to be all zeros. Store the result in this Vec2 and return this Vec2 for chaining.
existingVec2.makeZero() // -> vec2
setFromβ
Set this Vec2 to have the same value as another Vec2 or other object with x and y properties. Store the result in this Vec2 and return this Vec2 for chaining.
existingVec2.setFrom(source: Vec2Source) // -> vec2
setXyβ
Set the Vec2's x and y components. Store the result in this Vec2 and return this Vec2 for chaining.
existingVec2.setFrom(x: number, y: number) // -> vec2