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