Vectors and Rotations#
Positions, velocities and directions are vectors, and attitudes are rotations. Both are tuples of numbers, so they work the same way in every client and every service. Each one is given in a reference frame, which sets its origin and axes.
Vectors#
A vector is a 3-tuple (x, y, z). This example reads the prograde direction of the active
vessel:
import krpc
conn = krpc.connect()
v = conn.space_center.active_vessel.flight().prograde
print("(%.2f, %.2f, %.2f)" % v)
using System;
using KRPC.Client;
using KRPC.Client.Services.SpaceCenter;
class Vector3 {
public static void Main () {
using (var connection = new Connection ()) {
var vessel = connection.SpaceCenter ().ActiveVessel;
Tuple<double,double,double> v = vessel.Flight ().Prograde;
Console.WriteLine ("({0:F2}, {1:F2}, {2:F2})",
v.Item1, v.Item2, v.Item3);
}
}
}
#include <iomanip>
#include <iostream>
#include <krpc.hpp>
#include <krpc/services/space_center.hpp>
#include <tuple>
int main() {
krpc::Connection conn = krpc::connect();
krpc::services::SpaceCenter sc(conn);
std::tuple<double, double, double> v =
sc.active_vessel().value().flight().prograde();
std::cout << std::fixed << std::setprecision(2) << "(" << std::get<0>(v)
<< ", " << std::get<1>(v) << ", " << std::get<2>(v) << ")"
<< std::endl;
}
#include <krpc_cnano.h>
#include <krpc_cnano/services/space_center.h>
#include <stdio.h>
int main() {
krpc_connection_t conn;
if (krpc_open_serial(&conn, "COM0") != KRPC_OK) return 1;
if (krpc_connect(conn, "Vector3") != KRPC_OK) return 1;
krpc_SpaceCenter_Vessel_t vessel;
krpc_SpaceCenter_ActiveVessel(conn, &vessel);
krpc_SpaceCenter_Flight_t flight;
krpc_SpaceCenter_Vessel_Flight(conn, &flight, vessel, KRPC_NULL);
krpc_tuple_double_double_double_t v;
krpc_SpaceCenter_Flight_Prograde(conn, &v, flight);
printf("(%.2f, %.2f, %.2f)\n", v.e0, v.e1, v.e2);
krpc_disconnect(conn);
}
import krpc.client.Connection;
import krpc.client.services.SpaceCenter;
import krpc.client.services.SpaceCenter.Vessel;
import org.javatuples.Triplet;
public class Vector3 {
public static void main(String[] args) {
try (Connection connection = Connection.newInstance()) {
Vessel vessel =
SpaceCenter.newInstance(connection).getActiveVessel();
Triplet<Double, Double, Double> v = vessel.flight(null).getPrograde();
System.out.printf("(%.2f, %.2f, %.2f)%n",
v.getValue0(), v.getValue1(), v.getValue2());
}
}
}
local krpc = require 'krpc'
local conn = krpc.connect()
local v = conn.space_center.active_vessel:flight().prograde
print(string.format('(%.2f, %.2f, %.2f)', v[1], v[2], v[3]))
import { connect } from "@krpc-org/krpc";
const conn = await connect();
const vessel = await conn.spaceCenter.getActiveVessel();
const v = await (await vessel.flight()).getPrograde();
console.log(`(${v.map((x) => x.toFixed(2)).join(", ")})`);
await conn.close();
A position is in meters and a velocity in meters per second. A direction is a unit vector.
Handedness#
KSP and kRPC use a left-handed coordinate system, as Unity does. Point the thumb of your left hand along the x axis and your index finger along the y axis. Your middle finger then points along the z axis.
The cross product uses the usual formula, so the cross product of the x and y axes is the z axis. The left-hand rule gives the direction of the result.
A positive rotation about an axis turns clockwise, seen from the tip of the axis looking back toward the origin. Curl the fingers of your left hand with your thumb along the axis, and they point in the direction of a positive rotation. So a rotation of +90° about the y axis takes the z axis onto the x axis.
Rotations#
A rotation is a unit quaternion, a 4-tuple (x, y, z, w). The rotation that does nothing is
(0, 0, 0, 1). This example reads the rotation of the active vessel:
import krpc
conn = krpc.connect()
q = conn.space_center.active_vessel.flight().rotation
print("(%.2f, %.2f, %.2f, %.2f)" % q)
using System;
using KRPC.Client;
using KRPC.Client.Services.SpaceCenter;
class Quaternion {
public static void Main () {
using (var connection = new Connection ()) {
var vessel = connection.SpaceCenter ().ActiveVessel;
Tuple<double,double,double,double> q =
vessel.Flight ().Rotation;
Console.WriteLine ("({0:F2}, {1:F2}, {2:F2}, {3:F2})",
q.Item1, q.Item2, q.Item3, q.Item4);
}
}
}
#include <iomanip>
#include <iostream>
#include <krpc.hpp>
#include <krpc/services/space_center.hpp>
#include <tuple>
int main() {
krpc::Connection conn = krpc::connect();
krpc::services::SpaceCenter sc(conn);
std::tuple<double, double, double, double> q =
sc.active_vessel().value().flight().rotation();
std::cout << std::fixed << std::setprecision(2) << "(" << std::get<0>(q)
<< ", " << std::get<1>(q) << ", " << std::get<2>(q) << ", "
<< std::get<3>(q) << ")" << std::endl;
}
#include <krpc_cnano.h>
#include <krpc_cnano/services/space_center.h>
#include <stdio.h>
int main() {
krpc_connection_t conn;
if (krpc_open_serial(&conn, "COM0") != KRPC_OK) return 1;
if (krpc_connect(conn, "Quaternion") != KRPC_OK) return 1;
krpc_SpaceCenter_Vessel_t vessel;
krpc_SpaceCenter_ActiveVessel(conn, &vessel);
krpc_SpaceCenter_Flight_t flight;
krpc_SpaceCenter_Vessel_Flight(conn, &flight, vessel, KRPC_NULL);
krpc_tuple_double_double_double_double_t q;
krpc_SpaceCenter_Flight_Rotation(conn, &q, flight);
printf("(%.2f, %.2f, %.2f, %.2f)\n", q.e0, q.e1, q.e2, q.e3);
krpc_disconnect(conn);
}
import krpc.client.Connection;
import krpc.client.services.SpaceCenter;
import krpc.client.services.SpaceCenter.Vessel;
import org.javatuples.Quartet;
public class Quaternion {
public static void main(String[] args) {
try (Connection connection = Connection.newInstance()) {
Vessel vessel =
SpaceCenter.newInstance(connection).getActiveVessel();
Quartet<Double, Double, Double, Double> q =
vessel.flight(null).getRotation();
System.out.printf("(%.2f, %.2f, %.2f, %.2f)%n", q.getValue0(),
q.getValue1(), q.getValue2(), q.getValue3());
}
}
}
local krpc = require 'krpc'
local conn = krpc.connect()
local q = conn.space_center.active_vessel:flight().rotation
print(string.format('(%.2f, %.2f, %.2f, %.2f)', q[1], q[2], q[3], q[4]))
import { connect } from "@krpc-org/krpc";
const conn = await connect();
const vessel = await conn.spaceCenter.getActiveVessel();
const q = await (await vessel.flight()).getRotation();
console.log(`(${q.map((x) => x.toFixed(2)).join(", ")})`);
await conn.close();
The rotation of an object takes the axes of the reference frame onto the axes of the object. The y
axis of a vessel points out of its nose. So rotating (0, 1, 0) by Vessel.Rotation gives
Vessel.Direction, in the same reference frame.
Angles#
Most angles are in degrees, such as pitch, heading and roll, and latitude and longitude. The
orbital elements of an Orbit, the rotation angles of a CelestialBody and the angles of the
standard library are in radians. The API reference gives the unit of each one.
StdLib.DegreesToRadians and StdLib.RadiansToDegrees convert between them.
Computing on the Server#
The standard library service does vector and quaternion arithmetic, so you do not need a math library in your client. This example finds the angle between the direction of the active vessel and its prograde direction:
direction = vessel.direction(frame)
prograde = vessel.flight(frame).prograde
angle = conn.std_lib.vector_angle(direction, prograde)
print(conn.std_lib.radians_to_degrees(angle))
var direction = vessel.Direction (frame);
var prograde = vessel.Flight (frame).Prograde;
var angle = stdLib.VectorAngle (direction, prograde);
Console.WriteLine (stdLib.RadiansToDegrees (angle));
auto direction = vessel.direction(frame);
auto prograde = vessel.flight(frame).prograde();
auto angle = std_lib.vector_angle(direction, prograde);
std::cout << std_lib.radians_to_degrees(angle) << std::endl;
krpc_tuple_double_double_double_t direction, prograde;
krpc_SpaceCenter_Vessel_Direction(conn, &direction, vessel, frame);
krpc_SpaceCenter_Flight_t flight;
krpc_SpaceCenter_Vessel_Flight(conn, &flight, vessel, frame);
krpc_SpaceCenter_Flight_Prograde(conn, &prograde, flight);
double angle, degrees;
krpc_StdLib_VectorAngle(conn, &angle, &direction, &prograde);
krpc_StdLib_RadiansToDegrees(conn, °rees, angle);
printf("%f\n", degrees);
Triplet<Double, Double, Double> direction = vessel.direction(frame);
Triplet<Double, Double, Double> prograde =
vessel.flight(frame).getPrograde();
double angle = stdLib.vectorAngle(direction, prograde);
System.out.println(stdLib.radiansToDegrees(angle));
local direction = vessel:direction(frame)
local prograde = vessel:flight(frame).prograde
local angle = conn.std_lib.vector_angle(direction, prograde)
print(conn.std_lib.radians_to_degrees(angle))
const direction = await vessel.direction(frame);
const prograde = await (await vessel.flight(frame)).getPrograde();
const angle = await conn.stdLib.vectorAngle(direction, prograde);
console.log(await conn.stdLib.radiansToDegrees(angle));
The vector functions add, subtract, scale, normalize and interpolate vectors, and give their dot and cross products, lengths, distances and angles. The quaternion functions compose, invert and interpolate rotations, rotate vectors, and build rotations from an axis and angle, between two directions, or from a forward and up direction.
Each of these is a remote call. In a server side function, the server evaluates them without a round trip, which makes them the way to work with vectors there.