Oolite JavaScript Reference: Vector3D

From Elite Wiki
Revision as of 17:45, 7 April 2007 by Ahruman (talk | contribs) (Changed convention of equivalences, added some more.)

The Vector class is a geometrical vector in three-dimensional space, in cartesian representation. It is used to represent positions, headings and velocities. Explaining vector geometry is beyond the scope of this document, but there are numerous tutorials on the web.

Vector Expressions

All Oolite-provided functions which take a vector as an argument may instead be passed an Entity instead, in which case the entity’s position is used. In specifications, this is represented by arguments named vectorOrEntity.

Additionally, most Vector methods may be passed three numbers instead of a vector. In specifications, this is represented by arguments named vectorExpression. For example, if a and b are vectors whose values are (0, 1, 0) and (1, 0, 0) respectively, the following are equivalent:

var c = a.add(b);
var d = a.add(1, 0, 0);
// c and d are now both (1, 1, 0).

Properties

x

x [read-write double]

The x co-ordinate of the vector.

y

y [read-write double]

The y co-ordinate of the vector.

z

z [read-write double]

The z co-ordinate of the vector.

Methods

Constructor

new Vector([ vectorExpression]);

Create a new vector with the specified value. If no value is provided, the vector is initialized to (0, 0, 0).

add

Vector add(vectorExpression);

Returns the vector sum of the target and vectorExpression.

subtract

Vector subtract(vectorExpression);

Returns the vector difference between the target and vectorExpression.

distanceTo

double distanceTo(vectorExpression);

Returns the distance between the target and vectorExpression.

v.distanceTo(b) is equivalent to v.subtract(u).magnitude().

squaredDistanceTo

double squaredDistanceTo(vectorExpression);

Returns the square of the distance between the target and vectorExpression.

v.squaredDistanceTo(u) is equivalent to v.distanceTo(u) * v.distanceTo(u), or v.subtract(u).squaredMagnitude().

multiply

Vector multiply(number);

Returns the product of the target and vectorExpression.

dot

double dot(vectorExpression);

Returns the dot product of the target and vectorExpression.

angleTo

double angleTo(vectorExpression);

Returns the angle (in radians) between the target and vectorExpression.

v.angleTo(u) is equivalent to acos(v.direction().dot(u.direction())).

cross

double cross(vectorExpression);

Returns the cross product of the target and vectorExpression.

tripleProduct

double tripleProduct(vectorExpression1, vectorExpression2);

Returns the triple product of the target, vectorExpression1 and vectorExpression2.

v.tripleProduct(u, w) is equivalent to v.dot(u.cross(w)).

direction

Vector direction();

Returns the unit vector with the same direction as the target.

v.direction() is equivalent to v.multiply(1 / v.magnitude()).

magnitude

double magnitude();

Returns the magnitude (or length) of the vector.

squaredMagnitude

double squaredMagnitude();

Returns the square of the magnitude of the vector.

v.squaredMagnitude() is equivalent to v.magnitude() * v.magnitude().

rotateBy

Vector rotateBy(quaternionExpression);

Apply the rotation specified by quaternionExpression to the target.

rotationTo

Quaternion rotationTo(vectorExpression [, maxArc]);

Returns a quaternion corresponding to a rotation from the target to vectorExpression. The optional double parameter maxArc specifies a maximum rotation angle; if the angle between the target and vectorExpression is greater than maxArc radians, a rotation of maxArc radians towards vectorExpression is generated instead.