Advertisement
Guest User

Untitled

a guest
Nov 17th, 2012
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //vehicle script file
  2. //see http://xtrac.outerra.com/index.fcgi/wiki/vehicle for example and documentation
  3.  
  4. var sound = -1;
  5.  
  6. //invoked only the first time the model is loaded, or upon reload
  7. function init_chassis(){
  8.     var wheelparam = {
  9.         radius: 0.30,
  10.         width: 0.20,
  11.         suspension_max: 0.1,
  12.         suspension_min: -0.05,
  13.         suspension_stiffness: 50.0,
  14.         damping_compression: 0.06,
  15.         damping_relaxation: 0.05,
  16.         slip: 2.2,
  17.         roll_influence: 0.1,
  18.         rotation: -1
  19.     };
  20.     this.add_wheel('wheel_FL', wheelparam);
  21.     this.add_wheel('wheel_FR', wheelparam);
  22.     this.add_wheel('wheel_RL', wheelparam);
  23.     this.add_wheel('wheel_RR', wheelparam);
  24.  
  25.     this.load_sound("diesel-engine-start.ogg"); //start
  26.     this.load_sound("18L16V_onidle_ex.ogg");    //idle
  27.     this.load_sound("18L16V_onverylow_in.ogg"); //throttle
  28.     this.basepitch = [ 0, 1, 0.7 ];             //base pitch for start, idle, throttle
  29.     this.add_sound_emitter("wheel_FL");
  30.  
  31.     //engine properties -- modify these to change the engine behaviour
  32.     this.wheelRadius = 0.24;
  33.  
  34.     this.torquePoints = [ { rpm: 0,    torque: 220},
  35.                           { rpm: 1000, torque: 265},
  36.                           { rpm: 2000, torque: 290},
  37.                           { rpm: 3000, torque: 315},
  38.                           { rpm: 4000, torque: 335},
  39.                           { rpm: 4900, torque: 365},
  40.                           { rpm: 6000, torque: 340},
  41.                           { rpm: 7000, torque: 320},
  42.                           { rpm: 8000, torque: 290},
  43.                           { rpm: 8100, torque: 0  } ];
  44.  
  45.     this.forwardGears = [ { ratio: 4.20, shiftUp: 7000, shiftDown: -1   },
  46.                           { ratio: 2.49, shiftUp: 7000, shiftDown: 4000 },
  47.                           { ratio: 1.66, shiftUp: 7000, shiftDown: 4000 },
  48.                           { ratio: 1.24, shiftUp: 7000, shiftDown: 4000 },
  49.                           { ratio: 1.00, shiftUp: -1,   shiftDown: 4000 } ];
  50.     this.reverseGears = [ { ratio: 4.20, shiftUp: -1, shiftDown: -1 } ];
  51.  
  52.     this.finalRatio = 3;
  53.     this.torqueMultiplier = 10;
  54.     this.maximumSpeed = -1; // speed in m/s. a value < 0 means there's no maximum speed
  55.     //end of engine properties
  56.  
  57.     this.torque = engineTorque;
  58.  
  59.     return {mass:1500, steering:2.0, steering_ecf:60, centering: 2.6, centering_ecf:20};
  60. }
  61.  
  62. //invoked for each new instance of the vehicle
  63. function init_vehicle() {
  64.     this.set_fps_camera_pos({x:-0.33,y:-0.25,z:1.15});
  65.  
  66.     this.snd = this.sound();
  67.     this.snd.set_ref_distance(0, 9.0);
  68.  
  69.     this.started = 0;
  70.     this.gear = 0;
  71. }
  72.  
  73. //invoked when engine starts or stops
  74. function engine(start) {
  75.     if (start) {
  76.         this.started = 1;
  77.         this.sound = 0;
  78.         this.snd.play(0, 0, false, false);
  79.     }
  80. }
  81.  
  82. const BF = 4000.0;
  83.  
  84. function engineTorque(rpm) {
  85.     var min = 0;
  86.     var max = this.torquePoints.length - 1;
  87.  
  88.     if (rpm > this.torquePoints[max].rpm || rpm < this.torquePoints[min].rpm)
  89.         return 0;
  90.  
  91.     while (max - min > 1) {
  92.         var mid = Math.floor(min + (max - min) / 2);
  93.         var tp = this.torquePoints[mid];
  94.         if (tp.rpm == rpm) {
  95.             return tp.torque;
  96.         } else if (tp.rpm > rpm) {
  97.             max = mid;
  98.         } else {
  99.             min = mid;
  100.         }
  101.     }
  102.  
  103.     var minTp = this.torquePoints[min];
  104.     var maxTp = this.torquePoints[max];
  105.     var TM = maxTp.torque;
  106.     var Tm = minTp.torque;
  107.     var RM = maxTp.rpm;
  108.     var Rm = minTp.rpm;
  109.  
  110.     var a = (TM - Tm) / (RM - Rm);
  111.  
  112.     return rpm * a + Tm - Rm * a;
  113. }
  114.  
  115. //invoked each frame to handle the inputs and animate the model
  116. function update_frame(dt, engine, brake, steering) {
  117.     if (this.started != 1)
  118.         return;
  119.  
  120.     steering *= 0.6;
  121.     this.steer(-2, steering);
  122.  
  123.     var absSpeed = Math.abs(this.speed());
  124.     var khm = absSpeed * 3.6;
  125.     var wheels = absSpeed / (this.wheelRadius * (2 * Math.PI));
  126.  
  127.     var gears;
  128.     if (this.speed() >= 0) {
  129.         gears = this.forwardGears;
  130.     } else {
  131.         gears = this.reverseGears;
  132.     }
  133.  
  134.     var rpm = wheels * gears[this.gear].ratio * this.finalRatio * 60;
  135.  
  136.     //automatic gear shifting
  137.     var currentGear = gears[this.gear];
  138.     if (rpm > currentGear.shiftUp && this.gear < gears.length - 1) {
  139.         this.gear += 1;
  140.     } else if (rpm < currentGear.shiftDown && this.gear > 0) {
  141.         this.gear -= 1;
  142.     }
  143.  
  144.     var force = 0;
  145.     if (this.maximumSpeed < 0 || absSpeed < this.maximumSpeed) {
  146.         force = engine * this.torque(rpm) * this.torqueMultiplier;
  147.     }
  148.     this.wheel_force(2, force);
  149.     this.wheel_force(3, force);
  150.  
  151.  
  152.     brake *= BF;
  153.     this.wheel_brake(-1, brake);
  154.  
  155.     this.animate_wheels();
  156.  
  157.     if (engine == 0 && ((this.sound == 0 && !this.snd.is_playing(0)) || this.sound == 2)) { //idle
  158.         this.snd.stop(0);
  159.         this.snd.play(0, 1, true, false);
  160.         this.sound = 1;
  161.     } else if (engine != 0 && ((this.sound == 0 && !this.snd.is_playing(0)) || this.sound == 1)) { //throttle
  162.         this.snd.stop(0);
  163.         this.snd.play(0, 2, true, false);
  164.         this.sound = 2;
  165.     }
  166.  
  167.     if (this.sound > 0) {
  168.         var pitch = rpm / 5000.0;
  169.         this.snd.set_pitch(0, pitch + this.basepitch[this.sound]);
  170.     }
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement