wiki:aircraft

Version 8 (modified by angrypig, 7 years ago) (diff)

--

Aircraft

Models imported as aircraft can be bound to a particular JSBSim FDM and given a script to handle the animation of their parts according to the corresponding JSBSim properties. The assignment is done in the model's objdef file by adding the following lines:

"physics" : "jsbsim/c172r"
"script" : "c172.aircraft.js"

"physics" refers to the JSBSim configuration file, found in the program directory under the jsbsim folder.
"script" refers to a custom javascript file in the model directory that will be handling model animations, sounds and binding of the inputs to the underlying JSBSim model.

Aircraft script

Aircraft script, referenced from objdef file of a model, is used to define the link between the JSBSim model, 3D model and the input system.

The script should define two functions that are invoked internally: initialize(reload) and update_frame(dt). Initialize with reload=false is called before any other call to the script; it's expected that the script global variables are set up here. The update_frame event is called each frame to handle the internal state of the model instance.

When these events are invoked, their this parameter is actually a reference to Outerra's internal aircraft interface, that provides several methods that can be invoked via this.method() call.

Aircraft interface (aircraft)

The aircraft interface defines the following methods and events:

type name parameters description
event initialize reload invoked when the aircraft instance first loads
event update_frame dt - delta time from previous frame invoked each frame to handle the internal state of the object
method get_geomob id - id of geometry object (default 0) returns a geometry object that can be used to animate the model joints according to the current FDM state
method jsb returns the corresponding JSBSim object that can be used to get/set properties of JSBSim FDM
method set_fps_camera_pos float3 pos - position of the camera set the position of the first person camera in model space
method get_camera_mode returns current camera mode 0: means FPS camera usually used for the cockpit camera
method log_err text - text for console window add ERROR (red) text message to outerra console
method log_dbg text - text for console window add DEBUG (white) text message to outerra console
method log_inf text - text for console window add INFO (grey) text message to outerra console

Example aircraft script

var geom,jsb;

var     rotor_main_id,
        rotor_tail_id;
        
        // cockpit stuff
        
const PI = 3.14159265358979323846;
const TWO_PI = 6.28318530717958647692;
const DEG2RAD = 180.0 / PI;

function initialize(reload){
  geom = this.get_geomob(0); // get first geometry isntance
  jsb = this.jsb();                      // get JSBSim interface
  
  rotor_main_id = geom.get_joint('rotor_main');
  rotor_tail_id = geom.get_joint('rotor_tail');
  
  this.set_fps_camera_pos(
    geom.get_joint_local_pos(geom.get_joint('camera_pilot')));

  this.activate_event_group("heli");
}

function deg2rad(angle_deg) { return (PI / 180.0) * angle_deg; }

function update_frame(dt) {
  var val;// = 300 * (1.0 / 60.0) * dt * TWO_PI;

  // main / tail rotor 
  val = jsb['propulsion/engine[0]/rotor-rpm'] * (1.0 / 60.0) * dt * TWO_PI;
  geom.rotate_joint(rotor_main_id, val, {x:0,y:0,z:1});
  geom.rotate_joint(rotor_tail_id, val * 4.525, {x:1,y:0,z:0});
}