Difference between revisions of "Oolite JavaScript Reference: Vector3D"

From Elite Wiki
m (Added rotateBy().)
m (Changed convention of equivalences, added some more.)
Line 27: Line 27:
 
== Methods ==
 
== Methods ==
 
=== Constructor ===
 
=== Constructor ===
new Vector([ [[#Vector Expressions|vectorExpression]] ]);
+
new Vector([ [[#Vector Expressions|vectorExpression]]]);
 
Create a new vector with the specified value. If no value is provided, the vector is initialized to (0, 0, 0).
 
Create a new vector with the specified value. If no value is provided, the vector is initialized to (0, 0, 0).
   
Line 42: Line 42:
 
Returns the distance between the target and <code>[[#Vector Expressions|vectorExpression]]</code>.
 
Returns the distance between the target and <code>[[#Vector Expressions|vectorExpression]]</code>.
   
<code>a.distanceTo(b)</code> is equivalent to <code>a.[[#subtract|subtract]](b).[[#magnitude|magnitude]]()</code>.
+
<code>v.distanceTo(b)</code> is equivalent to <code>v.[[#subtract|subtract]](u).[[#magnitude|magnitude]]()</code>.
   
 
=== <code>squaredDistanceTo</code> ===
 
=== <code>squaredDistanceTo</code> ===
Line 48: Line 48:
 
Returns the square of the distance between the target and <code>[[#Vector Expressions|vectorExpression]]</code>.
 
Returns the square of the distance between the target and <code>[[#Vector Expressions|vectorExpression]]</code>.
   
<code>a.squaredDistanceTo(b)</code> is equivalent to <code>a.[[#distanceTo|distanceTo]](b) * a.[[#distanceTo|distanceTo]](b)</code>.
+
<code>v.squaredDistanceTo(u)</code> is equivalent to <code>v.[[#distanceTo|distanceTo]](u) * v.[[#distanceTo|distanceTo]](u)</code>, or <code>v.[[#subtract|subtract]](u).[[#squaredMagnitude|squaredMagnitude]]()</code>.
   
 
=== <code>multiply</code> ===
 
=== <code>multiply</code> ===
Line 62: Line 62:
 
Returns the angle (in radians) between the target and <code>[[#Vector Expressions|vectorExpression]]</code>.
 
Returns the angle (in radians) between the target and <code>[[#Vector Expressions|vectorExpression]]</code>.
   
<code>a.angleTo(b)</code> is equivalent to <code>[http://www.lib.tsinghua.edu.cn/chinese/INTERNET/JavaScript/methods.html#acos_method acos](a.[[#direction|direction]]().[[#dot|dot]](b.[[#direction|direction]]()))</code>.
+
<code>v.angleTo(u)</code> is equivalent to <code>[http://www.lib.tsinghua.edu.cn/chinese/INTERNET/JavaScript/methods.html#acos_method acos](v.[[#direction|direction]]().[[#dot|dot]](u.[[#direction|direction]]()))</code>.
   
 
=== <code>cross</code> ===
 
=== <code>cross</code> ===
Line 72: Line 72:
 
Returns the [http://en.wikipedia.org/wiki/Vector_%28spatial%29#Triple_product triple product] of the target, <code>[[#Vector Expressions|vectorExpression1]]</code> and <code>[[#Vector Expressions|vectorExpression2]]</code>.
 
Returns the [http://en.wikipedia.org/wiki/Vector_%28spatial%29#Triple_product triple product] of the target, <code>[[#Vector Expressions|vectorExpression1]]</code> and <code>[[#Vector Expressions|vectorExpression2]]</code>.
   
<code>a.tripleProduct(b, c)</code> is equivalent to <code>a.[[#dot|dot]](b.[[#cross|cross]](c))</code>.
+
<code>v.tripleProduct(u, w)</code> is equivalent to <code>v.[[#dot|dot]](u.[[#cross|cross]](w))</code>.
   
 
=== <code>direction</code> ===
 
=== <code>direction</code> ===
Line 78: Line 78:
 
Returns the [http://en.wikipedia.org/wiki/Unit_vector unit vector] with the same direction as the target.
 
Returns the [http://en.wikipedia.org/wiki/Unit_vector unit vector] with the same direction as the target.
   
<code>a.direction()</code> is equivalent to <code>a * (1 / a.[[#magnitude|magnitude]]())</code>.
+
<code>v.direction()</code> is equivalent to <code>v.[[#multiply|multiply]](1 / v.[[#magnitude|magnitude]]())</code>.
   
 
=== <code>magnitude</code> ===
 
=== <code>magnitude</code> ===
Line 88: Line 88:
 
Returns the square of the magnitude of the vector.
 
Returns the square of the magnitude of the vector.
   
<code>a. squaredMagnitude()</code> is equivalent to <code>a.[[#magnitude|magnitude]]() * a.[[#magnitude|magnitude]]()</code>.
+
<code>v.squaredMagnitude()</code> is equivalent to <code>v.[[#magnitude|magnitude]]() * v.[[#magnitude|magnitude]]()</code>.
   
 
=== <code>rotateBy</code> ===
 
=== <code>rotateBy</code> ===

Revision as of 17:45, 7 April 2007

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.