Skip to content

Documentation / base / Vector2

Class: Vector2

Defined in: libs/base/src/math/vector.ts:85

2 dimentional vector

Extends

Extended by

Indexable

[index: number]: number

Constructors

Constructor

new Vector2(x, y): Vector2

Defined in: libs/base/src/math/vector.ts:91

Creates a new Vector2 initialized with values.

Parameters

x

number

The x component.

y

number

The y component.

Returns

Vector2

Overrides

VectorBase.constructor

Constructor

new Vector2(elements): Vector2

Defined in: libs/base/src/math/vector.ts:96

Creates a new Vector2 initialized with values in an array.

Parameters

elements

number[]

Array that contains the x, y values.

Returns

Vector2

Overrides

VectorBase.constructor

Constructor

new Vector2(array): Vector2

Defined in: libs/base/src/math/vector.ts:101

Creates a new Vector2 initialized with values in a Float32Array.

Parameters

array

Float32Array<ArrayBuffer>

Float32Array object that contains the x, y values.

Returns

Vector2

Overrides

VectorBase.constructor

Constructor

new Vector2(buffer, offset): Vector2

Defined in: libs/base/src/math/vector.ts:107

Creates a new Vector2 placed on a given ArrayBuffer object.

Parameters

buffer

ArrayBuffer

The array buffer object.

offset

number

The byte offset of the buffer where the vector placed at.

Returns

Vector2

Overrides

VectorBase.constructor

Constructor

new Vector2(): Vector2

Defined in: libs/base/src/math/vector.ts:111

Creates a new Vector2 filled with zero values.

Returns

Vector2

Overrides

VectorBase.constructor

Accessors

x

Get Signature

get x(): number

Defined in: libs/base/src/math/vector.ts:136

Get the x component value.

Returns

number

Set Signature

set x(v): void

Defined in: libs/base/src/math/vector.ts:139

Parameters
v

number

Returns

void


y

Get Signature

get y(): number

Defined in: libs/base/src/math/vector.ts:143

Get the y component value.

Returns

number

Set Signature

set y(v): void

Defined in: libs/base/src/math/vector.ts:146

Parameters
v

number

Returns

void


magnitude

Get Signature

get magnitude(): number

Defined in: libs/base/src/math/vector.ts:150

Get the length of the vector.

Returns

number


magnitudeSq

Get Signature

get magnitudeSq(): number

Defined in: libs/base/src/math/vector.ts:154

Get the squared length of the vector.

Returns

number

Methods

equalsTo()

equalsTo(other, epsl?): boolean

Defined in: libs/base/src/math/vector.ts:31

Check if all data is close enough to another

Parameters

other

Float32Array<ArrayBuffer>

The data to be compared with.

epsl?

number

Returns

boolean

true if close enough, otherwise false.

Inherited from

VectorBase.equalsTo


toString()

toString(): string

Defined in: libs/base/src/math/vector.ts:51

Convert this to string object.

Returns

string

Inherited from

VectorBase.toString


isNaN()

isNaN(): boolean

Defined in: libs/base/src/math/vector.ts:60

Check the data for the presence of NaN.

Returns

boolean

true if NaN is present, otherwise false.

Inherited from

VectorBase.isNaN


setRandom()

setRandom(minValue, maxValue): void

Defined in: libs/base/src/math/vector.ts:74

Generate random vector

Parameters

minValue

number

Minimum value of any component of the vector

maxValue

number

Maximum value of any component of the vector

Returns

void

Inherited from

VectorBase.setRandom


clone()

clone(): Vector2

Defined in: libs/base/src/math/vector.ts:132

Creates a new Vector2 initialized with values from this vector.

Returns

Vector2

The new Vector2.


setXY()

setXY(x, y): Vector2

Defined in: libs/base/src/math/vector.ts:163

Set component values.

Parameters

x

number

The x component value.

y

number

The y component value.

Returns

Vector2

self


setAndNormalize()

setAndNormalize(x, y): Vector2

Defined in: libs/base/src/math/vector.ts:174

Set component values and then normalize the vector.

Parameters

x

number

The x component value.

y

number

The y component value.

Returns

Vector2

self


subBy()

subBy(other): Vector2

Defined in: libs/base/src/math/vector.ts:183

Subtract a vector from this vector.

Parameters

other

Vector2

The vector that will be subtract.

