Crafty.math.Vector2D Class

Vector2D has the following constructors:

public {Vector2D} new Vector2D();
[Returns]

A new vector with x and y equal to 0

public {Vector2D} new Vector2D(Number x, Number y);
x

The initial x value

y

The initial y value

[Returns]
  • A new vector with the given x and y values
public {Vector2D} new Vector2D(Vector2D vector);
vector

A vector to copy

[Returns]

A new vector with the copied x and y values

Example

var v1 = new Crafty.math.Vector2D(3, 5);
var v2 = new Crafty.math.Vector2D(v1);

Methods

Back to top

.add()

Adds the passed vector to this vector

public {Vector2D} add(Vector2D vecRH);
vecRH

The vector to add

[Returns]

The resulting modified vector

Back to top

.angleBetween()

Calculates the angle between the passed vector and this vector, using <0,0> as the point of reference. Angles returned have the range (−π, π].

public {Number} angleBetween(Vector2D vecRH);
vecRH

The vector to compare

[Returns]

the angle between the two vectors in radians

Back to top

.angleTo()

Calculates the angle to the passed vector from this vector, using this vector as the point of reference.

public {Number} angleTo(Vector2D vecRH);
vecRH

The vector to compare

[Returns]

the angle to the passed vector in radians

Back to top

.clone()

Creates and exact, numeric copy of this vector

public {Vector2D} clone();
[Returns]

the new vector

Back to top

.crossProduct()

Calculates the z component of the cross product of the two vectors augmented to 3D.

public {Number} crossProduct(Vector2D vecRH);
vecRH

The passed vector

[Returns]

the resultant cross product

Back to top

.distance()

Calculates the distance from this vector to the passed vector.

public {Number} distance(Vector2D vecRH);
vecRH

The passed vector

[Returns]

the distance between the two vectors

Back to top

.distanceSq()

Calculates the squared distance from this vector to the passed vector. This function avoids calculating the square root, thus being slightly faster than .distance( ).

public {Number} distanceSq(Vector2D vecRH);
vecRH

The passed vector

[Returns]

the squared distance between the two vectors

See Also

Back to top

.divide()

Divides this vector by the passed vector.

public {Vector2D} divide(Vector2D vecRH);
vecRH

The passed vector

[Returns]

this vector after dividing

Back to top

.dotProduct()

Calculates the dot product of this and the passed vectors

public {Number} dotProduct(Vector2D vecRH);
vecRH

The passed vector

[Returns]

the resultant dot product

Back to top

.equals()

Determines if this vector is numerically equivalent to the passed vector.

public {Boolean} equals(Vector2D vecRH);
vecRH

The passed vector

[Returns]

true if the vectors are equivalent

Back to top

.getNormal()

Calculates a new right-handed unit vector that is perpendicular to the line created by this and the passed vector.

public {Vector2D} getNormal(Vector2D vecRH[, Vector2D result]);
vecRH

The passed vector

[result]

An optional parameter to save the result in

[Returns]

the new normal vector

Back to top

.isZero()

Determines if this vector is equal to <0,0>

public {Boolean} isZero();
[Returns]

true if this vector is equal to <0,0>

Back to top

.magnitude()

Calculates the magnitude of this vector. Note: Function objects in JavaScript already have a 'length' member, hence the use of magnitude instead.

public {Number} magnitude();
[Returns]

the magnitude of this vector

Back to top

.magnitudeSq()

Calculates the square of the magnitude of this vector. This function avoids calculating the square root, thus being slightly faster than .magnitude( ).

public {Number} magnitudeSq();
[Returns]

the square of the magnitude of this vector

See Also

Back to top

.multiply()

Multiplies this vector by the passed vector, using component-wise multiplciation

public {Vector2D} multiply(Vector2D vecRH);
vecRH

The passed vector

[Returns]

this vector after multiplying

Back to top

.negate()

Negates this vector (ie. <-x,-y>)

public {Vector2D} negate();
[Returns]

this vector after negation

Back to top

.normalize()

Normalizes this vector (scales the vector so that its new magnitude is 1) For vectors where magnitude is 0, <1,0> is returned.

public {Vector2D} normalize();
[Returns]

this vector after normalization

Back to top

.perpendicular()

Calculates a new vector that is perpendicular to this vector. The perpendicular vector has the same magnitude as this vector and is obtained by a counter-clockwise rotation of 90° of this vector.

public {Vector2D} perpendicular([Vector2D result]);
[result]

An optional parameter to save the result in

[Returns]

the perpendicular vector

Back to top

.scale()

Scales this vector by the passed amount(s)

public {Vector2D} scale(Number scale);
scale

The amount to scale by

[Returns]

this after scaling

public {Vector2D} scale(Number scalarX, Number scalarY);
scalarX

The amount to scale x by

[scalarY]

The amount to scale y by

[Returns]

this after scaling

Back to top

.scaleToMagnitude()

Scales this vector such that its new magnitude is equal to the passed value.

public {Vector2D} scaleToMagnitude(Number mag);
mag

The desired magnitude

[Returns]

this vector after scaling

Back to top

.setValues()

Sets the values of this vector using a passed vector or pair of numbers.

public {Vector2D} setValues(Vector2D vector);
vector

a vector to copy

[Returns]

this vector after copying the values

public {Vector2D} setValues(Number x, Number y);
x

The x value to set

y

The y value to set

[Returns]

this vector after setting the values

Back to top

.subtract()

Subtracts the passed vector from this vector.

public {Vector2D} subtract(Vector2D vecRH);
vecRH

the passed vector to subtract

[Returns]

this vector after subtracting

Back to top

.toString()

Returns a string representation of this vector.

public {String} toString();
[Returns]

A representation like "Vector2D(4, 7)"

Back to top

.translate()

Translates (moves) this vector by the passed amounts. If dy is omitted, dx is used for both axes.

public {Vector2D} translate(Number dx[, Number dy]);
dx

The amount to shift by

[dy]

The amount to shift along the y axis

[Returns]

this vector after translating

Back to top

.tripleProduct()

Calculates the triple product of three vectors. triple vector product = b(a•c) - a(b•c)

public {Vector2D} tripleProduct(Vector2D a, Vector2D b, Vector2D c, [Vector2D result]);
a

The first vector

b

The second vector

c

The third vector

[result]

An optional parameter to save the result in

[Returns]

the triple product as a new vector