2006-08-22 10:06:46 +00:00
|
|
|
# Dynamic Cockpit View manager. Tries to simulate the pilot's most likely
|
|
|
|
# deliberate view direction. Doesn't consider forced view changes due to
|
|
|
|
# acceleration.
|
2006-08-17 15:59:44 +00:00
|
|
|
|
2006-08-24 20:55:18 +00:00
|
|
|
|
2006-08-17 15:59:44 +00:00
|
|
|
sin = func(a) { math.sin(a * math.pi / 180.0) }
|
|
|
|
cos = func(a) { math.cos(a * math.pi / 180.0) }
|
|
|
|
sigmoid = func(x) { 1 / (1 + math.exp(-x)) }
|
|
|
|
nsigmoid = func(x) { 2 / (1 + math.exp(-x)) - 1 }
|
|
|
|
pow = func(v, w) { math.exp(math.ln(v) * w) }
|
|
|
|
npow = func(v, w) { math.exp(math.ln(abs(v)) * w) * (v < 0 ? -1 : 1) }
|
|
|
|
clamp = func(v, min, max) { v < min ? min : v > max ? max : v }
|
2006-08-19 22:24:24 +00:00
|
|
|
normatan = func(x) { math.atan2(x, 1) * 2 / math.pi }
|
|
|
|
|
2006-11-02 21:06:36 +00:00
|
|
|
normdeg = func(a) {
|
|
|
|
while (a >= 180) {
|
|
|
|
a -= 360;
|
|
|
|
}
|
|
|
|
while (a < -180) {
|
|
|
|
a += 360;
|
|
|
|
}
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
2006-08-19 22:24:24 +00:00
|
|
|
|
|
|
|
|
2006-08-17 15:59:44 +00:00
|
|
|
# Class that reads a property value, applies factor & offset, clamps to min & max,
|
2006-08-22 10:06:46 +00:00
|
|
|
# and optionally lowpass filters.
|
2006-08-17 15:59:44 +00:00
|
|
|
#
|
|
|
|
Input = {
|
2006-08-22 10:06:46 +00:00
|
|
|
new : func(prop = "/null", factor = 1, offset = 0, filter = 0, min = nil, max = nil) {
|
2006-08-17 15:59:44 +00:00
|
|
|
var m = { parents : [Input] };
|
|
|
|
m.prop = isa(props.Node, prop) ? prop : props.globals.getNode(prop, 1);
|
|
|
|
m.factor = factor;
|
|
|
|
m.offset = offset;
|
|
|
|
m.min = min;
|
|
|
|
m.max = max;
|
2006-11-03 17:38:32 +00:00
|
|
|
m.lowpass = filter ? aircraft.lowpass.new(filter) : nil;
|
2006-08-17 15:59:44 +00:00
|
|
|
return m;
|
|
|
|
},
|
|
|
|
get : func {
|
|
|
|
var v = me.prop.getValue() * me.factor + me.offset;
|
|
|
|
if (me.min != nil and v < me.min) {
|
|
|
|
v = me.min;
|
|
|
|
}
|
|
|
|
if (me.max != nil and v > me.max) {
|
|
|
|
v = me.max;
|
|
|
|
}
|
2006-08-22 10:06:46 +00:00
|
|
|
return me.lowpass == nil ? v : me.lowpass.filter(v);
|
2006-08-17 15:59:44 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
set : func(v) {
|
|
|
|
me.prop.setDoubleValue(v);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Class that maintains one sim/current-view/goal-*-offset-deg property.
|
|
|
|
#
|
|
|
|
ViewAxis = {
|
|
|
|
new : func(prop) {
|
|
|
|
var m = { parents : [ViewAxis] };
|
|
|
|
m.prop = props.globals.getNode(prop, 0);
|
|
|
|
m.reset();
|
|
|
|
return m;
|
|
|
|
},
|
|
|
|
reset : func {
|
|
|
|
me.applied_offset = 0;
|
|
|
|
},
|
|
|
|
add_offset : func {
|
|
|
|
me.prop.setValue(me.prop.getValue() + me.applied_offset);
|
|
|
|
},
|
2006-08-18 17:00:16 +00:00
|
|
|
apply : func(v) {
|
|
|
|
var raw = me.prop.getValue() - me.applied_offset;
|
|
|
|
me.applied_offset = v;
|
|
|
|
me.prop.setDoubleValue(raw + me.applied_offset);
|
|
|
|
},
|
2006-08-17 15:59:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Class that manages a dynamic cockpit view by manipulating
|
|
|
|
# sim/current-view/goal-*-offset-deg properties.
|
|
|
|
#
|
|
|
|
ViewManager = {
|
2006-12-09 22:42:13 +00:00
|
|
|
new : func {
|
2006-08-17 15:59:44 +00:00
|
|
|
var m = { parents : [ViewManager] };
|
2006-08-22 10:06:46 +00:00
|
|
|
m.elapsedN = props.globals.getNode("/sim/time/elapsed-sec", 1);
|
|
|
|
m.deltaN = props.globals.getNode("/sim/time/delta-realtime-sec", 1);
|
|
|
|
|
2006-08-18 17:00:16 +00:00
|
|
|
m.headingN = props.globals.getNode("/orientation/heading-deg", 1);
|
|
|
|
m.pitchN = props.globals.getNode("/orientation/pitch-deg", 1);
|
|
|
|
m.rollN = props.globals.getNode("/orientation/roll-deg", 1);
|
2006-08-19 22:24:24 +00:00
|
|
|
m.slipN = props.globals.getNode("/orientation/side-slip-deg", 1);
|
2006-12-07 22:38:07 +00:00
|
|
|
m.speedN = props.globals.getNode("velocities/airspeed-kt", 1);
|
2006-08-19 22:24:24 +00:00
|
|
|
|
|
|
|
m.wind_dirN = props.globals.getNode("/environment/wind-from-heading-deg", 1);
|
|
|
|
m.wind_speedN = props.globals.getNode("/environment/wind-speed-kt", 1);
|
2006-08-17 15:59:44 +00:00
|
|
|
|
2006-08-18 17:00:16 +00:00
|
|
|
m.heading_axis = ViewAxis.new("/sim/current-view/goal-heading-offset-deg");
|
|
|
|
m.pitch_axis = ViewAxis.new("/sim/current-view/goal-pitch-offset-deg");
|
|
|
|
m.roll_axis = ViewAxis.new("/sim/current-view/goal-roll-offset-deg");
|
2006-08-17 15:59:44 +00:00
|
|
|
|
2006-12-09 22:42:13 +00:00
|
|
|
# accelerations are converted to G (one G is subtraced from z-accel)
|
|
|
|
m.axI = Input.new("/accelerations/pilot/x-accel-fps_sec", 0.03108095, 0, 0.58, 0);
|
|
|
|
m.ayI = Input.new("/accelerations/pilot/y-accel-fps_sec", 0.03108095, 0, 0.95);
|
|
|
|
m.azI = Input.new("/accelerations/pilot/z-accel-fps_sec", -0.03108095, -1, 0.46);
|
2006-08-17 15:59:44 +00:00
|
|
|
|
|
|
|
# velocities are converted to knots
|
2006-12-09 22:42:13 +00:00
|
|
|
m.vxI = Input.new("/velocities/uBody-fps", 0.5924838, 0, 0.45);
|
|
|
|
m.vyI = Input.new("/velocities/vBody-fps", 0.5924838, 0);
|
|
|
|
m.vzI = Input.new("/velocities/wBody-fps", 0.5924838, 0);
|
2006-08-17 15:59:44 +00:00
|
|
|
|
2006-08-24 20:55:18 +00:00
|
|
|
# turn WoW bool into smooth values ranging from 0 to 1
|
2006-12-09 22:42:13 +00:00
|
|
|
m.wowI = Input.new("/gear/gear/wow", 1, 0, 0.74);
|
2006-11-03 17:38:32 +00:00
|
|
|
m.hdg_change = aircraft.lowpass.new(0.95);
|
|
|
|
m.ubody = aircraft.lowpass.new(0.95);
|
2006-08-19 22:24:24 +00:00
|
|
|
m.last_heading = m.headingN.getValue();
|
|
|
|
m.size_factor = -getprop("/sim/chase-distance-m") / 25;
|
2006-12-09 22:42:13 +00:00
|
|
|
|
|
|
|
if (props.globals.getNode("rotors", 0) != nil) {
|
|
|
|
m.default_helicopter();
|
|
|
|
} else {
|
|
|
|
m.default_plane();
|
|
|
|
}
|
2006-08-17 15:59:44 +00:00
|
|
|
m.reset();
|
|
|
|
return m;
|
|
|
|
},
|
|
|
|
reset : func {
|
2006-12-09 22:42:13 +00:00
|
|
|
me.lookat_active = 0;
|
2006-08-17 15:59:44 +00:00
|
|
|
me.heading_axis.reset();
|
|
|
|
me.pitch_axis.reset();
|
|
|
|
me.roll_axis.reset();
|
2006-12-03 23:59:50 +00:00
|
|
|
me.add_offset();
|
2006-08-17 15:59:44 +00:00
|
|
|
},
|
2006-08-18 17:00:16 +00:00
|
|
|
add_offset : func {
|
|
|
|
me.heading_axis.add_offset();
|
|
|
|
me.pitch_axis.add_offset();
|
|
|
|
me.roll_axis.add_offset();
|
|
|
|
},
|
2006-08-17 15:59:44 +00:00
|
|
|
apply : func {
|
2006-12-09 22:42:13 +00:00
|
|
|
if (me.lookat_active) {
|
|
|
|
me.heading_axis.prop.setValue(me.lookat_heading);
|
|
|
|
me.pitch_axis.prop.setValue(me.lookat_pitch);
|
|
|
|
return;
|
|
|
|
}
|
2006-08-19 22:24:24 +00:00
|
|
|
|
2006-12-09 22:42:13 +00:00
|
|
|
me.pitch = me.pitchN.getValue();
|
|
|
|
me.roll = me.rollN.getValue();
|
|
|
|
me.wow = me.wowI.get();
|
|
|
|
|
|
|
|
me.calc != nil and me.calc();
|
|
|
|
me.calc_heading != nil and me.heading_axis.apply(me.calc_heading());
|
|
|
|
me.calc_pitch != nil and me.pitch_axis.apply(me.calc_pitch());
|
|
|
|
me.calc_roll != nil and me.roll_axis.apply(me.calc_roll());
|
|
|
|
},
|
|
|
|
lookat : func(h = nil, p = nil) {
|
|
|
|
if (h == nil) {
|
|
|
|
view.resetView();
|
|
|
|
me.lookat_active = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
me.lookat_heading = h;
|
|
|
|
me.lookat_pitch = p;
|
|
|
|
me.lookat_active = 1;
|
|
|
|
},
|
|
|
|
};
|
2006-08-19 22:24:24 +00:00
|
|
|
|
2006-12-09 22:42:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
ViewManager.default_plane = func {
|
|
|
|
me.calc = func {
|
2006-11-02 21:06:36 +00:00
|
|
|
# calculate steering factor
|
2006-08-19 22:24:24 +00:00
|
|
|
var hdg = me.headingN.getValue();
|
2006-11-02 21:06:36 +00:00
|
|
|
var hdiff = normdeg(me.last_heading - hdg);
|
2006-08-19 22:24:24 +00:00
|
|
|
me.last_heading = hdg;
|
2006-12-09 22:42:13 +00:00
|
|
|
me.steering = normatan(me.hdg_change.filter(hdiff)) * me.size_factor;
|
2006-12-07 22:38:07 +00:00
|
|
|
|
2006-12-09 22:42:13 +00:00
|
|
|
me.az = me.azI.get();
|
|
|
|
me.vx = me.vxI.get();
|
|
|
|
|
|
|
|
# calculate sideslip factor (zeroed when no forward ground speed)
|
|
|
|
var wspd = me.wind_speedN.getValue();
|
|
|
|
var wdir = me.headingN.getValue() - me.wind_dirN.getValue();
|
|
|
|
var u = me.vx - wspd * cos(wdir);
|
|
|
|
me.slip = sin(me.slipN.getValue()) * me.ubody.filter(normatan(u / 10));
|
|
|
|
}
|
|
|
|
|
|
|
|
me.calc_heading = func { # heading
|
|
|
|
-15 * sin(me.roll) * cos(me.pitch) # due to roll
|
|
|
|
+ 40 * me.steering * me.wow # due to ground steering
|
|
|
|
+ 10 * me.slip * (1 - me.wow) # due to sideslip (in air)
|
|
|
|
}
|
|
|
|
|
|
|
|
me.calc_pitch = func { # pitch
|
|
|
|
10 * sin(me.roll) * sin(me.roll) # due to roll
|
|
|
|
+ 30 * (1 / (1 + math.exp(2 - me.az)) # due to G load
|
|
|
|
- 0.119202922) # [move to origin; 1/(1+exp(2)) ]
|
|
|
|
}
|
|
|
|
|
|
|
|
me.calc_roll = nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ViewManager.default_helicopter = func {
|
|
|
|
me.calc = func {
|
|
|
|
me.lowspeed = 1 - normatan(me.speedN.getValue() / 20);
|
|
|
|
}
|
|
|
|
|
|
|
|
me.calc_heading = func { # view heading due to
|
|
|
|
-50 * npow(sin(me.roll) * cos(me.pitch), 2) # roll
|
|
|
|
}
|
|
|
|
|
|
|
|
me.calc_pitch = func { # view pitch due to
|
|
|
|
(me.pitch < 0 ? -35 : -40) * sin(me.pitch) * me.lowspeed # pitch
|
|
|
|
+ 15 * sin(me.roll) * sin(me.roll) # roll
|
|
|
|
}
|
|
|
|
|
|
|
|
me.calc_roll = func { # view roll due to
|
|
|
|
-15 * sin(me.roll) * cos(me.pitch) * me.lowspeed # roll
|
|
|
|
}
|
|
|
|
}
|
2006-08-17 15:59:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2006-08-17 22:58:06 +00:00
|
|
|
# Update loop for the whole dynamic view manager. It only runs if
|
2006-08-17 15:59:44 +00:00
|
|
|
# /sim/view[0]/dynamic/enabled is true.
|
|
|
|
#
|
|
|
|
main_loop = func(id) {
|
|
|
|
if (id != loop_id) {
|
|
|
|
return;
|
|
|
|
}
|
2006-08-17 16:42:00 +00:00
|
|
|
if (cockpit_view and !panel_visible and !mouse_button) {
|
2006-08-17 15:59:44 +00:00
|
|
|
view_manager.apply();
|
|
|
|
}
|
|
|
|
settimer(func { main_loop(id) }, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var original_resetView = nil;
|
|
|
|
var panel_visibilityN = nil;
|
2006-08-22 10:06:46 +00:00
|
|
|
var dynamic_view = nil;
|
|
|
|
var view_manager = nil;
|
2006-08-17 15:59:44 +00:00
|
|
|
|
2006-08-17 18:53:44 +00:00
|
|
|
var cockpit_view = nil;
|
|
|
|
var panel_visible = nil;
|
|
|
|
var mouse_button = nil;
|
2006-08-17 15:59:44 +00:00
|
|
|
|
2006-08-18 17:00:16 +00:00
|
|
|
var loop_id = 0;
|
|
|
|
var L = []; # vector of listener ids; allows to remove all listeners (= useless feature :-)
|
|
|
|
|
|
|
|
|
2006-08-17 15:59:44 +00:00
|
|
|
|
|
|
|
# Initialization.
|
|
|
|
#
|
2006-12-07 22:38:07 +00:00
|
|
|
setlistener("/sim/signals/fdm-initialized", func {
|
2006-08-22 10:06:46 +00:00
|
|
|
# disable menu entry and return for inappropriate FDMs (see Main/fg_init.cxx)
|
|
|
|
var fdms = {
|
|
|
|
acms:0, ada:0, balloon:0, external:0,
|
|
|
|
jsb:1, larcsim:1, magic:0, network:0,
|
|
|
|
null:0, pipe:0, ufo:0, yasim:1,
|
|
|
|
};
|
2006-08-21 09:45:48 +00:00
|
|
|
var fdm = getprop("/sim/flight-model");
|
|
|
|
if (!contains(fdms, fdm) or !fdms[fdm]) {
|
|
|
|
return gui.menuEnable("dynamic-view", 0);
|
|
|
|
}
|
|
|
|
|
2006-08-18 17:00:16 +00:00
|
|
|
# some properties may still be unavailable or nil
|
|
|
|
props.globals.getNode("/accelerations/pilot/x-accel-fps_sec", 1).setDoubleValue(0);
|
|
|
|
props.globals.getNode("/accelerations/pilot/y-accel-fps_sec", 1).setDoubleValue(0);
|
|
|
|
props.globals.getNode("/accelerations/pilot/z-accel-fps_sec", 1).setDoubleValue(-32);
|
2006-08-19 22:24:24 +00:00
|
|
|
props.globals.getNode("/orientation/side-slip-deg", 1).setDoubleValue(0);
|
2006-08-18 17:00:16 +00:00
|
|
|
props.globals.getNode("/gear/gear/wow", 1).setBoolValue(1);
|
2006-08-17 15:59:44 +00:00
|
|
|
|
2006-08-17 18:53:44 +00:00
|
|
|
# let listeners keep some variables up-to-date, so that they don't have
|
|
|
|
# to be queried in the loop
|
|
|
|
append(L, setlistener("/sim/current-view/view-number", func {
|
|
|
|
cockpit_view = (cmdarg().getValue() == 0);
|
|
|
|
}, 1));
|
|
|
|
|
|
|
|
append(L, setlistener("/sim/signals/reinit", func {
|
|
|
|
cockpit_view = getprop("/sim/current-view/view-number") == 0;
|
|
|
|
}, 0));
|
|
|
|
|
|
|
|
append(L, setlistener("/sim/panel/visibility", func {
|
|
|
|
panel_visible = cmdarg().getBoolValue();
|
|
|
|
}, 1));
|
|
|
|
|
|
|
|
append(L, setlistener("/devices/status/mice/mouse/button", func {
|
|
|
|
mouse_button = cmdarg().getBoolValue();
|
|
|
|
}, 1));
|
|
|
|
|
2006-12-09 22:42:13 +00:00
|
|
|
view_manager = ViewManager.new();
|
2006-08-17 15:59:44 +00:00
|
|
|
|
2006-08-18 17:00:16 +00:00
|
|
|
original_resetView = view.resetView;
|
2006-08-17 15:59:44 +00:00
|
|
|
view.resetView = func {
|
|
|
|
original_resetView();
|
|
|
|
if (cockpit_view and dynamic_view) {
|
2006-08-18 17:00:16 +00:00
|
|
|
view_manager.add_offset();
|
2006-08-17 15:59:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
append(L, setlistener("/sim/view/dynamic/enabled", func {
|
|
|
|
dynamic_view = cmdarg().getBoolValue();
|
|
|
|
loop_id += 1;
|
|
|
|
view.resetView();
|
|
|
|
if (dynamic_view) {
|
|
|
|
main_loop(loop_id);
|
|
|
|
}
|
|
|
|
}, 1));
|
2006-08-17 18:53:44 +00:00
|
|
|
|
|
|
|
append(L, setlistener("/sim/signals/reinit", func {
|
2006-12-03 23:59:50 +00:00
|
|
|
view_manager.reset();
|
2006-08-17 18:53:44 +00:00
|
|
|
}, 0));
|
2006-12-07 22:38:07 +00:00
|
|
|
});
|
2006-08-17 15:59:44 +00:00
|
|
|
|
|
|
|
|