Difference between revisions of "Oolite JavaScript Reference: Vector3D"

From Elite Wiki
m (Added category:Oolite.)
(Cleaning up JavaScript pages with more idiomatic style (with JS 2-style type annotations).)
Line 1: Line 1:
 +
<small>'''Prototype:''' <code>Object</code></small><br />
 +
<small>'''Subtypes:''' none</small>
 +
 
The '''<code>Vector</code>''' class is a [http://en.wikipedia.org/wiki/Vector_%28spatial%29 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.
 
The '''<code>Vector</code>''' class is a [http://en.wikipedia.org/wiki/Vector_%28spatial%29 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.
  
Line 12: Line 15:
 
== Properties ==
 
== Properties ==
 
=== <code>x</code> ===
 
=== <code>x</code> ===
  x [read-write double]
+
  '''x''' : Number (read/write)
 
The ''x'' co-ordinate of the vector.
 
The ''x'' co-ordinate of the vector.
  
 
=== <code>y</code> ===
 
=== <code>y</code> ===
  y [read-write double]
+
  '''y''' : Number (read/write)
 
The ''y'' co-ordinate of the vector.
 
The ''y'' co-ordinate of the vector.
  
 
=== <code>z</code> ===
 
=== <code>z</code> ===
  z [read-write double]
+
  '''z''' : Number (read/write)
 
The ''z'' co-ordinate of the vector.
 
The ''z'' co-ordinate of the vector.
  
 
== Methods ==
 
== Methods ==
 
=== Constructor ===
 
=== Constructor ===
  new Vector([ [[#Vector Expressions|vectorExpression]]]);
+
  '''new Vector'''([value : [[#Vector Expressions|vectorExpression]]]) : Vector
 
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).
  
 
=== <code>add</code> ===
 
=== <code>add</code> ===
  Vector add([[#Vector Expressions|vectorExpression]]);
+
  function '''add'''(v : [[#Vector Expressions|vectorExpression]]) : Vector
Returns the vector sum of the target and <code>[[#Vector Expressions|vectorExpression]]</code>.
+
Returns the vector sum of the target and <code>v</code>.
 
 
=== <code>subtract</code> ===
 
Vector subtract([[#Vector Expressions|vectorExpression]]);
 
Returns the vector difference between the target and <code>[[#Vector Expressions|vectorExpression]]</code>.
 
 
 
=== <code>distanceTo</code> ===
 
double distanceTo([[#Vector Expressions|vectorExpression]]);
 
Returns the distance between the target and <code>[[#Vector Expressions|vectorExpression]]</code>.
 
 
 
<code>v.distanceTo(b)</code> is equivalent to <code>v.[[#subtract|subtract]](u).[[#magnitude|magnitude]]()</code>.
 
  
=== <code>squaredDistanceTo</code> ===
+
'''See Also:''' <code>[[#subtract|subtract]]()</code>
double squaredDistanceTo([[#Vector Expressions|vectorExpression]]);
 
Returns the square of the distance between the target and <code>[[#Vector Expressions|vectorExpression]]</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> ===
 
Vector multiply(number);
 
Returns the product of the target and <code>[[#Vector Expressions|vectorExpression]]</code>.
 
 
 
=== <code>dot</code> ===
 
double dot([[#Vector Expressions|vectorExpression]]);
 
Returns the [http://en.wikipedia.org/wiki/Vector_%28spatial%29#Dot_product dot product] of the target and <code>[[#Vector Expressions|vectorExpression]]</code>.
 
  
 
=== <code>angleTo</code> ===
 
=== <code>angleTo</code> ===
  double angleTo([[#Vector Expressions|vectorExpression]]);
+
  function '''angleTo'''(v : [[#Vector Expressions|vectorExpression]]) : Number
 
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>.
  
Line 63: Line 44:
  
 
=== <code>cross</code> ===
 
=== <code>cross</code> ===
  double cross([[#Vector Expressions|vectorExpression]]);
+
  function '''cross'''(v : [[#Vector Expressions|vectorExpression]]) : Vector
 
Returns the [http://en.wikipedia.org/wiki/Vector_%28spatial%29#Cross_product cross product] of the target and <code>[[#Vector Expressions|vectorExpression]]</code>.
 
Returns the [http://en.wikipedia.org/wiki/Vector_%28spatial%29#Cross_product cross product] of the target and <code>[[#Vector Expressions|vectorExpression]]</code>.
  
=== <code>tripleProduct</code> ===
+
'''See Also:''' <code>[[#dot|dot]]()</code>
double tripleProduct([[#Vector Expressions|vectorExpression1]], [[#Vector Expressions|vectorExpression2]]);
 
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>v.tripleProduct(u, w)</code> is equivalent to <code>v.[[#dot|dot]](u.[[#cross|cross]](w))</code>.
 
  
 
=== <code>direction</code> ===
 
=== <code>direction</code> ===
  Vector direction();
+
  function '''direction'''() : Vector
 
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>v.direction()</code> is equivalent to <code>v.[[#multiply|multiply]](1 / v.[[#magnitude|magnitude]]())</code>.
 
<code>v.direction()</code> is equivalent to <code>v.[[#multiply|multiply]](1 / v.[[#magnitude|magnitude]]())</code>.
 +
 +
'''See Also:''' <code>[[#magnitude|magnitude]]()</code>
 +
 +
=== <code>distanceTo</code> ===
 +
function '''distanceTo'''(v : [[#Vector Expressions|vectorExpression]]) : Number
 +
Returns the distance between the target and <code>v</code>.
 +
 +
<code>u.distanceTo(v)</code> is equivalent to <code>u.[[#subtract|subtract]](v).[[#magnitude|magnitude]]()</code>.
 +
 +
'''See Also:''' <code>[[#squaredDistanceTo|squaredDistanceTo]]()</code>
 +
 +
=== <code>dot</code> ===
 +
function '''dot'''(v : [[#Vector Expressions|vectorExpression]]) : Number
 +
Returns the [http://en.wikipedia.org/wiki/Vector_%28spatial%29#Dot_product dot product] of the target and <code>v</code>.
 +
 +
'''See Also:''' <code>[[#cross|cross]]()</code>
  
 
=== <code>magnitude</code> ===
 
=== <code>magnitude</code> ===
  double magnitude();
+
  function '''magnitude'''() : Number
 
Returns the magnitude (or length) of the vector.
 
Returns the magnitude (or length) of the vector.
 +
 +
'''See Also:''' <code>[[#squaredMagnitude|squaredMagnitude]]()</code>, <code>[[#direction|direction]]()</code>
 +
 +
=== <code>multiply</code> ===
 +
function '''multiply'''(f : Number) : Vector
 +
Returns the product of the target and <code>f</code>. This has the effect of scaling the vector by the factor <code>f</code>.
 +
 +
=== <code>rotateBy</code> ===
 +
function '''rotateBy'''(q : [[Oolite JavaScript Reference: Quaternion#Quaternion Expressions|quaternionExpression]]) : Vector
 +
Apply the rotation specified by <code>q</code> to the target.
 +
 +
=== <code>rotationTo</code> ===
 +
function '''rotationTo'''(v : [[#Vector Expressions|vectorExpression]] [, maxArc : Number]) : [[Oolite JavaScript Reference: Quaternion|Quaternion]]
 +
Returns a [[Oolite JavaScript Reference: Quaternion|quaternion]] corresponding to a rotation from the target to <code>v</code>. The optional parameter <code>maxArc</code> specifies a maximum rotation angle; if the angle between the target and <code>v</code> is greater than <code>maxArc</code> radians, a rotation of <code>maxArc</code> radians towards <code>vectorExpression</code> is generated instead.
 +
 +
=== <code>subtract</code> ===
 +
function '''subtract'''(v : [[#Vector Expressions|vectorExpression]]) : Vector
 +
Returns the vector difference between the target and <code>v</code>.
 +
 +
'''See Also:''' <code>[[#add|add]]()</code>
 +
 +
=== <code>squaredDistanceTo</code> ===
 +
function '''squaredDistanceTo'''(v: [[#Vector Expressions|vectorExpression]]) : Number
 +
Returns the square of the distance between the target and <code>v</code>.
 +
 +
<code>u.squaredDistanceTo(v)</code> is equivalent to <code>u.[[#distanceTo|distanceTo]](v) * u.[[#distanceTo|distanceTo]](v)</code>, or <code>u.[[#subtract|subtract]](v).[[#squaredMagnitude|squaredMagnitude]]()</code>.
  
 
=== <code>squaredMagnitude</code> ===
 
=== <code>squaredMagnitude</code> ===
  double squaredMagnitude();
+
  function '''squaredMagnitude() : Number
 
Returns the square of the magnitude of the vector.
 
Returns the square of the magnitude of the vector.
  
 
<code>v.squaredMagnitude()</code> is equivalent to <code>v.[[#magnitude|magnitude]]() * v.[[#magnitude|magnitude]]()</code>.
 
<code>v.squaredMagnitude()</code> is equivalent to <code>v.[[#magnitude|magnitude]]() * v.[[#magnitude|magnitude]]()</code>.
  
=== <code>rotateBy</code> ===
+
=== <code>tripleProduct</code> ===
  Vector rotateBy([[Oolite JavaScript Reference: Quaternion#Quaternion Expressions|quaternionExpression]]);
+
  function '''tripleProduct'''(v : [[#Vector Expressions|vectorExpression]], w : [[#Vector Expressions|vectorExpression]]) : Number
Apply the rotation specified by <code>[[Oolite JavaScript Reference: Quaternion#Quaternion Expressions|quaternionExpression]]</code> to the target.
+
Returns the [http://en.wikipedia.org/wiki/Vector_%28spatial%29#Triple_product triple product] of the target, <code>v</code> and <code>w</code>.
  
=== <code>rotationTo</code> ===
+
<code>u.tripleProduct(v, w)</code> is equivalent to <code>u.[[#dot|dot]](v.[[#cross|cross]](w))</code>.
[[Oolite JavaScript Reference: Quaternion|Quaternion]] rotationTo([[#Vector Expressions|vectorExpression]] [, maxArc]);
 
Returns a [[Oolite JavaScript Reference: Quaternion|quaternion]] corresponding to a rotation from the target to <code>[[#Vector Expressions|vectorExpression]]</code>. The optional <code>double</code> parameter <code>maxArc</code> specifies a maximum rotation angle; if the angle between the target and <code>[[#Vector Expressions|vectorExpression]]</code> is greater than <code>maxArc</code> radians, a rotation of <code>maxArc</code> radians towards <code>vectorExpression</code> is generated instead.
 
  
  
 
[[Category:Oolite scripting]] [[Category: Oolite]]
 
[[Category:Oolite scripting]] [[Category: Oolite]]

Revision as of 23:16, 20 August 2007

Prototype: Object
Subtypes: none

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 : Number (read/write)

The x co-ordinate of the vector.

y

y : Number (read/write)

The y co-ordinate of the vector.

z

z : Number (read/write)

The z co-ordinate of the vector.

Methods

Constructor

new Vector([value : vectorExpression]) : Vector

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

add

function add(v : vectorExpression) : Vector

Returns the vector sum of the target and v.

See Also: subtract()

angleTo

function angleTo(v : vectorExpression) : Number

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

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

cross

function cross(v : vectorExpression) : Vector

Returns the cross product of the target and vectorExpression.

See Also: dot()

direction

function direction() : Vector

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

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

See Also: magnitude()

distanceTo

function distanceTo(v : vectorExpression) : Number

Returns the distance between the target and v.

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

See Also: squaredDistanceTo()

dot

function dot(v : vectorExpression) : Number

Returns the dot product of the target and v.

See Also: cross()

magnitude

function magnitude() : Number

Returns the magnitude (or length) of the vector.

See Also: squaredMagnitude(), direction()

multiply

function multiply(f : Number) : Vector

Returns the product of the target and f. This has the effect of scaling the vector by the factor f.

rotateBy

function rotateBy(q : quaternionExpression) : Vector

Apply the rotation specified by q to the target.

rotationTo

function rotationTo(v : vectorExpression [, maxArc : Number]) : Quaternion

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

subtract

function subtract(v : vectorExpression) : Vector

Returns the vector difference between the target and v.

See Also: add()

squaredDistanceTo

function squaredDistanceTo(v: vectorExpression) : Number

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

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

squaredMagnitude

function squaredMagnitude() : Number

Returns the square of the magnitude of the vector.

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

tripleProduct

function tripleProduct(v : vectorExpression, w : vectorExpression) : Number

Returns the triple product of the target, v and w.

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