Returns

Vector2

self


addBy()

addBy(other): Vector2

Defined in: libs/base/src/math/vector.ts:192

Add a vector to this vector.

Parameters

other

Vector2

The vector that will be added.

Returns

Vector2

self


combineBy()

combineBy(other, t0, t1): Vector2

Defined in: libs/base/src/math/vector.ts:203

Combine a vector to this vector.

Parameters

other

Vector2

The vector that will be added.

t0

number

Scale factor for this vector

t1

number

Scale factor for other vector

Returns

Vector2

self


mulBy()

mulBy(other): Vector2

Defined in: libs/base/src/math/vector.ts:212

Multiply this vector by a vector.

Parameters

other

Vector2

The vector that will be multiplied by.

Returns

Vector2

self


divBy()

divBy(other): Vector2

Defined in: libs/base/src/math/vector.ts:221

Divide this vector by a vector.

Parameters

other

Vector2

The vector that will be divide by.

Returns

Vector2

self


scaleBy()

scaleBy(f): Vector2

Defined in: libs/base/src/math/vector.ts:230

Scale this vector by a scalar number.

Parameters

f

number

amount to scale this vector by.

Returns

Vector2

self


inplaceNormalize()

inplaceNormalize(): Vector2

Defined in: libs/base/src/math/vector.ts:238

Normalize this vector inplace.

Returns

Vector2

self


inplaceInverse()

inplaceInverse(): Vector2

Defined in: libs/base/src/math/vector.ts:246

Inverse this vector inplace.

Returns

Vector2

self


inplaceMin()

inplaceMin(other): Vector2

Defined in: libs/base/src/math/vector.ts:255

Set the component values to the minimum of this vector and another vector.

Parameters

other

Vector2

The vector that to be compared with.

Returns

Vector2

self


inplaceMax()

inplaceMax(other): Vector2

Defined in: libs/base/src/math/vector.ts:264

Set the component values to the maximum of this vector and another vector.

Parameters

other

Vector2

The vector that to be compared with.

Returns

Vector2

self


zero()

static zero(): Vector2

Defined in: libs/base/src/math/vector.ts:272

Creates a new Vector2 initialized with zero values.

Returns

Vector2

The new vector


one()

static one(): Vector2

Defined in: libs/base/src/math/vector.ts:279

Creates a new Vector2 initialized with one.

Returns

Vector2

The new vector


axisPX()

static axisPX(): Vector2

Defined in: libs/base/src/math/vector.ts:286

Creates a new Vector2 pointing in the positive direction of the X axis, i.e. vec2(1, 0)

Returns

Vector2

The new vector


axisNX()

static axisNX(): Vector2

Defined in: libs/base/src/math/vector.ts:293

Creates a new Vector2 pointing in the negative direction of the X axis, i.e. vec2(-1, 0)

Returns

Vector2

The new vector


axisPY()

static axisPY(): Vector2

Defined in: libs/base/src/math/vector.ts:300

Creates a new Vector2 pointing in the positive direction of the Y axis, i.e. vec2(0, 1)

Returns

Vector2

The new vector


axisNY()

static axisNY(): Vector2

Defined in: libs/base/src/math/vector.ts:307

Creates a new Vector2 pointing in the negative direction of the Y axis, i.e. vec2(0, -1)

Returns

Vector2

The new vector


distance()

static distance(v1, v2): number

Defined in: libs/base/src/math/vector.ts:316

Calculates the distance between two Vector2's.

Parameters

v1

Vector2

The first vector.

v2

Vector2

The second vector.

Returns

number

distance between v1 and v2


distanceSq()

static distanceSq(v1, v2): number

Defined in: libs/base/src/math/vector.ts:325

Calculates the squared distance between two Vector2's.

Parameters

v1

Vector2

The first vector.

v2

Vector2

The second vector.

Returns

number

squared distance between v1 and v2


normalize()

static normalize(v, result?): Vector2

Defined in: libs/base/src/math/vector.ts:336

Normalize a Vector2

Parameters

v

Vector2

The input vector

result?

Vector2

The output vector (can be the same vector as v). if not specified, a new vector will be created.

Returns

Vector2

The output vector


inverse()

static inverse(v, result?): Vector2

Defined in: libs/base/src/math/vector.ts:348

Inverse a Vector2

Parameters

v

Vector2

The input vector

result?

Vector2

The output vector (can be the same vector as v). if not specified, a new vector will be created.

Returns

Vector2

The output vector


sub()

static sub(a, b, result?): Vector2

Defined in: libs/base/src/math/vector.ts:360

Subtract two Vector2's.

Parameters

a

Vector2

The first operand.

b

Vector2

The second operand.

result?

Vector2

The output vector (can be the same vector as a or b). if not specified, a new vector will be created.

Returns

Vector2

The output vector


add()

static add(a, b, result?): Vector2

Defined in: libs/base/src/math/vector.ts:372

Add two Vector2's.

Parameters

a

Vector2

The first operand.

b

Vector2

The second operand.

result?

Vector2

The output vector (can be the same vector as a or b). if not specified, a new vector will be created.

Returns

Vector2

The output vector


combine()

static combine(a, b, t0, t1, result?): Vector2

Defined in: libs/base/src/math/vector.ts:386

Combine two Vector2's.

Parameters

a

Vector2

The first operand.

b

Vector2

The second operand.

t0

number

Scale factor for the first operand

t1

number

Scale factor for the second operand

result?

Vector2

The output vector (can be the same vector as a or b). if not specified, a new vector will be created.

Returns

Vector2

The output vector


lerp()

static lerp(a, b, t, result?): Vector2

Defined in: libs/base/src/math/vector.ts:399

Linearly interpolate between two Vector2's.

Parameters

a

Vector2

The first operand.

b

Vector2

The second operand.

t

number

The interpolation factor. The result is equal to a when t = 0 and equal to b when t = 1.

result?

Vector2

The output vector (can be the same vector as a or b). if not specified, a new vector will be created.

Returns

Vector2

The output vector


mul()

static mul(a, b, result?): Vector2

Defined in: libs/base/src/math/vector.ts:411

Multiply two Vector2's.

Parameters

a

Vector2

The first operand.

b

Vector2

The second operand.

result?

Vector2

The output vector (can be the same vector as a or b). if not specified, a new vector will be created.

Returns

Vector2

The output vector


div()

static div(a, b, result?): Vector2

Defined in: libs/base/src/math/vector.ts:423

Divide two Vector2's.

Parameters

a

Vector2

The first operand.

b

Vector2

The second operand.

result?

Vector2

The output vector (can be the same vector as a or b). if not specified, a new vector will be created.

Returns

Vector2

The output vector


scale()

static scale(a, b, result?): Vector2

Defined in: libs/base/src/math/vector.ts:435

Scale a Vector2 by a scalar number.

Parameters

a

Vector2

The vector to be scaled.

b

number

The scalar number.

result?

Vector2

The output vector (can be the same vector as a). if not specified, a new vector will be created.

Returns

Vector2

The output vector


min()

static min(a, b, result?): Vector2

Defined in: libs/base/src/math/vector.ts:447

Calculates the minimum of two Vector2's.

Parameters

a

Vector2

The first operand.

b

Vector2

The second operand.

result?

Vector2

The output vector (can be the same vector as a or b). if not specified, a new vector will be created.

Returns

Vector2

The output vector


max()

static max(a, b, result?): Vector2

Defined in: libs/base/src/math/vector.ts:459

Calculates the maximum of two Vector2's.

Parameters

a

Vector2

The first operand.

b

Vector2

The second operand.

result?

Vector2

The output vector (can be the same vector as a or b). if not specified, a new vector will be created.

Returns

Vector2

The output vector


abs()

static abs(a, result?): Vector2

Defined in: libs/base/src/math/vector.ts:470

Calculates the absolute values of a Vector2.

Parameters

a

Vector2

The input vector.

result?

Vector2

The output vector (can be the same vector as a or b). if not specified, a new vector will be created.

Returns

Vector2

The output vector


dot()

static dot(a, b): number

Defined in: libs/base/src/math/vector.ts:481

Calculates the dot product of two Vector2's.

Parameters

a

Vector2

The first operand.

b

Vector2

The second operand.

Returns

number

dot product of a and b


cross()

static cross(a, b): number

Defined in: libs/base/src/math/vector.ts:490

Calculates the cross product of two Vector2's.

Parameters

a

Vector2

The first operand.

b

Vector2

The second operand.

Returns

number

z component of the cross product of the two vectors.

Released under the MIT License.