Anteworld - Outerra Game > JavaScript development

Extended Controls

<< < (2/3) > >>

zzz:
I'm having trouble with the air/controls section. None of them seem to register.

this.register_event("air/controls/elevator", {minval:-1, center:1000, vel:1000, acc:1000}, function(v) {
this.log_inf(v);     
}

With car controls and airplane weapons it shows me the value of V but not with air controls.

Uriah:
Try:

1. Declare a global variable named test_var
2. Assign v to test_var in the register_event function
3. log_inf for test_var is update_frame

Uriah:
Also, there is a problem, syntax error unexpected identifier, because you didn't close the method.

Fixed:

--- Code: ---  this.register_axis("air/controls/elevator", {minval:-1, center:1000, vel:1000, acc:1000}, function(v) {
    this.log_inf(v);
    test_var = v;
  });
--- End code ---

});

I am able to log_inf from inside other register_axis or register_event methods functions perfectly fine. Also, for something like elevator controls, you would want to use register_axis method. Register event would be used for switches, like opening and closing a door, lights, etc... register_axis is used for controls which have a range of input stages, such as a joystick axis.

Regards,
Uriah

Uriah:
You can also assign jsbsim properties directly inside the register_axis method:


--- Code: ---  this.register_axis("air/controls/elevator", {minval:-1, maxval:1, vel:0.1}, function(v){
    this.jsbsim['fcs/elevator-cmd-norm']=v;
    test_var=v;
  });
--- End code ---

zzz:
no, none of those are working. I can get Canopy to work, but any buttons with + and - next to them in the controls don't register at all.

My full JS file:


--- Quote ---//aircraft script file
//see http://xtrac.outerraworld.com/trac.fcgi/wiki/aircraft for example and documentation

var fc = 0;
var ammo = 125;

const MaxTurretProjSpeed01 = 1025; // m/s bullet speed
const MaxTurretProjSize01 = 1;
const MaxTurretDev01 = 0.5;
const flapSpeed = 10;
const flapAccel = 100;
const flapMinRange = -35;
const flapMaxRange = 35;

function radians(v){
  return v*Math.PI/180.0;
}

function getRandomInt(min, max) {
  return Math.floor(Math.random() * (max - min)) + min;
}

function MoA(deviance) {
   r1 = getRandomInt(1, 10001)/10000;
   r2 = getRandomInt(1, 10001)/10000;
   comp1 = (r1/(r1+r2)) * Math.pow(deviance, 2);
   comp2 = (r2/(r1+r2)) * Math.pow(deviance, 2);
   r3 = getRandomInt(1, 5);
   r4 = getRandomInt(1, Math.sqrt(comp1)*10000)/10000;
   r5 = getRandomInt(1, Math.sqrt(comp2)*10000)/10000;
   if (r3 == 1) {
      return [r4, r5];
   } else if (r3 == 2) {
      return [-r4, r5];
   } else if (r3 == 3) {
      return [r4, -r5];
   } else {
      return [-r4, -r5];
   }
}

function axis_integrator(maxspeed,maxaccel,clamp_min,clamp_max){
  this.mac = maxaccel ? maxaccel : 1.0;
  this.msp = maxspeed;
  this.speed = 0;
  this.value = 0;
  this.targ = 0;
  this.cmin = clamp_min!=undefined ? clamp_min : Number.NEGATIVE_INFINITY;
  this.cmax = clamp_max!=undefined ? clamp_max : Number.POSITIVE_INFINITY;
}

axis_integrator.prototype.set = function(v){this.targ = v}
axis_integrator.prototype.set_max_speed = function(v){this.msp = v}
axis_integrator.prototype.changed = function(dt){
  var ob = this.speed;
  var dm = this.mac*dt;
  if(this.targ > ob+dm)
    this.speed += dm;
  else if(this.targ < ob-dm)
    this.speed -= dm;
  else
    this.speed = this.targ;
  var ov = this.value;
  this.value += this.speed*this.msp*dt;
  if(this.value < this.cmin) this.value = this.cmin;
  if(this.value > this.cmax) this.value = this.cmax;
  return ov != this.value;
}

//aircraft instance initialization
//params - parameter object from objdef
function initialize(params){
 
  this.set_fps_camera_pos({x:-0.067,y:4.829,z:0.9675});
  this.geom = this.get_geomob(0);
this.canL = this.geom.get_joint("LCanardP");
this.canR = this.geom.get_joint("RCanardP");
this.rud = this.geom.get_joint("RudderP");
this.flapL = this.geom.get_joint("LFlapP");
this.flapLL = this.geom.get_joint("LLFlapP");
this.flapR = this.geom.get_joint("RFlapP");
this.flapRR = this.geom.get_joint("RRFlapP");

  this.fL = new axis_integrator(radians(flapSpeed), radians(flapAccel), radians(flapMinRange), radians(flapMaxRange));


  this.register_axis("air/weapons/fire_any_armed", {minval:0, center:1000, vel:1000, acc:1000}, function(v) {
   if (v==1) {
      fireLaser = 1;
   }else{
      fireLaser=0;
   }
   
   soundNo = getRandomInt(0,5);
   //this.snd.play(0, soundNo, 0, 0, 0);
});

this.register_event("air/controls/elevator", {minval:0, center:1000, vel:1000, acc:1000}, function(v) {
            this.log_inf(v);     
});


}

//invoked each frame to handle the inputs and animate the model
function update_frame(dt){
 
 if (fc > 1 && fireLaser == 1 && ammo > 0) {
   dev01 = MoA(radians(MaxTurretDev01));
this.fire({x:1.2385,y:2.165,z:-0.302}, {x:Math.sin(dev01[0])*Math.cos(dev01[1]),y:Math.cos(dev01[0])*Math.cos(dev01[1]),z:-Math.sin(dev01[1])}, MaxTurretProjSpeed01, MaxTurretProjSize01, {x:255/255,y:111/255,z:15/255});
 fc = 0;
 ammo = ammo - 1;
 } else {
   fc = fc +1;
 }

if (ammo < 1 && fireLaser == 1) {
   this.log_inf('OUT OF AMMO')
}

  if(this.fL.changed(dt)){
    this.geom.rotate_joint_orig(this.canL, radians(-flapMaxRange)+this.fL.value, {x:1});
  }

}

--- End quote ---

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version