Skip to content

Class: Vector

Vector is the 2 dimensional representation of a vector, with two values (x,y). This is a mutable data structure, operations on Vector objects will affect the original object.

Hierarchy

Implements

Table of contents

Constructors

Properties

Accessors

Methods

Constructors

constructor

+ new Vector(x: number, y: number): Vector

Parameters:

Name Type
x number
y number

Returns: Vector

Inherited from: Pooled

Properties

data

data: Float32Array


objectInPool

objectInPool: boolean= false

objectInPool is true if an object is made available in the object pool. If it is false it is not currently available in the object pool. This is used to avoid adding the same object to the same object pool multiple times if there are successive calls to free the the same object.

Implementation of: IPoolable.objectInPool

Inherited from: Pooled.objectInPool


CLASS_SERIALIZATION_KEY

Readonly Static CLASS_SERIALIZATION_KEY: jamjar.Vector= "jamjar.Vector"


pools

Protected Static pools: Map<string, [number, IPoolable[]]>

pools is the global, static mapping of string keys to object pools. An object pool contains two pieces of data, the maximum size of the pool (first value), and the objects that make up the pool as an array (second value).

Inherited from: Pooled.pools

Accessors

x

• get x(): number

Returns: number

• set x(value: number): void

Parameters:

Name Type
value number

Returns: void


y

• get y(): number

Returns: number

• set y(value: number): void

Parameters:

Name Type
value number

Returns: void

Methods

Add

Add(vector: Vector): Vector

Add adds two vectors together, result saved to the original Vector and returned.

Parameters:

Name Type Description
vector Vector The vector to add to this one

Returns: Vector

This vector to allow chaining, the result of the addition


Apply3D

Apply3D(matrix: Matrix3D): Vector

Apply3D applies a 3x3 matrix to this vector, result saved to the original Vector and returned.

Parameters:

Name Type Description
matrix Matrix3D Matrix to apply to this vector

Returns: Vector

This vector to allow chaining, Vector that has the matrix applied to it


Apply4D

Apply4D(matrix: Matrix4D): Vector

Apply4D applies a 4x4 matrix to this vector, result saved to the original Vector and returned.

Parameters:

Name Type Description
matrix Matrix4D Matrix to apply to this vector

Returns: Vector

This vector to allow chaining, Vector that has the matrix applied to it


Copy

Copy(): Vector

Copy produces a copy of this vector and its values, rather than pointing to the same vector.

Returns: Vector

The copy of this vector


Dot

Dot(vector: Vector): number

Dot calculates the dot product of two vectors.

Parameters:

Name Type Description
vector Vector The vector to dot with this vector

Returns: number

The result of the dot product


Equals

Equals(other: Vector): boolean

Equals determines if another

Parameters:

Name Type
other Vector

Returns: boolean


Free

Free(): void

Free releases an object or it's constituent parts back into any available object pools.

Returns: void

Implementation of: IPoolable


Invert

Invert(): Vector

Invert flips the values of this vector, x -> -x and y -> -y, result saved to the original Vector and returned.

Returns: Vector

This vector to allow chaining, the result of the inverting


Magnitude

Magnitude(): number

Calculates magnitude of this vector.

Returns: number

The magnitude


Multiply

Multiply(vector: Vector): Vector

Multiply multiplies two vectors together, result saved to the original Vector and returned.

Parameters:

Name Type Description
vector Vector The matrix to multiply this one by

Returns: Vector

This vector to allow chaining, the result of the multiplication


Normalize

Normalize(): Vector

Returns a normalized version of this vector, result saved to the original Vector and returned.

Returns: Vector

This vector to allow chaining, the normalized vector


Recycle

Recycle(x: number, y: number): Vector

Parameters:

Name Type
x number
y number

Returns: Vector


Rotate

Rotate(center: Vector, angle: number): Vector

Rotate applies a rotation around a point to the vector in radians, result saved to the original Vector and returned.

Parameters:

Name Type Description
center Vector The point to rotate around
angle number The angle in radians to rotate by

Returns: Vector

This vector to allow chaining, the result of the rotation


RotateDeg

RotateDeg(center: Vector, angle: number): Vector

RotateDeg applies a rotation around a point to the vector in degrees, result saved to the original Vector and returned.

Parameters:

Name Type Description
center Vector The point to rotate around
angle number The angle in degrees to rotate by

Returns: Vector

This vector to allow chaining, the result of the rotation


Scale

Scale(scalar: number): Vector

Scale multiplies this vector by a scalar value (non-vector), result saved to the original Vector and returned.

Parameters:

Name Type Description
scalar number The scalar value to multiply this vector by

Returns: Vector

This vector to allow chaining, the result of the scaling


Serialize

Serialize(): string

Returns: string

Implementation of: ISerializable


Sub

Sub(vector: Vector): Vector

Sub takes one vector from another, result saved to the original Vector and returned.

Parameters:

Name Type Description
vector Vector The vector to subtract from this one

Returns: Vector

This vector to allow chaining, the result result of the subtraction


Deserialize

StaticDeserialize(json: any): Vector

Parameters:

Name Type
json any

Returns: Vector


Free

StaticFree(obj: Vector): void

Free the provided vector.

Parameters:

Name Type
obj Vector

Returns: void


Init

StaticInit(size: number): void

Initialize the Vector pool to the size provided.

Parameters:

Name Type
size number

Returns: void


New

StaticNew(x: number, y: number): Vector

Create a new Vector, using pooling if available.

Parameters:

Name Type
x number
y number

Returns: Vector


free

Protected Staticfree(poolKey: string, obj: IPoolable): void

free is used to mark a provided object as free in the pool provided. This method can be called multiple times with the same object, it will only add one entry to the pool.

Parameters:

Name Type Description
poolKey string The key of the pool to add the object to.
obj IPoolable The object to add to the pool.

Returns: void

Inherited from: Pooled


init

Protected Staticinit(poolKey: string, emptyGenerator: () => IPoolable, size: number): void

init is used to initialize an object pool to a certain size. This method takes a key of the pool to initialize, an 'empty generator' which is a function that should return an empty/blank instance of the object being pooled which can be overwritten at a later point, and the maximum size of the pool (which it will be initialized to at the start using the empty generator).

Parameters:

Name Type
poolKey string
emptyGenerator () => IPoolable
size number

Returns: void

Inherited from: Pooled


new

Protected Staticnew(poolKey: *string*, type: (...args: *any*) => T, ...args: any): T

new is used to request a new object from the pool specified, if the pool is unavailable or empty it will use the type to provision a new object through a constructor. This is a generic method, it includes a cast to the generic type provided - this cast can fail if the objects returned from the pool are not the type expected.

Type parameters:

Name Type
T IPoolable

Parameters:

Name Type Description
poolKey string The key of the pool to retrieve from.
type (...args: any) => T The fallback constructor to use if the pool is not initialized/empty.
...args any The args to use when creating/recycling the object.

Returns: T

Inherited from: Pooled