Difference between revisions of "Oolite JavaScript Reference: Vector3D"

From Elite Wiki
m (Added rotateBy().)
m (Methods: replaced "function" (x2), added code tags)
 
(42 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{Oolite-future-scripting}}
+
<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>Vector3D</code>''' class represents 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.
  
 
=== Vector Expressions ===
 
=== Vector Expressions ===
All Oolite-provided functions which take a vector as an argument may instead be passed an [[Oolite/Development/Scripting/Class/Entity|Entity]] instead, in which case the entity’s <code>[[Oolite/Development/Scripting/Class/Entity#position|position]]</code> is used. In specifications, this is represented by arguments named <code>vectorOrEntity</code>.
+
All Oolite-provided functions which take a vector as an argument may instead be passed an array of three numbers, or an [[Oolite JavaScript Reference: Entity|Entity]] (in which case the entity’s <code>[[Oolite JavaScript Reference: Entity#position|position]]</code> is used). In specifications, this is represented by arguments typed <code>vectorExpression</code>. For example, if <code>a</code> and <code>b</code> are vectors whose values are (0, 1, 0) and (1, 0, 0) respectively, the following are equivalent:
 
 
Additionally, most <code>Vector</code> methods may be passed three numbers instead of a vector. In specifications, this is represented by arguments named <code>vectorExpression</code>. For example, if <code>a</code> and <code>b</code> are vectors whose values are (0, 1, 0) and (1, 0, 0) respectively, the following are equivalent:
 
  
 
  var c = a.add(b);
 
  var c = a.add(b);
  var d = a.add(1, 0, 0);
+
  var d = a.add(Vector3D(1, 0, 0));
  // c and d are now both (1, 1, 0).
+
var e = a.add([1, 0, 0]);
 +
  // c, d and e are now all (1, 1, 0).
  
 
== 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 Vector3D'''([value : [[#Vector Expressions|vectorExpression]]]) : Vector3D
 
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]]) : Vector3D
Returns the vector sum of the target and <code>[[#Vector Expressions|vectorExpression]]</code>.
+
Returns the vector sum of the target and <code>v</code>.
 +
 
 +
'''See Also:''' <code>[[#subtract|subtract]]()</code>
 +
 
 +
=== <code>angleTo</code> ===
 +
function '''angleTo'''(v : [[#Vector Expressions|vectorExpression]]) : Number
 +
Returns the angle (in radians) between the target and <code>[[#Vector Expressions|vectorExpression]]</code>. This is always a positive value between 0 and π.
 +
 
 +
<code>v.angleTo(u)</code> is equivalent to <code>Math.[https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Math/acos acos](v.[[#direction|direction]]().[[#dot|dot]](u.[[#direction|direction]]()))</code>.
 +
 
 +
=== <code>cross</code> ===
 +
function '''cross'''(v : [[#Vector Expressions|vectorExpression]]) : Vector3D
 +
Returns the [http://en.wikipedia.org/wiki/Vector_%28spatial%29#Cross_product cross product] of the target and <code>[[#Vector Expressions|vectorExpression]]</code>.
  
=== <code>subtract</code> ===
+
'''See Also:''' <code>[[#dot|dot]]()</code>
  Vector subtract([[#Vector Expressions|vectorExpression]]);
+
 
Returns the vector difference between the target and <code>[[#Vector Expressions|vectorExpression]]</code>.
+
=== <code>direction</code> ===
 +
  function '''direction'''() : Vector3D
 +
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>.
 +
 
 +
'''See Also:''' <code>[[#magnitude|magnitude]]()</code>
  
 
=== <code>distanceTo</code> ===
 
=== <code>distanceTo</code> ===
  double distanceTo([[#Vector Expressions|vectorExpression]]);
+
  function '''distanceTo'''(v : [[#Vector Expressions|vectorExpression]]) : Number
Returns the distance between the target and <code>[[#Vector Expressions|vectorExpression]]</code>.
+
Returns the distance between the source vector and <code>v</code>. If <code>v</code> is not a vector, then if it is an [[Oolite JavaScript Reference: Entity|Entity]] its position will be used; and if it is an array of three values (for example, <code>[x, y, z]</code> or <code>[1, 2, 3]</code>), it will be treated as a vector with those coordinates.
  
<code>a.distanceTo(b)</code> is equivalent to <code>a.[[#subtract|subtract]](b).[[#magnitude|magnitude]]()</code>.
+
<code>u.distanceTo(v)</code> is equivalent to <code>u.[[#subtract|subtract]](v).[[#magnitude|magnitude]]()</code>.
  
=== <code>squaredDistanceTo</code> ===
+
'''CAUTION:''' If passed an invalid <code>v</code>, the method will return zero, not log an error.  It is also incorrect to call <code>distanceTo</code> on something that is not a vector. For example: <code>this.ship.distanceTo(v)</code> is missing <code>position</code> and needs to be written as <code>this.ship.position.distanceTo(v)</code>.
  double squaredDistanceTo([[#Vector Expressions|vectorExpression]]);
 
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>.
+
'''See Also:''' <code>[[#squaredDistanceTo|squaredDistanceTo]]()</code>
 
 
=== <code>multiply</code> ===
 
Vector multiply(number);
 
Returns the product of the target and <code>[[#Vector Expressions|vectorExpression]]</code>.
 
  
 
=== <code>dot</code> ===
 
=== <code>dot</code> ===
  double dot([[#Vector Expressions|vectorExpression]]);
+
  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>[[#Vector Expressions|vectorExpression]]</code>.
+
Returns the [http://en.wikipedia.org/wiki/Vector_%28spatial%29#Dot_product dot product] of the target and <code>v</code>.
  
=== <code>angleTo</code> ===
+
The dot product of two vectors says something how well they are aligned to each other. The dot product of two identical vectors is 1 while two vectors pointing in opposite direction result in a -1 result. Also you can't reliably compare two vectors with each other to see if they are identical. To check if <code>vector1 == vector2</code> you need the dot product :
  double angleTo([[#Vector Expressions|vectorExpression]]);
+
  vector1.dot(vector2) > 0.999
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>.
+
'''See Also:''' <code>[[#cross|cross]]()</code>
  
=== <code>cross</code> ===
+
=== <code>fromCoordinateSystem</code> ===
  double cross([[#Vector Expressions|vectorExpression]]);
+
  function '''fromCoordinateSystem'''(system : String) : Vector3D
Returns the [http://en.wikipedia.org/wiki/Vector_%28spatial%29#Cross_product cross product] of the target and <code>[[#Vector Expressions|vectorExpression]]</code>.
+
Convert a vector from an [[Oolite coordinate systems|abstract coordinate system]] to absolute coordinates. <code>system</code> must be a three-letter string specifying an abstract coordinate system.
 
 
=== <code>tripleProduct</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>a.tripleProduct(b, c)</code> is equivalent to <code>a.[[#dot|dot]](b.[[#cross|cross]](c))</code>.
+
'''Important:'''
 +
Vectors do not “know” which coordinate system they’re in. The script must keep track of that. If you add together vectors in different coordinate systems the result will be nonsense, just as if you added measurements in different units without appropriate conversions.
  
=== <code>direction</code> ===
+
'''Example:'''
  Vector direction();
+
  this.halfway = Vector3D(0, 0, 0.5).fromCoordinateSystem("wpu");
Returns the [http://en.wikipedia.org/wiki/Unit_vector unit vector] with the same direction as the target.
+
// Equivalent: Vector3D.interpolate([0, 0, 0], S.mainPlanet, 0.5)
  
<code>a.direction()</code> is equivalent to <code>a * (1 / a.[[#magnitude|magnitude]]())</code>.
+
'''See Also:''' <code>[[#toCoordinateSystem|toCoordinateSystem]]()</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) : Vector3D
 +
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]]) : Vector3D
 +
Apply the rotation specified by <code>q</code> to the target vector.
 +
 +
=== <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 vector 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.
 +
 +
Both the target and <code>v</code> must be normalized. The vectors must not be antiparallel (180° apart), since the axis of rotation is undefined in this case.
 +
 +
'''Example:'''
 +
var orientation = myVector.rotationTo([0, 0, 1])
 +
This will generate a quaternion were the <code>forwardVector</code> is pointing in the same direction as <code>myVector</code>. (<code>[0, 0, 1]</code> is equal to the <code>forwardVector</code> of the identity quaternion.)
 +
 +
=== <code>squaredDistanceTo</code> ===
 +
function '''squaredDistanceTo'''(v: [[#Vector Expressions|vectorExpression]]) : Number
 +
Returns the square of the distance between the source vector and vector <code>v</code>. If <code>v</code> is not a vector, then if it is an [[Oolite JavaScript Reference: Entity|Entity]] its position will be used; and if it is an array of three values (for example, <code>[x, y, z]</code> or <code>[1, 2, 3]</code>), it will be treated as a vector with those coordinates.
 +
 +
<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>.
 +
 +
'''CAUTION:''' If passed an invalid <code>v</code>, the method will return zero, not log an error.  It is also incorrect to call <code>squaredDistanceTo</code> on something that is not a vector.  For example: <code>this.ship.squaredDistanceTo(v)</code> is missing <code>position</code> and needs to be written as <code>this.ship.position.squaredDistanceTo(v)</code>.
 +
 +
'''See Also:''' <code>[[#distanceTo|distanceTo]]()</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>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>subtract</code> ===
 +
function '''subtract'''(v : [[#Vector Expressions|vectorExpression]]) : Vector3D
 +
Returns the vector difference between the target and <code>v</code>.
 +
 
 +
'''See Also:''' <code>[[#add|add]]()</code>
  
=== <code>rotateBy</code> ===
+
=== <code>toArray</code> ===
  Vector rotateBy([[Oolite/Development/Scripting/Class/Quaternion#Quaternion Expressions|quaternionExpression]]);
+
  function '''toArray'''() : Array
Apply the rotation specified by <code>[[Oolite/Development/Scripting/Class/Quaternion#Quaternion Expressions|quaternionExpression]]</code> to the target.
+
Returns an array of the vector’s components, in the order <code>[x, y, z]</code>. <code>v.toArray()</code> is equivalent to <code>[v.x, v.y, v.z]</code>.
 +
 
 +
=== <code>toCoordinateSystem</code> ===
 +
function '''toCoordinateSystem'''(system : String) : Vector3D
 +
Convert a vector from absolute coordinates to an [[Oolite coordinate systems|abstract coordinate system]]. <code>system</code> must be a three-letter string specifying an abstract coordinate system. The target of the method must be a vector in absolute coordinates. (To convert a vector from one abstract coordinate system to another, you must first call code>[[#fromCoordinateSystem|fromCoordinateSystem]]()</code>, then <code>toCoordinateSystem()</code>.)
 +
 
 +
'''Important:'''
 +
Vectors do not “know” which coordinate system they’re in. The script must keep track of that. If you add together vectors in different coordinate systems the result will be nonsense, just as if you added measurements in different units without appropriate conversions.
 +
 
 +
'''Example:'''
 +
Vector3D(0, 0, 226380).toCoordinateSystem("wpu")
 +
// In Lave system, this returns (0, 0, 0.5).
 +
 
 +
'''See Also:''' <code>[[#fromCoordinateSystem|fromCoordinateSystem]]()</code>
 +
 
 +
=== <code>tripleProduct</code> ===
 +
function '''tripleProduct'''(v : [[#Vector Expressions|vectorExpression]], w : [[#Vector Expressions|vectorExpression]]) : Number
 +
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>u.tripleProduct(v, w)</code> is equivalent to <code>u.[[#dot|dot]](v.[[#cross|cross]](w))</code>.
 +
 
 +
== Static methods ==
 +
 
 +
=== <code>interpolate</code> ===
 +
function '''interpolate'''(u : [[#Vector Expressions|vectorExpression]], v : [[#Vector Expressions|vectorExpression]], where : Number) : Vector3D
 +
Returns a point on the line between <code>u</code> and <code>v</code>. If <code>where</code> is 0, the result is <code>u</code>.  If <code>where</code> is 1, the result is <code>v</code>. If <code>where</code> is 0.5, the result is half way between <code>u</code> and <code>v</code>. Values of <code>where</code> outside the range [0, 1] are valid; for instance, <code>Vector3D.interpolate(u, v, -1)</code> returns a point as far from <code>u</code> as <code>v</code> is, but in the opposite direction.
 +
 
 +
<code>Vector3D.interpolate(u, v, where)</code> is equivalent to <code>u.[[#add|add]](v.[[#subtract|subtract]](u).[[#multiply|multiply]](where))</code>, or <code>u.[[#multiply|multiply]](1 - where).[[#add|add]](v. [[#multiply|multiply]](where))</code>.
 +
 
 +
=== <code>random</code> ===
 +
function '''random'''([maxLength : Number]) : Vector3D
 +
Returns a vector of random length up to <code>maxLength</code>, in a random direction. If <code>maxLength</code> is not specified (or not a number), 1.0 is used. These vectors are uniformly distributed within the unit sphere, which has the effect that longer vectors are more common than shorter ones. Use <code>Vector3D.[[#randomDirectionAndLength|randomDirectionAndLength]]()</code> if an even length distribution is desired.
 +
 
 +
In the following image, the cloud on the left was made with the 2D equivalent of <code>random()</code>, and the image on the right was made with the 2D equivalent of <code>randomDirectionAndLength()</code>.<br />[[Image:Randomvectordistribution.png]]
 +
 
 +
'''See Also:''' <code>[[#randomDirection|randomDirection]]()</code>, <code>[[#randomDirectionAndLength|randomDirectionAndLength]]()</code>
 +
 
 +
=== <code>randomDirection</code> ===
 +
function '''randomDirection'''([scale : Number]) : Vector3D
 +
Returns a vector of length <code>scale</code>, in a random direction. If <code>scale</code> is not specified (or not a number), 1.0 is used.
 +
 
 +
'''See Also:''' <code>[[#random|random]]()</code>, <code>[[#randomDirectionAndLength|randomDirectionAndLength]]()</code>
  
=== <code>rotationTo</code> ===
+
=== <code>randomDirectionAndLength</code> ===
  [[Oolite/Development/Scripting/Class/Quaternion|Quaternion]] rotationTo([[#Vector Expressions|vectorExpression]] [, maxArc]);
+
  function '''randomDirectionAndLength'''([maxLength : Number]) : Vector3D
Returns a [[Oolite/Development/Scripting/Class/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.
+
Returns a vector of random length up to <code>maxLength</code>, in a random direction. If <code>maxLength</code> is not specified (or not a number), 1.0 is used. These vectors have a uniform distribution of magnitude (all lengths are equally likely), but cluster towards the origin. Use <code>Vector3D.[[#random|random]]()</code> if an even spacial distribution is desired.
  
 +
'''See Also:''' <code>[[#random|random]]()</code>, <code>[[#randomDirection|randomDirection]]()</code>
  
[[Category:Oolite scripting]] [[Category:Oolite Development]]
+
[[Category:Oolite JavaScript Reference]]

Latest revision as of 19:49, 28 October 2022

Prototype: Object
Subtypes: none

The Vector3D class represents 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 array of three numbers, or an Entity (in which case the entity’s position is used). In specifications, this is represented by arguments typed 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(Vector3D(1, 0, 0));
var e = a.add([1, 0, 0]);
// c, d and e are now all (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 Vector3D([value : vectorExpression]) : Vector3D

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) : Vector3D

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. This is always a positive value between 0 and π.

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

cross

function cross(v : vectorExpression) : Vector3D

Returns the cross product of the target and vectorExpression.

See Also: dot()

direction

function direction() : Vector3D

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 source vector and v. If v is not a vector, then if it is an Entity its position will be used; and if it is an array of three values (for example, [x, y, z] or [1, 2, 3]), it will be treated as a vector with those coordinates.

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

CAUTION: If passed an invalid v, the method will return zero, not log an error. It is also incorrect to call distanceTo on something that is not a vector. For example: this.ship.distanceTo(v) is missing position and needs to be written as this.ship.position.distanceTo(v).

See Also: squaredDistanceTo()

dot

function dot(v : vectorExpression) : Number

Returns the dot product of the target and v.

The dot product of two vectors says something how well they are aligned to each other. The dot product of two identical vectors is 1 while two vectors pointing in opposite direction result in a -1 result. Also you can't reliably compare two vectors with each other to see if they are identical. To check if vector1 == vector2 you need the dot product :

vector1.dot(vector2) > 0.999

See Also: cross()

fromCoordinateSystem

function fromCoordinateSystem(system : String) : Vector3D

Convert a vector from an abstract coordinate system to absolute coordinates. system must be a three-letter string specifying an abstract coordinate system.

Important: Vectors do not “know” which coordinate system they’re in. The script must keep track of that. If you add together vectors in different coordinate systems the result will be nonsense, just as if you added measurements in different units without appropriate conversions.

Example:

this.halfway = Vector3D(0, 0, 0.5).fromCoordinateSystem("wpu");
// Equivalent: Vector3D.interpolate([0, 0, 0], S.mainPlanet, 0.5)

See Also: toCoordinateSystem()

magnitude

function magnitude() : Number

Returns the magnitude (or length) of the vector.

See Also: squaredMagnitude(), direction()

multiply

function multiply(f : Number) : Vector3D

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) : Vector3D

Apply the rotation specified by q to the target vector.

rotationTo

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

Returns a quaternion corresponding to a rotation from the target vector 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.

Both the target and v must be normalized. The vectors must not be antiparallel (180° apart), since the axis of rotation is undefined in this case.

Example:

var orientation = myVector.rotationTo([0, 0, 1])

This will generate a quaternion were the forwardVector is pointing in the same direction as myVector. ([0, 0, 1] is equal to the forwardVector of the identity quaternion.)

squaredDistanceTo

function squaredDistanceTo(v: vectorExpression) : Number

Returns the square of the distance between the source vector and vector v. If v is not a vector, then if it is an Entity its position will be used; and if it is an array of three values (for example, [x, y, z] or [1, 2, 3]), it will be treated as a vector with those coordinates.

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

CAUTION: If passed an invalid v, the method will return zero, not log an error. It is also incorrect to call squaredDistanceTo on something that is not a vector. For example: this.ship.squaredDistanceTo(v) is missing position and needs to be written as this.ship.position.squaredDistanceTo(v).

See Also: distanceTo()

squaredMagnitude

function squaredMagnitude() : Number

Returns the square of the magnitude of the vector.

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

subtract

function subtract(v : vectorExpression) : Vector3D

Returns the vector difference between the target and v.

See Also: add()

toArray

function toArray() : Array

Returns an array of the vector’s components, in the order [x, y, z]. v.toArray() is equivalent to [v.x, v.y, v.z].

toCoordinateSystem

function toCoordinateSystem(system : String) : Vector3D

Convert a vector from absolute coordinates to an abstract coordinate system. system must be a three-letter string specifying an abstract coordinate system. The target of the method must be a vector in absolute coordinates. (To convert a vector from one abstract coordinate system to another, you must first call code>fromCoordinateSystem(), then toCoordinateSystem().)

Important: Vectors do not “know” which coordinate system they’re in. The script must keep track of that. If you add together vectors in different coordinate systems the result will be nonsense, just as if you added measurements in different units without appropriate conversions.

Example:

Vector3D(0, 0, 226380).toCoordinateSystem("wpu")
// In Lave system, this returns (0, 0, 0.5).

See Also: fromCoordinateSystem()

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)).

Static methods

interpolate

function interpolate(u : vectorExpression, v : vectorExpression, where : Number) : Vector3D

Returns a point on the line between u and v. If where is 0, the result is u. If where is 1, the result is v. If where is 0.5, the result is half way between u and v. Values of where outside the range [0, 1] are valid; for instance, Vector3D.interpolate(u, v, -1) returns a point as far from u as v is, but in the opposite direction.

Vector3D.interpolate(u, v, where) is equivalent to u.add(v.subtract(u).multiply(where)), or u.multiply(1 - where).add(v. multiply(where)).

random

function random([maxLength : Number]) : Vector3D

Returns a vector of random length up to maxLength, in a random direction. If maxLength is not specified (or not a number), 1.0 is used. These vectors are uniformly distributed within the unit sphere, which has the effect that longer vectors are more common than shorter ones. Use Vector3D.randomDirectionAndLength() if an even length distribution is desired.

In the following image, the cloud on the left was made with the 2D equivalent of random(), and the image on the right was made with the 2D equivalent of randomDirectionAndLength().
Randomvectordistribution.png

See Also: randomDirection(), randomDirectionAndLength()

randomDirection

function randomDirection([scale : Number]) : Vector3D

Returns a vector of length scale, in a random direction. If scale is not specified (or not a number), 1.0 is used.

See Also: random(), randomDirectionAndLength()

randomDirectionAndLength

function randomDirectionAndLength([maxLength : Number]) : Vector3D

Returns a vector of random length up to maxLength, in a random direction. If maxLength is not specified (or not a number), 1.0 is used. These vectors have a uniform distribution of magnitude (all lengths are equally likely), but cluster towards the origin. Use Vector3D.random() if an even spacial distribution is desired.

See Also: random(), randomDirection()