-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib_navball.ks
54 lines (41 loc) · 1.13 KB
/
lib_navball.ks
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// A library of functions to calculate navball-based directions:
// This file is distributed under the terms of the MIT license, (c) the KSLib team
@lazyglobal off.
function east_for {
parameter ves.
return vcrs(ves:up:vector, ves:north:vector).
}
function compass_for {
parameter ves.
local pointing is ves:facing:forevector.
local east is east_for(ves).
local trig_x is vdot(ves:north:vector, pointing).
local trig_y is vdot(east, pointing).
local result is arctan2(trig_y, trig_x).
if result < 0 {
return 360 + result.
} else {
return result.
}
}
function pitch_for {
parameter ves.
return 90 - vang(ves:up:vector, ves:facing:forevector).
}
function roll_for {
parameter ves.
if vang(ship:facing:vector,ship:up:vector) < 0.2 { //this is the dead zone for roll when the ship is vertical
return 0.
} else {
local raw is vang(vxcl(ship:facing:vector,ship:up:vector), ves:facing:starvector).
if vang(ves:up:vector, ves:facing:topvector) > 90 {
if raw > 90 {
return 270 - raw.
} else {
return -90 - raw.
}
} else {
return raw - 90.
}
}
}.