Advertisement
Guest User

Untitled

a guest
Nov 21st, 2012
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --- bmwM3.js.old        2012-11-17 22:51:49.644171426 +0100
  2. +++ bmwM3.js    2012-11-21 22:34:29.511794340 +0100
  3. @@ -31,29 +31,66 @@ function init_chassis(){
  4.      //engine properties -- modify these to change the engine behaviour
  5.      this.wheelRadius = 0.24;
  6.  
  7. -    this.torquePoints = [ { rpm: 0,    torque: 220},
  8. -                          { rpm: 1000, torque: 265},
  9. -                          { rpm: 2000, torque: 290},
  10. -                          { rpm: 3000, torque: 315},
  11. -                          { rpm: 4000, torque: 335},
  12. -                          { rpm: 4900, torque: 365},
  13. -                          { rpm: 6000, torque: 340},
  14. -                          { rpm: 7000, torque: 320},
  15. -                          { rpm: 8000, torque: 290},
  16. -                          { rpm: 8100, torque: 0  } ];
  17. -
  18. -    this.forwardGears = [ { ratio: 4.20, shiftUp: 7000, shiftDown: -1   },
  19. -                          { ratio: 2.49, shiftUp: 7000, shiftDown: 4000 },
  20. -                          { ratio: 1.66, shiftUp: 7000, shiftDown: 4000 },
  21. -                          { ratio: 1.24, shiftUp: 7000, shiftDown: 4000 },
  22. -                          { ratio: 1.00, shiftUp: -1,   shiftDown: 4000 } ];
  23. +    this.torquePoints = [ { rpm: 0,    torque: 180},
  24. +                          { rpm: 1000, torque: 250},
  25. +                          { rpm: 2000, torque: 335},
  26. +                          { rpm: 3000, torque: 380},
  27. +                          { rpm: 4000, torque: 400},
  28. +                          { rpm: 5000, torque: 395},
  29. +                          { rpm: 6000, torque: 400},
  30. +                          { rpm: 7000, torque: 390},
  31. +                          { rpm: 8000, torque: 365},
  32. +                          { rpm: 9000, torque: 0  } ];
  33. +
  34. +    this.forwardGears = [ { ratio: 4.20, shiftUp: 8000, shiftDown: -1   },
  35. +                          { ratio: 2.49, shiftUp: 8000, shiftDown: 4500 },
  36. +                          { ratio: 1.66, shiftUp: 8000, shiftDown: 5000 },
  37. +                          { ratio: 1.24, shiftUp: 8000, shiftDown: 6000 },
  38. +                          { ratio: 1.00, shiftUp: -1,   shiftDown: 6000 } ];
  39.      this.reverseGears = [ { ratio: 4.20, shiftUp: -1, shiftDown: -1 } ];
  40.  
  41.      this.finalRatio = 3;
  42. -    this.torqueMultiplier = 10;
  43. +    this.torqueMultiplier = 6;
  44.      this.maximumSpeed = -1; // speed in m/s. a value < 0 means there's no maximum speed
  45. +    this.engineBrakeCoefficient = 0.2;
  46. +    this.pitchMultiplier = 1.8;
  47.      //end of engine properties
  48.  
  49. +    this.maxPowerTP = this.torquePoints[0];
  50. +    this.maxRPM = 0;
  51. +    var maxPw = 0;
  52. +    for (var i = 1; i < this.torquePoints.length; ++i) {
  53. +        var tp = this.torquePoints[i];
  54. +        if (tp.rpm * tp.torque > maxPw) {
  55. +            this.maxPowerTP = tp;
  56. +        }
  57. +        if (tp.rpm > this.maxRPM) {
  58. +            this.maxRPM = tp.rpm;
  59. +        }
  60. +    }
  61. +
  62. +    this.neutroGear = { ratio: 0.0, shiftUp: -1, shiftDown: -1, index: 0 };
  63. +    var prev = null;
  64. +    for (var i = 0; i < this.forwardGears.length; ++i) {
  65. +        var gear = this.forwardGears[i];
  66. +        if (prev)
  67. +            prev.next = gear;
  68. +        gear.prev = prev;
  69. +        gear.index = i + 1;
  70. +        prev = gear;
  71. +    }
  72. +
  73. +    prev = null;
  74. +    for (var i = 0; i < this.reverseGears.length; ++i) {
  75. +        var gear = this.reverseGears[i];
  76. +        if (prev)
  77. +            prev.next = gear;
  78. +        gear.prev = prev;
  79. +        gear.index = -i - 1;
  80. +        prev = gear;
  81. +    }
  82. +
  83. +
  84.      this.torque = engineTorque;
  85.  
  86.      return {mass:1500, steering:2.0, steering_ecf:60, centering: 2.6, centering_ecf:20};
  87. @@ -67,7 +104,8 @@ function init_vehicle() {
  88.      this.snd.set_ref_distance(0, 9.0);
  89.  
  90.      this.started = 0;
  91. -    this.gear = 0;
  92. +    this.gear = this.neutroGear;
  93. +    this.direction = 0;
  94.  }
  95.  
  96.  //invoked when engine starts or stops
  97. @@ -112,6 +150,10 @@ function engineTorque(rpm) {
  98.      return rpm * a + Tm - Rm * a;
  99.  }
  100.  
  101. +function sign(x) {
  102. +    return (x > 0.0 ? 1 : (x < 0.0 ? -1 : 0));
  103. +}
  104. +
  105.  //invoked each frame to handle the inputs and animate the model
  106.  function update_frame(dt, engine, brake, steering) {
  107.      if (this.started != 1)
  108. @@ -124,33 +166,48 @@ function update_frame(dt, engine, brake,
  109.      var khm = absSpeed * 3.6;
  110.      var wheels = absSpeed / (this.wheelRadius * (2 * Math.PI));
  111.  
  112. -    var gears;
  113. -    if (this.speed() >= 0) {
  114. -        gears = this.forwardGears;
  115. -    } else {
  116. -        gears = this.reverseGears;
  117. +    //automatic gear shifting
  118. +    var rpm = wheels * this.gear.ratio * this.finalRatio * 60;
  119. +    if (rpm < 1 && this.gear.index != 0) {
  120. +        this.gear = this.neutroGear;
  121. +        this.direction = 0;
  122. +//         this.log_inf("neutro");
  123. +    }
  124. +    if (engine != 0.0 && sign(engine) != this.direction) {
  125. +        this.direction = sign(engine);
  126. +        if (this.direction == 1.0) {
  127. +            this.gear = this.forwardGears[0];
  128. +//             this.log_inf("forward, gear " + this.gear.index);
  129. +        } else if (this.direction == -1.0) {
  130. +            this.gear = this.reverseGears[0];
  131. +//             this.log_inf("reverse, gear " + this.gear.index);
  132. +        }
  133. +        rpm = wheels * this.gear.ratio * this.finalRatio * 60;
  134.      }
  135.  
  136. -    var rpm = wheels * gears[this.gear].ratio * this.finalRatio * 60;
  137. -
  138. -    //automatic gear shifting
  139. -    var currentGear = gears[this.gear];
  140. -    if (rpm > currentGear.shiftUp && this.gear < gears.length - 1) {
  141. -        this.gear += 1;
  142. -    } else if (rpm < currentGear.shiftDown && this.gear > 0) {
  143. -        this.gear -= 1;
  144. +    if (rpm > this.gear.shiftUp && this.gear.next) {
  145. +        this.gear = this.gear.next;
  146. +//         this.log_inf("gear up " + this.gear.index);
  147. +    } else if (rpm < this.gear.shiftDown && this.gear.prev) {
  148. +        this.gear = this.gear.prev;
  149. +//         this.log_inf("gear down " + this.gear.index);
  150.      }
  151.  
  152. -    var force = 0;
  153. -    if (this.maximumSpeed < 0 || absSpeed < this.maximumSpeed) {
  154. -        force = engine * this.torque(rpm) * this.torqueMultiplier;
  155. +    rpm = wheels * this.gear.ratio * this.finalRatio * 60;
  156. +
  157. +    if (this.maximumSpeed < 0 || this.speed < this.maximumSpeed) {
  158. +        var force = engine * this.torque(rpm) * this.torqueMultiplier * this.gear.ratio;
  159. +        this.wheel_force(2, force);
  160. +        this.wheel_force(3, force);
  161.      }
  162. -    this.wheel_force(2, force);
  163. -    this.wheel_force(3, force);
  164.  
  165. +    //From Torcs engine code
  166. +    var static_friction = 0.1;
  167. +    var engineBrake = this.maxPowerTP.torque * this.engineBrakeCoefficient * this.torqueMultiplier * this.gear.ratio *
  168. +                      (static_friction + (1.0 - static_friction) * rpm / this.maxRPM);
  169.  
  170.      brake *= BF;
  171. -    this.wheel_brake(-1, brake);
  172. +    this.wheel_brake(-1, brake + engineBrake);
  173.  
  174.      this.animate_wheels();
  175.  
  176. @@ -165,7 +222,7 @@ function update_frame(dt, engine, brake,
  177.      }
  178.  
  179.      if (this.sound > 0) {
  180. -        var pitch = rpm / 5000.0;
  181. +        var pitch = this.pitchMultiplier * rpm / this.maxRPM;
  182.          this.snd.set_pitch(0, pitch + this.basepitch[this.sound]);
  183.      }
  184.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement