Vectors¶
Contents
Creation¶
- V(x,y,z)¶
- Parameters
x – (scalar) \(x\) coordinate
y – (scalar) \(y\) coordinate
z – (scalar) \(z\) coordinate
- Returns
This creates a new vector from 3 components in \((x,y,z)\):
SET vec TO V(x,y,z).
Here, a new
Vector
calledvec
is created . The objectVector
represents a three-dimensional euclidean vector To deeply understand most vectors in kOS, you have to understand a bit about the underlying coordinate system of KSP. If you are having trouble making sense of the direction the axes point in, go read that page.
Note
Remember that the XYZ grid in Kerbal Space Program uses a left-handed coordinate system.
Structure¶
- structure Vector¶
¶ Suffix
Type
Get
Set
yes
yes
yes
yes
yes
yes
yes
yes
yes
no
yes
no
yes
yes
yes
no
Note
This type is serializable.
- Vector:MAG¶
- Type
- Access
Get/Set
The magnitude of the vector, as a scalar number, by the Pythagorean Theorem.
- Vector:NORMALIZED¶
- Type
- Access
Get only
This creates a unit vector pointing in the same direction as this vector. This is the same effect as multiplying the vector by the scalar
1 / vec:MAG
.
- Vector:SQRMAGNITUDE¶
- Type
- Access
Get only
The magnitude of the vector, squared. Use instead of
vec:MAG^2
if you need to square of the magnitude as this skips the step in the Pythagorean formula where you take the square root in the first place. Taking the square root and then squaring that would introduce floating point error needlessly.
- Vector:DIRECTION¶
- Type
- Access
Get/Set
- GET:
The vector rendered into a Direction (see note in the Directions documentation about information loss when doing this).
- SET:
Tells the vector to keep its magnitude as it is but point in a new direction, adjusting its \((x,y,z)\) numbers accordingly.
Operations and Methods¶
Method / Operator |
Return Type |
---|---|
|
|
- *
Scalar multiplication or dot product of two
Vectors
. See alsoVECTORDOTPRODUCT
:SET a TO 2. SET vec1 TO V(1,2,3). SET vec2 TO V(2,3,4). PRINT a * vec1. // prints: V(2,4,6) PRINT vec1 * vec2. // prints: 20
Note that the unary minus operator is really a multiplication of the vector by a scalar of (-1):
PRINT -vec1. // these two both print the PRINT (-1)*vec1. // exact same thing.
- +, -
Adding and subtracting a
Vector
with anotherVector
:SET a TO 2. SET vec1 TO V(1,2,3). SET vec2 TO V(2,3,4). PRINT vec1 + vec2. // prints: V(3,5,7) PRINT vec2 - vec1. // prints: V(1,1,1)
Note that the unary minus operator is the same thing as multiplying the vector by a scalar of (-1), and is not technically an addition or subtraction operator:
// These two both print the same exact thing: PRINT -vec1. PRINT (-1)*vec1.
- VDOT(v1,v2)¶
Same as
VECTORDOTPRODUCT(v1,v2)
and v1 * v2.
- VECTORDOTPRODUCT(v1,v2)¶
- Parameters
- Returns
- Return type
This is the dot product of two vectors returning a scalar number. This is the same as v1 * v2:
SET vec1 TO V(1,2,3). SET vec2 TO V(2,3,4). // These are different ways to perform the same operation. // All of them will print the value: 20 // ------------------------------------------------------- PRINT VDOT(vec1, vec2). PRINT VECTORDOTPRODUCT(vec1, vec2). PRINT vec1 * vec2. // multiplication of two vectors with asterisk "*" performs a VDOT().
- VCRS(v1,v2)¶
Same as
VECTORCROSSPRODUCT(v1,v2)
- VECTORCROSSPRODUCT(v1,v2)¶
- Parameters
- Returns
- Return type
The vector cross product of two vectors in the order
(v1,v2)
returning a new Vector:SET vec1 TO V(1,2,3). SET vec2 TO V(2,3,4). // These will both print: V(-1,2,-1) PRINT VCRS(vec1, vec2). PRINT VECTORCROSSPRODUCT(vec1, vec2).
When visualizing the direction that a vector cross product will point, remember that KSP is using a left-handed coordinate system, and this means a cross-product of two vectors will point in the opposite direction of what it would had KSP been using a right-handed coordinate system.
- VANG(v1,v2)¶
Same as
VECTORANGLE(v1,v2)
.
- VECTORANGLE(v1,v2)¶
-
This returns the angle between v1 and v2. It is the same result as:
\[\arccos\left( \frac{ \vec{v_1}\cdot\vec{v_2} }{ \left|\vec{v_1}\right|\cdot\left|\vec{v_2}\right| } \right)\]or in KerboScript:
arccos( (VDOT(v1,v2) / (v1:MAG * v2:MAG) ) )
- VXCL(v1,v2)¶
Same as
VECTOREXCLUDE(v1,v2)
- VECTOREXCLUDE(v1,v2)¶
This is a vector,
v2
with all ofv1
excluded from it. In other words, the projection ofv2
onto the plane that is normal tov1
.
Some examples of using the Vector
object:
// initializes a vector with x=100, y=5, z=0
SET varname TO V(100,5,0).
varname:X. // Returns 100.
V(100,5,0):Y. // Returns 5.
V(100,5,0):Z. // Returns 0.
// Returns the magnitude of the vector
varname:MAG.
// Changes x coordinate value to 111.
SET varname:X TO 111.
// Lengthen or shorten vector to make its magnitude 10.
SET varname:MAG to 10.
// get vector pointing opposite to surface velocity.
SET retroSurf to (-1)*velocity:surface.
// use cross product to find normal to the orbit plane.
SET norm to VCRS(velocity:orbit, ship:body:position).