vec2
Class2D vector with x and y components, used for 2D positions, directions, and math operations.
Index
Properties
Methods
- add
- addInPlace
- angleTo
- clampLength
- clampLengthInPlace
- clone
- copyFrom
- distance
- distanceSquared
- div
- divInPlace
- dot
- down
- equal
- fill
- left
- lerp
- lerpInPlace
- max
- min
- moveTowards
- moveTowardsInPlace
- mult
- multInPlace
- normalize
- normalizeInPlace
- one
- project
- projectInPlace
- projectOnPlane
- projectOnPlaneInPlace
- randomUnitVector
- reflect
- reflectInPlace
- right
- scale
- scaleInPlace
- setRandomUnitVector
- setXY
- sub
- subInPlace
- toString
- uniformScale
- uniformScaleInPlace
- up
- zero
Example
const a = new vec2(3, 4);
const b = new vec2(1, 2);
const sum = a.add(b);
console.log('a + b:', sum.x, sum.y);
const diff = a.sub(b);
console.log('a - b:', diff.x, diff.y);
const scaled = a.uniformScale(2);
console.log('a * 2:', scaled.x, scaled.y);
console.log('length of a:', a.length);
console.log('dot(a, b):', a.dot(b));
Constructors
Constructor
new vec2(
x,y):vec2
Constructs a vec2 with the given x and y values.
Parameters
| Parameter | Type |
|---|---|
x | number |
y | number |
Returns
vec2
Properties
g
g:
number
Alias for the y component.
length
length:
number
Magnitude of the vector.
lengthSquared
lengthSquared:
number
Squared magnitude of the vector.
r
r:
number
Alias for the x component.
x
x:
number
Horizontal component of the vector.
y
y:
number
Vertical component of the vector.
Methods
add()
add(
vec):vec2
Returns a new vec2 that is the sum of this and the given vector.
Parameters
| Parameter | Type |
|---|---|
vec | vec2 |
Returns
vec2
addInPlace()
addInPlace(
vec):void
Adds the given vector to this vector in place.
Parameters
| Parameter | Type |
|---|---|
vec | vec2 |
Returns
void
angleTo()
angleTo(
vec):number
Returns the angle in radians between this vector and the given vector.
Parameters
| Parameter | Type |
|---|---|
vec | vec2 |
Returns
number
clampLength()
clampLength(
length):vec2
Returns a new vec2 with its length clamped to the given maximum.
Parameters
| Parameter | Type |
|---|---|
length | number |
Returns
vec2
clampLengthInPlace()
clampLengthInPlace(
length):void
Clamps the length of this vector to the given maximum in place.
Parameters
| Parameter | Type |
|---|---|
length | number |
Returns
void
clone()
clone():
vec2
Returns a new vec2 with the same x and y values.
Returns
vec2
copyFrom()
copyFrom(
source):void
Copies x and y values from the given vector into this vector.
Parameters
Returns
void
distance()
distance(
vec):number
Returns the distance between this vector and the given vector.
Parameters
| Parameter | Type |
|---|---|
vec | vec2 |
Returns
number
distanceSquared()
distanceSquared(
vec):number
Returns the squared distance between this vector and the given vector.
Parameters
| Parameter | Type |
|---|---|
vec | vec2 |
Returns
number
div()
div(
vec):vec2
Returns a new vec2 that is this vector divided component-wise by the given vector.
Parameters
| Parameter | Type |
|---|---|
vec | vec2 |
Returns
vec2
divInPlace()
divInPlace(
vec):void
Divides this vector component-wise by the given vector in place.
Parameters
| Parameter | Type |
|---|---|
vec | vec2 |
Returns
void
dot()
dot(
vec):number
Returns the dot product of this vector and the given vector.
Parameters
| Parameter | Type |
|---|---|
vec | vec2 |
Returns
number
down()
staticdown():vec2
Static vec2 pointing in the negative y direction (0, -1).
Returns
vec2
equal()
equal(
vec):boolean
Returns true if this vector and the given vector have equal x and y values.
Parameters
| Parameter | Type |
|---|---|
vec | vec2 |
Returns
boolean
fill()
fill(
scalar):void
Sets all components of this vector to the given scalar value.
Parameters
| Parameter | Type |
|---|---|
scalar | number |
Returns
void
left()
staticleft():vec2
Static vec2 pointing in the negative x direction (-1, 0).
Returns
vec2
lerp()
staticlerp(from,to,t):vec2
Returns a new vec2 linearly interpolated between two vectors by the given factor.
Parameters
| Parameter | Type |
|---|---|
from | vec2 |
to | vec2 |
t | number |
Returns
vec2
lerpInPlace()
lerpInPlace(
target,t):void
Linearly interpolates this vector toward the target vector by the given factor in place.
Parameters
| Parameter | Type |
|---|---|
target | vec2 |
t | number |
Returns
void
max()
staticmax(a,b):vec2
Returns a vec2 with the component-wise maximum of two vectors.
Parameters
| Parameter | Type |
|---|---|
a | vec2 |
b | vec2 |
Returns
vec2
min()
staticmin(a,b):vec2
Returns a vec2 with the component-wise minimum of two vectors.
Parameters
| Parameter | Type |
|---|---|
a | vec2 |
b | vec2 |
Returns
vec2
moveTowards()
moveTowards(
target,step):vec2
Returns a new vec2 moved from this vector toward the target by at most the given max distance.
Parameters
| Parameter | Type |
|---|---|
target | vec2 |
step | number |
Returns
vec2
moveTowardsInPlace()
moveTowardsInPlace(
point,magnitude):void
Moves this vector toward the target by at most the given max distance in place.
Parameters
| Parameter | Type |
|---|---|
point | vec2 |
magnitude | number |
Returns
void
mult()
mult(
vec):vec2
Returns a new vec2 that is this vector multiplied component-wise by the given vector.
Parameters
| Parameter | Type |
|---|---|
vec | vec2 |
Returns
vec2
multInPlace()
multInPlace(
vec):void
Multiplies this vector component-wise by the given vector in place.
Parameters
| Parameter | Type |
|---|---|
vec | vec2 |
Returns
void
normalize()
normalize():
vec2
Returns a new vec2 with the same direction but unit length.
Returns
vec2
normalizeInPlace()
normalizeInPlace():
void
Normalizes this vector to unit length in place.
Returns
void
one()
staticone():vec2
Static vec2 with all components set to 1.
Returns
vec2
project()
project(
onto):vec2
Returns the projection of this vector onto the given vector.
Parameters
| Parameter | Type |
|---|---|
onto | vec2 |
Returns
vec2
projectInPlace()
projectInPlace(
onto):void
Projects this vector onto the given vector in place.
Parameters
| Parameter | Type |
|---|---|
onto | vec2 |
Returns
void
projectOnPlane()
projectOnPlane(
planeNormal):vec2
Returns this vector projected onto the plane defined by the given normal.
Parameters
| Parameter | Type |
|---|---|
planeNormal | vec2 |
Returns
vec2
projectOnPlaneInPlace()
projectOnPlaneInPlace(
planeNormal):void
Projects this vector onto the plane defined by the given normal in place.
Parameters
| Parameter | Type |
|---|---|
planeNormal | vec2 |
Returns
void
randomUnitVector()
staticrandomUnitVector():vec2
Returns a random unit vec2.
Returns
vec2
reflect()
reflect(
planeNormal):vec2
Returns the reflection of this vector off the surface defined by the given normal.
Parameters
| Parameter | Type |
|---|---|
planeNormal | vec2 |
Returns
vec2
reflectInPlace()
reflectInPlace(
planeNormal):void
Reflects this vector off the surface defined by the given normal in place.
Parameters
| Parameter | Type |
|---|---|
planeNormal | vec2 |
Returns
void
right()
staticright():vec2
Static vec2 pointing in the positive x direction (1, 0).
Returns
vec2
scale()
scale(
vec):vec2
Returns a new vec2 scaled by the given scalar.
Parameters
| Parameter | Type |
|---|---|
vec | vec2 |
Returns
vec2
scaleInPlace()
scaleInPlace(
vec):void
Scales this vector by the given scalar in place.
Parameters
| Parameter | Type |
|---|---|
vec | vec2 |
Returns
void
setRandomUnitVector()
setRandomUnitVector():
void
Sets this vector to a random unit vector.
Returns
void
setXY()
setXY(
x?,y?):void
Sets the x and y components of this vector.
Parameters
| Parameter | Type |
|---|---|
x? | number |
y? | number |
Returns
void
sub()
sub(
vec):vec2
Returns a new vec2 that is this vector minus the given vector.
Parameters
| Parameter | Type |
|---|---|
vec | vec2 |
Returns
vec2
subInPlace()
subInPlace(
vec):void
Subtracts the given vector from this vector in place.
Parameters
| Parameter | Type |
|---|---|
vec | vec2 |
Returns
void
toString()
toString():
string
Returns a string representation of this vector.
Returns
string
uniformScale()
uniformScale(
scale):vec2
Returns a new vec2 uniformly scaled by the given scalar.
Parameters
| Parameter | Type |
|---|---|
scale | number |
Returns
vec2
uniformScaleInPlace()
uniformScaleInPlace(
scale):void
Uniformly scales this vector by the given scalar in place.
Parameters
| Parameter | Type |
|---|---|
scale | number |
Returns
void
up()
staticup():vec2
Static vec2 pointing in the positive y direction (0, 1).
Returns
vec2
zero()
staticzero():vec2
Static vec2 with all components set to 0.
Returns
vec2