wiki:aircraft

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(param) 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
alt_msl - altitude above the mean sea level
alt_grd - altitude above the ground
invoked each frame to handle the internal state of the object
event switch_seat int seat - seat id to switch to handle seat and camera switching
should return false if given seat/camera id is not present
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 sound returns the corresponding sound interface object
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: -1 not bound, 0 FPS, 1 TPS, 2 TPS follow
method log text - text for console window print text message to outerra console
recognizes error: and warning: prefixes
method add_spot_light float3 offset - model-space offset relative to the bone or model pivot
float3 dir - ligth direction
light_params - light parameters
string joint joint name to attach the light to
Define circular spotlight source
note should be called in initialize()
method add_point_light float3 offset - model-space offset relative to the bone or model pivot
light_params - light parameters
string joint joint name to attach the light to
Define point light source
note: should be called in initialize()
method light uint id
bool on
Turn light on/off
method light_mask uint mask
bool on
uint base=0 - base light id to offset the mask from
Turn multiple lights identified by the bit mask on or off
method light_toggle uint id Toggle light
method light_toggle_mask uint mask
uint base=0 - base light id to offset the mask from
Toggle multiple lights identified by the bit mask
method light_color uint id
float3 color
Set light color/intensity
method lights_off All lights off
method solar_time returns {time, sun_coef} object
time - solar time at vehicle location, in miliseconds
sun_coef - sun position relative to horizon: 0 sun at horizon, 1 sun at zenith, -1 sun at anti-zenith
method fire float3 pos - model-space position
float3 dir - model-space direction of firing
float speed - firing speed
float caliber - trace diameter
float3 color - tracer color
fire a colored plasma tracer

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 instance
  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});
}  
Last modified 22 months ago Last modified on Feb 26, 2016, 6:06:08 AM