-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththrottle_balance.ks
More file actions
47 lines (40 loc) · 1.16 KB
/
throttle_balance.ks
File metadata and controls
47 lines (40 loc) · 1.16 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
function throttle_balance
{
list engines in engs.
local total_torque is V(0, 0, 0).
local torques is list().
for eng in engs
{
torques:add(0).
}
local i is 0.
until i = engs:length
{
set thrust to engs[i]:facing:forevector * engs[i]:maxthrust * engs[i]:thrustlimit / 100.
set torques[i] to VCRS(thrust, engs[i]:position).
set i to i + 1.
}
set i to 0.
until (i = torques:length)
{
set total_torque to total_torque + torques[i].
set i to i + 1.
}
// get engine with lowest angle to current torque, and reduce it's throttle
set lowestAngle to 360.
set lowestEngine to 0.
set i to 0.
until i = engs:length
{
set angle to arccos(total_torque * torques[i] / (total_torque:MAG * torques[i]:MAG)).
if angle < lowestAngle
{
set lowestAngle to angle.
set lowestEngine to i.
}
set i to i+1.
}
set factor to 1 - (total_torque:mag / (torques[lowestEngine]:mag * cos(lowestAngle))).
set engs[lowestEngine]:thrustlimit to engs[lowestEngine]:thrustlimit * factor.
return.
}