-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib_navball.ks
More file actions
131 lines (101 loc) · 3.26 KB
/
lib_navball.ks
File metadata and controls
131 lines (101 loc) · 3.26 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
function east_for {
parameter ves is ship.
return vcrs(ves:up:vector, ves:north:vector).
}
function compass_for {
parameter ves is ship,thing is "default".
local pointing is ves:facing:forevector.
if not thing:istype("string") {
set pointing to type_to_vector(ves,thing).
}
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 is ship,thing is "default".
local pointing is ves:facing:forevector.
if not thing:istype("string") {
set pointing to type_to_vector(ves,thing).
}
return 90 - vang(ves:up:vector, pointing).
}
function roll_for {
parameter ves is ship,thing is "default".
local pointing is ves:facing.
if not thing:istype("string") {
if thing:istype("vessel") or pointing:istype("part") {
set pointing to thing:facing.
} else if thing:istype("direction") {
set pointing to thing.
} else {
print "type: " + thing:typename + " is not reconized by roll_for".
}
}
local trig_x is vdot(pointing:topvector,ves:up:vector).
if abs(trig_x) < 0.0035 {//this is the dead zone for roll when within 0.2 degrees of vertical
return 0.
} else {
local vec_y is vcrs(ves:up:vector,ves:facing:forevector).
local trig_y is vdot(pointing:topvector,vec_y).
return arctan2(trig_y,trig_x).
}
}
function compass_and_pitch_for {
parameter ves is ship,thing is "default".
local pointing is ves:facing:forevector.
if not thing:istype("string") {
set pointing to type_to_vector(ves,thing).
}
local east is east_for(ves).
local trig_x is vdot(ves:north:vector, pointing).
local trig_y is vdot(east, pointing).
local trig_z is vdot(ves:up:vector, pointing).
local compass is arctan2(trig_y, trig_x).
if compass < 0 {
set compass to 360 + compass.
}
local pitch is arctan2(trig_z, sqrt(trig_x^2 + trig_y^2)).
return list(compass,pitch).
}
function bearing_between {
parameter ves,thing_1,thing_2.
local vec_1 is type_to_vector(ves,thing_1).
local vec_2 is type_to_vector(ves,thing_2).
local fake_north is vxcl(ves:up:vector, vec_1).
local fake_east is vcrs(ves:up:vector, fake_north).
local trig_x is vdot(fake_north, vec_2).
local trig_y is vdot(fake_east, vec_2).
return arctan2(trig_y, trig_x).
}
function type_to_vector {
parameter ves,thing.
if thing:istype("vector") {
return thing:normalized.
} else if thing:istype("direction") {
return thing:forevector.
} else if thing:istype("vessel") or thing:istype("part") {
return thing:facing:forevector.
} else if thing:istype("geoposition") or thing:istype("waypoint") {
return (thing:position - ves:position):normalized.
} else {
print "type: " + thing:typename + " is not recognized by lib_navball".
}
}
function heading_of_vector
{
// heading_of_vector returns the heading of the vector (number range 0 to 360)
parameter vecT.
local east is vcrs(ship:up:vector, ship:north:vector).
local trig_x is vdot(ship:north:vector, vecT).
local trig_y is vdot(east, vecT).
local result is arctan2(trig_y, trig_x).
if result < 0 {return 360 + result.}
else {return result.}
}