## An example to show how to convert from Euler rotations to Unit vectors Edit

Some people have an easier time dealing with unit vectors in the XYZ coordinates and using vector addition to achieve tip-to-tail vector manipulation to make their deflections and steering commands.

Therefore it may be useful to obtain any direction such as Up, North, or R(50,10,0) in terms of it being a Unit Vector pointing in that direction in the XYZ coordinate system.

**caveat:** This information was obtained by guesswork and experimentation with the Kosscript console. It might be wrong.

Looking at a page from the documentation for Unity script, which is presumably based on the same 3D system and Unity for C#, the 3D engine underneath Kerbal Space Program, it seems that rotations expressed in Unity's own Euler rotation nomenclature are in the following order:

Rotate around Z. THEN Rotate around X. THEN Rotate around Y.

This information helps give the final data needed to understand how to perform the rotational math once and for all to do this.

The following example program will take any rotation expressed in its R(y,p,r) form (which includes built-in things like PROGRADE, UP, and NORTH) and convert it into a unit vector pointing in that direction. Using this it is possible to perform vector operations like

set aimAboveTarget to target:direction + (0.2)*upwardUnitvector.

run tfDirToUnitV( TARGET:DIRECTION ). set targetUnitVec to tfUnit. run tfDirToUnitV( UP ). set upFifthUnitVec to 0.2 * tfUnit. // unit vector tfUnit times scalar 0.2 set aimAboveTarget to targetUnitVec + upFifthUnitVec. lock steering to R(0,0,0) + aimAboveTarget.

(NOTE: The expression 0.2 * tfUnit won't work in versions of kOS earlier than 0.8.5 . Multiplying a vector by a scalar wasn't implemented in older versions. In older versions you had to explode the vector into its X,Y,Z components and multiply them separately by the scalar and then rebuild the vector from that.)

## File: call it Note: This file is publicly editable and might contain malicious code.

Please exercise caution when using.tfDirToUnitV.txt ^{view} Edit

// Given a Direction tuple like any of the examples
// below:
// Up. North. R(100,32,0), Q(1,0,0,30) HEADING 30 by 10
//
// Calculate what that direction would be if expressed
// as a unit vector instead.
//
// This is accomplished by performing the matrix rotation
// on a vector that begins as V(0,0,1) and seeing what
// the result is.
//
//
// Because you can't pass things out of a program,
// global variables must be used here for the output.
// INPUT: 1 parameter: the Direction vector.
// OUTPUT: tfUnit, a Vector containing the xyz coords of
// the output.
//
// All "local" variables begin with "tf" to help
// prevent them from clashing with the other
// variables you might have used in the global
// namespace of KOS.
declare parameter tfDir.
// Rotation angles for rotation matrix:
set tfA to tfDir:yaw.
set tfCosA to cos(tfA).
set tfSinA to sin(tfA).
set tfB to tfDir:pitch.
set tfCosB to cos(tfB).
set tfSinB to sin(tfB).
set tfC to tfDir:roll.
set tfCosC to cos(tfC).
set tfSinC to sin(tfC).
set tf11 to tfCosA*tfCosC + tfSinA*tfSinB*tfSinC .
set tf21 to tfCosC*tfSinA*tfSinB - tfCosA*tfSinC .
set tf31 to tfCosB*tfSinA .
set tf12 to tfCosB*tfSinC .
set tf22 to tfCosB*tfCosC .
set tf32 to 0-tfSinB .
set tf13 to tfCosA*tfSinB*tfSinC - tfCosC*tfSinA .
set tf23 to tfSinA*tfSinC + tfCosA*tfCosC*tfSinB .
set tf33 to tfCosA*tfCosB .
set tfV to V( 0,0,1 ).
set tfUnit to V(
tf11*tfV:x + tf21*tfV:y + tf31*tfV:z ,
tf12*tfV:x + tf22*tfV:y + tf32*tfV:z ,
tf13*tfV:x + tf23*tfV:y + tf33*tfV:z ) .