| 1 |
# SPDX-License-Identifier: GPL-2.0-or-later |
| 2 |
# |
| 3 |
# NOTE! This copyright does *not* cover user models that use these Nasal |
| 4 |
# services by normal function calls - this is merely considered normal |
| 5 |
# use of the code, and does *not* fall under the heading of "derived |
| 6 |
# work." |
| 7 |
# |
| 8 |
# Copyright (C) 2006-2008 by mfranz |
| 9 |
|
| 10 |
|
| 11 |
# Dynamic Cockpit View manager. Tries to simulate the pilot's most likely |
| 12 |
# deliberate view direction. Doesn't consider forced view changes due to |
| 13 |
# acceleration. |
| 14 |
# |
| 15 |
# To override the default recipes, put something like this into one of |
| 16 |
# your aircraft's Nasal files: |
| 17 |
# |
| 18 |
# dynamic_view.register(func { |
| 19 |
# # me.default_plane(); # uncomment one of these if you want |
| 20 |
# # me.default_helicopter(); # to base your code on the defaults |
| 21 |
# |
| 22 |
# # positive values rotate (deg) or move (m) |
| 23 |
# me.heading_offset = ... # left |
| 24 |
# me.pitch_offset = ... # up |
| 25 |
# me.roll_offset = ... # right |
| 26 |
# me.x_offset = ... # right (transversal axis) |
| 27 |
# me.y_offset = ... # up (vertical axis) |
| 28 |
# me.z_offset = ... # back/aft (longitudinal axis) |
| 29 |
# me.fov_offset = ... # zoom out (field of view) |
| 30 |
# }); |
| 31 |
# |
| 32 |
# All offsets are by default 0, and you only need to set them if they should |
| 33 |
# be non-zero. The registered function is called for each frame and the respective |
| 34 |
# view parameters are set accordingly. The function can access all internal |
| 35 |
# variables of the view_manager class, such as me.roll, me.pitch, etc., and it |
| 36 |
# can, of course, also use module variables from the file where it's defined. |
| 37 |
# |
| 38 |
# The following commands move smoothly to a fixed view position and back. |
| 39 |
# All values are relative to aircraft origin (absolute), not relative to |
| 40 |
# the default cockpit view position. The time and field-of-view argument |
| 41 |
# is optional. |
| 42 |
# |
| 43 |
# dynamic_view.lookat(hdg, pitch, roll, x, y, z [, time=0.2 [, fov=55]]); |
| 44 |
# dynamic_view.resume(); |
| 45 |
|
| 46 |
|
| 47 |
var FREEZE_DURATION = 2; |
| 48 |
var BLEND_TIME = 0.2; |
| 49 |
|
| 50 |
|
| 51 |
var sin = func(a) math.sin(a * D2R); |
| 52 |
var cos = func(a) math.cos(a * D2R); |
| 53 |
var sigmoid = func(x) { 1 / (1 + math.exp(-x)) } |
| 54 |
var nsigmoid = func(x) { 2 / (1 + math.exp(-x)) - 1 } |
| 55 |
var pow = func(v, w) { v < 0 ? nil : v == 0 ? 0 : math.exp(math.ln(v) * w) } |
| 56 |
var npow = func(v, w) { v == 0 ? 0 : math.exp(math.ln(abs(v)) * w) * (v < 0 ? -1 : 1) } |
| 57 |
var clamp = func(v, min, max) { v < min ? min : v > max ? max : v } |
| 58 |
var normatan = func(x) { math.atan2(x, 1) * 2 / math.pi } |
| 59 |
|
| 60 |
var normdeg = func(a) { |
| 61 |
while (a >= 180) |
| 62 |
a -= 360; |
| 63 |
while (a < -180) |
| 64 |
a += 360; |
| 65 |
return a; |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
# Class that reads a property value, applies factor & offset, clamps to min & max, |
| 71 |
# and optionally lowpass filters. |
| 72 |
# |
| 73 |
var Input = { |
| 74 |
new : func(prop = "/null", factor = 1, offset = 0, filter = 0, min = nil, max = nil) { |
| 75 |
var m = { parents : [Input] }; |
| 76 |
m.prop = isa(props.Node, prop) ? prop : props.globals.getNode(prop, 1); |
| 77 |
m.factor = factor; |
| 78 |
m.offset = offset; |
| 79 |
m.min = min; |
| 80 |
m.max = max; |
| 81 |
m.lowpass = filter ? aircraft.lowpass.new(filter) : nil; |
| 82 |
return m; |
| 83 |
}, |
| 84 |
get : func { |
| 85 |
var v = me.prop.getValue() * me.factor + me.offset; |
| 86 |
if (me.min != nil and v < me.min) |
| 87 |
v = me.min; |
| 88 |
if (me.max != nil and v > me.max) |
| 89 |
v = me.max; |
| 90 |
|
| 91 |
return me.lowpass == nil ? v : me.lowpass.filter(v); |
| 92 |
}, |
| 93 |
set : func(v) { |
| 94 |
me.prop.setDoubleValue(v); |
| 95 |
}, |
| 96 |
}; |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
# Class that maintains one sim/current-view/goal-*-offset-deg property. |
| 101 |
# |
| 102 |
var ViewAxis = { |
| 103 |
new : func(prop) { |
| 104 |
var m = { parents : [ViewAxis] }; |
| 105 |
m.prop = props.globals.getNode(prop, 1); |
| 106 |
if (m.prop.getType() == "NONE") |
| 107 |
m.prop.setDoubleValue(0); |
| 108 |
|
| 109 |
m.reset(); |
| 110 |
return m; |
| 111 |
}, |
| 112 |
reset : func { |
| 113 |
me.applied_offset = 0; |
| 114 |
}, |
| 115 |
add_offset : func { |
| 116 |
me.prop.setValue(me.prop.getValue() + me.applied_offset); |
| 117 |
}, |
| 118 |
sub_offset : func { |
| 119 |
var raw = me.prop.getValue() - me.applied_offset; |
| 120 |
me.prop.setValue(raw); |
| 121 |
return raw; |
| 122 |
}, |
| 123 |
apply : func(v) { |
| 124 |
var raw = me.prop.getValue() - me.applied_offset; |
| 125 |
me.applied_offset = v; |
| 126 |
me.prop.setDoubleValue(raw + me.applied_offset); |
| 127 |
}, |
| 128 |
static : func(v) { |
| 129 |
normdeg(v - me.prop.getValue() + me.applied_offset); |
| 130 |
}, |
| 131 |
}; |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
# Singleton class that manages a dynamic cockpit view by manipulating |
| 136 |
# sim/current-view/goal-*-offset-deg properties. |
| 137 |
# |
| 138 |
var view_manager = { |
| 139 |
init : func { |
| 140 |
me.elapsedN = props.globals.getNode("/sim/time/elapsed-sec", 1); |
| 141 |
me.deltaN = props.globals.getNode("/sim/time/delta-realtime-sec", 1); |
| 142 |
|
| 143 |
me.headingN = props.globals.getNode("/orientation/heading-deg", 1); |
| 144 |
me.pitchN = props.globals.getNode("/orientation/pitch-deg", 1); |
| 145 |
me.rollN = props.globals.getNode("/orientation/roll-deg", 1); |
| 146 |
me.slipN = props.globals.getNode("/orientation/side-slip-deg", 1); |
| 147 |
me.speedN = props.globals.getNode("velocities/airspeed-kt", 1); |
| 148 |
|
| 149 |
me.wind_dirN = props.globals.getNode("/environment/wind-from-heading-deg", 1); |
| 150 |
me.wind_speedN = props.globals.getNode("/environment/wind-speed-kt", 1); |
| 151 |
|
| 152 |
me.axes = [ |
| 153 |
me.heading_axis = ViewAxis.new("/sim/current-view/goal-heading-offset-deg"), |
| 154 |
me.pitch_axis = ViewAxis.new("/sim/current-view/goal-pitch-offset-deg"), |
| 155 |
me.roll_axis = ViewAxis.new("/sim/current-view/goal-roll-offset-deg"), |
| 156 |
me.x_axis = ViewAxis.new("/sim/current-view/x-offset-m"), |
| 157 |
me.y_axis = ViewAxis.new("/sim/current-view/y-offset-m"), |
| 158 |
me.z_axis = ViewAxis.new("/sim/current-view/z-offset-m"), |
| 159 |
me.fov_axis = ViewAxis.new("/sim/current-view/field-of-view"), |
| 160 |
]; |
| 161 |
|
| 162 |
# accelerations are converted to G (Earth gravitation is omitted) |
| 163 |
me.ax = Input.new("/accelerations/pilot/x-accel-fps_sec", 0.03108095, 0, 0.58, 0); |
| 164 |
me.ay = Input.new("/accelerations/pilot/y-accel-fps_sec", 0.03108095, 0, 0.95); |
| 165 |
me.az = Input.new("/accelerations/pilot/z-accel-fps_sec", -0.03108095, -1, 0.46); |
| 166 |
|
| 167 |
# velocities are converted to knots |
| 168 |
me.vx = Input.new("/velocities/uBody-fps", 0.5924838, 0, 0.45); |
| 169 |
me.vy = Input.new("/velocities/vBody-fps", 0.5924838, 0); |
| 170 |
me.vz = Input.new("/velocities/wBody-fps", 0.5924838, 0); |
| 171 |
|
| 172 |
# turn WoW bool into smooth values ranging from 0 to 1 |
| 173 |
me.wow = Input.new("/gear/gear/wow", 1, 0, 0.74); |
| 174 |
me.hdg_change = aircraft.lowpass.new(0.95); |
| 175 |
me.ubody = aircraft.lowpass.new(0.95); |
| 176 |
me.last_heading = me.headingN.getValue(); |
| 177 |
me.size_factor = getprop("/sim/chase-distance-m") / -25; |
| 178 |
|
| 179 |
# "lookat" blending |
| 180 |
me.blendN = props.globals.getNode("/sim/view/dynamic/blend", 1); |
| 181 |
me.blendN.setDoubleValue(0); |
| 182 |
me.blendtime = BLEND_TIME; |
| 183 |
me.frozen = 0; |
| 184 |
|
| 185 |
if (props.globals.getNode("rotors", 0) != nil) |
| 186 |
me.calculate = me.default_helicopter; |
| 187 |
else |
| 188 |
me.calculate = me.default_plane; |
| 189 |
me.reset(); |
| 190 |
}, |
| 191 |
reset : func { |
| 192 |
me.heading_offset = me.heading = me.target_heading = 0; |
| 193 |
me.pitch_offset = me.pitch = me.target_pitch = 0; |
| 194 |
me.roll_offset = me.roll = me.target_roll = 0; |
| 195 |
me.x_offset = me.x = me.target_x = 0; |
| 196 |
me.y_offset = me.y = me.target_y = 0; |
| 197 |
me.z_offset = me.z = me.target_z = 0; |
| 198 |
me.fov_offset = me.fov = me.target_fov = 0; |
| 199 |
|
| 200 |
interpolate(me.blendN); |
| 201 |
me.blendN.setDoubleValue(0); |
| 202 |
foreach (var a; me.axes) |
| 203 |
a.reset(); |
| 204 |
|
| 205 |
me.add_offset(); |
| 206 |
}, |
| 207 |
add_offset : func { |
| 208 |
me.heading_axis.add_offset(); |
| 209 |
me.pitch_axis.add_offset(); |
| 210 |
me.roll_axis.add_offset(); |
| 211 |
me.fov_axis.add_offset(); |
| 212 |
}, |
| 213 |
apply : func { |
| 214 |
if (me.elapsedN.getValue() < me.frozen) |
| 215 |
return; |
| 216 |
elsif (me.frozen) |
| 217 |
me.unfreeze(); |
| 218 |
|
| 219 |
me.pitch = me.pitchN.getValue(); |
| 220 |
me.roll = me.rollN.getValue(); |
| 221 |
|
| 222 |
me.calculate(); |
| 223 |
|
| 224 |
var b = me.blendN.getValue(); |
| 225 |
var B = 1 - b; |
| 226 |
me.heading = me.target_heading * b + me.heading_offset * B; |
| 227 |
me.pitch = me.target_pitch * b + me.pitch_offset * B; |
| 228 |
me.roll = me.target_roll * b + me.roll_offset * B; |
| 229 |
me.x = me.target_x * b + me.x_offset * B; |
| 230 |
me.y = me.target_y * b + me.y_offset * B; |
| 231 |
me.z = me.target_z * b + me.z_offset * B; |
| 232 |
me.fov = me.target_fov * b + me.fov_offset * B; |
| 233 |
|
| 234 |
me.heading_axis.apply(me.heading); |
| 235 |
me.pitch_axis.apply(me.pitch); |
| 236 |
me.roll_axis.apply(me.roll); |
| 237 |
me.x_axis.apply(me.x); |
| 238 |
me.y_axis.apply(me.y); |
| 239 |
me.z_axis.apply(me.z); |
| 240 |
me.fov_axis.apply(me.fov); |
| 241 |
}, |
| 242 |
lookat : func(heading, pitch, roll, x, y, z, time, fov) { |
| 243 |
me.target_heading = me.heading_axis.static(heading); |
| 244 |
me.target_pitch = me.pitch_axis.static(pitch); |
| 245 |
me.target_roll = me.roll_axis.static(roll); |
| 246 |
me.target_x = me.x_axis.static(x); |
| 247 |
me.target_y = me.y_axis.static(y); |
| 248 |
me.target_z = me.z_axis.static(z); |
| 249 |
me.target_fov = me.fov_axis.static(fov); |
| 250 |
|
| 251 |
me.blendtime = time; |
| 252 |
me.blendN.setValue(0); |
| 253 |
interpolate(me.blendN, 1, me.blendtime); |
| 254 |
}, |
| 255 |
resume : func { |
| 256 |
interpolate(me.blendN, 0, me.blendtime); |
| 257 |
me.blendtime = BLEND_TIME; |
| 258 |
}, |
| 259 |
freeze : func { |
| 260 |
if (!me.frozen) { |
| 261 |
me.target_heading = me.heading; |
| 262 |
me.target_pitch = me.pitch; |
| 263 |
me.target_roll = me.roll; |
| 264 |
me.target_x = me.x; |
| 265 |
me.target_y = me.y; |
| 266 |
me.target_z = me.z; |
| 267 |
me.target_fov = me.fov; |
| 268 |
me.blendN.setDoubleValue(1); |
| 269 |
} |
| 270 |
me.frozen = me.elapsedN.getValue() + FREEZE_DURATION; |
| 271 |
}, |
| 272 |
unfreeze : func { |
| 273 |
if (me.frozen) { |
| 274 |
me.frozen = 0; |
| 275 |
me.resume(); |
| 276 |
} |
| 277 |
}, |
| 278 |
}; |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
# default calculations for a plane |
| 283 |
# |
| 284 |
view_manager.default_plane = func { |
| 285 |
var wow = me.wow.get(); |
| 286 |
|
| 287 |
# calculate steering factor |
| 288 |
var hdg = me.headingN.getValue(); |
| 289 |
var hdiff = normdeg(me.last_heading - hdg); |
| 290 |
me.last_heading = hdg; |
| 291 |
var steering = 0; # normatan(me.hdg_change.filter(hdiff)) * me.size_factor; |
| 292 |
|
| 293 |
var az = me.az.get(); |
| 294 |
var vx = me.vx.get(); |
| 295 |
|
| 296 |
# calculate sideslip factor (zeroed when no forward ground speed) |
| 297 |
var wspd = me.wind_speedN.getValue(); |
| 298 |
var wdir = me.headingN.getValue() - me.wind_dirN.getValue(); |
| 299 |
var u = vx - wspd * cos(wdir); |
| 300 |
var slip = sin(me.slipN.getValue()) * me.ubody.filter(normatan(u / 10)); |
| 301 |
|
| 302 |
me.heading_offset = # view heading |
| 303 |
-15 * sin(me.roll) * cos(me.pitch) # due to roll |
| 304 |
+ 40 * steering * wow # due to ground steering |
| 305 |
+ 10 * slip * (1 - wow); # due to sideslip (in air) |
| 306 |
|
| 307 |
me.pitch_offset = # view pitch |
| 308 |
10 * sin(me.roll) * sin(me.roll) # due to roll |
| 309 |
+ 30 * (1 / (1 + math.exp(2 - az)) # due to G load |
| 310 |
- 0.119202922); # [move to origin; 1/(1+exp(2)) ] |
| 311 |
|
| 312 |
me.roll_offset = 0; |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
# default calculations for a helicopter |
| 318 |
# |
| 319 |
view_manager.default_helicopter = func { |
| 320 |
var lowspeed = 1 - normatan(me.speedN.getValue() / 20); |
| 321 |
|
| 322 |
me.heading_offset = # view heading due to |
| 323 |
-50 * npow(sin(me.roll) * cos(me.pitch), 2); # roll |
| 324 |
|
| 325 |
me.pitch_offset = # view pitch due to |
| 326 |
(me.pitch < 0 ? -35 : -40) * sin(me.pitch) * lowspeed # pitch |
| 327 |
+ 15 * sin(me.roll) * sin(me.roll); # roll |
| 328 |
|
| 329 |
me.roll_offset = # view roll due to |
| 330 |
-15 * sin(me.roll) * cos(me.pitch) * lowspeed; # roll |
| 331 |
} |
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
# Update loop for the whole dynamic view manager. It only runs if |
| 336 |
# /sim/current-view/dynamic-view is true. |
| 337 |
# |
| 338 |
var main_loop = func(id) { |
| 339 |
id == loop_id or return; |
| 340 |
if (cockpit_view and !panel_visible) { |
| 341 |
if (mouse_button) |
| 342 |
freeze(); |
| 343 |
else |
| 344 |
view_manager.apply(); |
| 345 |
} |
| 346 |
settimer(func { main_loop(id) }, 0); |
| 347 |
} |
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
var freeze = func { |
| 352 |
if (mouse_mode == 0) |
| 353 |
view_manager.freeze(); |
| 354 |
} |
| 355 |
|
| 356 |
var register = func(f) { |
| 357 |
view_manager.calculate = f; |
| 358 |
} |
| 359 |
|
| 360 |
var reset = func { |
| 361 |
view_manager.reset(); |
| 362 |
} |
| 363 |
|
| 364 |
var lookat = func { |
| 365 |
call(view_manager.lookat, arg, view_manager); |
| 366 |
} |
| 367 |
|
| 368 |
var resume = func { |
| 369 |
view_manager.resume(); |
| 370 |
} |
| 371 |
|
| 372 |
|
| 373 |
var original_resetView = nil; |
| 374 |
var panel_visibilityN = nil; |
| 375 |
var dynamic_view = nil; |
| 376 |
|
| 377 |
var cockpit_view = nil; |
| 378 |
var panel_visible = nil; # whether 2D panel is visible |
| 379 |
var elapsedN = nil; |
| 380 |
var mouse_mode = nil; |
| 381 |
var mouse_button = nil; |
| 382 |
var enabled = nil; |
| 383 |
|
| 384 |
var loop_id = 0; |
| 385 |
|
| 386 |
|
| 387 |
# disable menu entry and return for inappropriate FDMs (see Main/fg_init.cxx) |
| 388 |
var fdms = { |
| 389 |
acms:0, ada:0, balloon:0, external:0, |
| 390 |
jsb:1, larcsim:1, magic:0, network:0, |
| 391 |
null:0, pipe:0, ufo:0, yasim:1, |
| 392 |
}; |
| 393 |
var fdm = getprop("/sim/flight-model"); |
| 394 |
if (!contains(fdms, fdm) or !fdms[fdm]) |
| 395 |
return; |
| 396 |
|
| 397 |
enabled = props.globals.getNode("/sim").getChildren("view"); |
| 398 |
forindex (var i; enabled) |
| 399 |
enabled[i] = ((var n = enabled[i].getNode("config/dynamic-view")) != nil) and n.getBoolValue(); |
| 400 |
|
| 401 |
# some properties may still be unavailable or nil |
| 402 |
props.globals.initNode("/accelerations/pilot/x-accel-fps_sec", 0); |
| 403 |
props.globals.initNode("/accelerations/pilot/y-accel-fps_sec", 0); |
| 404 |
props.globals.initNode("/accelerations/pilot/z-accel-fps_sec", -32); |
| 405 |
props.globals.initNode("/orientation/side-slip-deg", 0); |
| 406 |
props.globals.initNode("/gear/gear/wow", 1, "BOOL"); |
| 407 |
elapsedN = props.globals.getNode("/sim/time/elapsed-sec", 1); |
| 408 |
|
| 409 |
# let listeners keep some variables up-to-date, so that they do not have |
| 410 |
# to be queried in the loop |
| 411 |
setlistener("/sim/panel/visibility", func(n) { panel_visible = n.getValue() }, 1); |
| 412 |
setlistener("/sim/current-view/view-number", func(n) { cockpit_view = enabled[n.getValue()] }, 1); |
| 413 |
setlistener("/devices/status/mice/mouse/button", func(n) { mouse_button = n.getValue() }, 1); |
| 414 |
setlistener("/devices/status/mice/mouse/x", freeze); |
| 415 |
setlistener("/devices/status/mice/mouse/y", freeze); |
| 416 |
setlistener("/devices/status/mice/mouse/mode", func(n) { |
| 417 |
if (mouse_mode = n.getValue()) |
| 418 |
view_manager.unfreeze(); |
| 419 |
}, 1); |
| 420 |
|
| 421 |
setlistener("/sim/signals/reinit", func(n) { |
| 422 |
n.getValue() and return; |
| 423 |
cockpit_view = enabled[getprop("/sim/current-view/view-number")]; |
| 424 |
view_manager.reset(); |
| 425 |
}, 0); |
| 426 |
|
| 427 |
view_manager.init(); |
| 428 |
|
| 429 |
original_resetView = view.resetView; |
| 430 |
view.resetView = func { |
| 431 |
original_resetView(); |
| 432 |
if (cockpit_view and dynamic_view) |
| 433 |
view_manager.add_offset(); |
| 434 |
} |
| 435 |
|
| 436 |
settimer(func { |
| 437 |
setlistener("/sim/current-view/dynamic-view", func(n) { |
| 438 |
dynamic_view = n.getBoolValue(); |
| 439 |
loop_id += 1; |
| 440 |
view.resetView(); |
| 441 |
if (dynamic_view) |
| 442 |
main_loop(loop_id); |
| 443 |
}, 1); |
| 444 |
}, 0); |
| 445 |
|
| 446 |
|
| 447 |
|