Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --- bmwM3.js.old 2012-11-17 22:51:49.644171426 +0100
- +++ bmwM3.js 2012-11-21 22:34:29.511794340 +0100
- @@ -31,29 +31,66 @@ function init_chassis(){
- //engine properties -- modify these to change the engine behaviour
- this.wheelRadius = 0.24;
- - this.torquePoints = [ { rpm: 0, torque: 220},
- - { rpm: 1000, torque: 265},
- - { rpm: 2000, torque: 290},
- - { rpm: 3000, torque: 315},
- - { rpm: 4000, torque: 335},
- - { rpm: 4900, torque: 365},
- - { rpm: 6000, torque: 340},
- - { rpm: 7000, torque: 320},
- - { rpm: 8000, torque: 290},
- - { rpm: 8100, torque: 0 } ];
- -
- - this.forwardGears = [ { ratio: 4.20, shiftUp: 7000, shiftDown: -1 },
- - { ratio: 2.49, shiftUp: 7000, shiftDown: 4000 },
- - { ratio: 1.66, shiftUp: 7000, shiftDown: 4000 },
- - { ratio: 1.24, shiftUp: 7000, shiftDown: 4000 },
- - { ratio: 1.00, shiftUp: -1, shiftDown: 4000 } ];
- + this.torquePoints = [ { rpm: 0, torque: 180},
- + { rpm: 1000, torque: 250},
- + { rpm: 2000, torque: 335},
- + { rpm: 3000, torque: 380},
- + { rpm: 4000, torque: 400},
- + { rpm: 5000, torque: 395},
- + { rpm: 6000, torque: 400},
- + { rpm: 7000, torque: 390},
- + { rpm: 8000, torque: 365},
- + { rpm: 9000, torque: 0 } ];
- +
- + this.forwardGears = [ { ratio: 4.20, shiftUp: 8000, shiftDown: -1 },
- + { ratio: 2.49, shiftUp: 8000, shiftDown: 4500 },
- + { ratio: 1.66, shiftUp: 8000, shiftDown: 5000 },
- + { ratio: 1.24, shiftUp: 8000, shiftDown: 6000 },
- + { ratio: 1.00, shiftUp: -1, shiftDown: 6000 } ];
- this.reverseGears = [ { ratio: 4.20, shiftUp: -1, shiftDown: -1 } ];
- this.finalRatio = 3;
- - this.torqueMultiplier = 10;
- + this.torqueMultiplier = 6;
- this.maximumSpeed = -1; // speed in m/s. a value < 0 means there's no maximum speed
- + this.engineBrakeCoefficient = 0.2;
- + this.pitchMultiplier = 1.8;
- //end of engine properties
- + this.maxPowerTP = this.torquePoints[0];
- + this.maxRPM = 0;
- + var maxPw = 0;
- + for (var i = 1; i < this.torquePoints.length; ++i) {
- + var tp = this.torquePoints[i];
- + if (tp.rpm * tp.torque > maxPw) {
- + this.maxPowerTP = tp;
- + }
- + if (tp.rpm > this.maxRPM) {
- + this.maxRPM = tp.rpm;
- + }
- + }
- +
- + this.neutroGear = { ratio: 0.0, shiftUp: -1, shiftDown: -1, index: 0 };
- + var prev = null;
- + for (var i = 0; i < this.forwardGears.length; ++i) {
- + var gear = this.forwardGears[i];
- + if (prev)
- + prev.next = gear;
- + gear.prev = prev;
- + gear.index = i + 1;
- + prev = gear;
- + }
- +
- + prev = null;
- + for (var i = 0; i < this.reverseGears.length; ++i) {
- + var gear = this.reverseGears[i];
- + if (prev)
- + prev.next = gear;
- + gear.prev = prev;
- + gear.index = -i - 1;
- + prev = gear;
- + }
- +
- +
- this.torque = engineTorque;
- return {mass:1500, steering:2.0, steering_ecf:60, centering: 2.6, centering_ecf:20};
- @@ -67,7 +104,8 @@ function init_vehicle() {
- this.snd.set_ref_distance(0, 9.0);
- this.started = 0;
- - this.gear = 0;
- + this.gear = this.neutroGear;
- + this.direction = 0;
- }
- //invoked when engine starts or stops
- @@ -112,6 +150,10 @@ function engineTorque(rpm) {
- return rpm * a + Tm - Rm * a;
- }
- +function sign(x) {
- + return (x > 0.0 ? 1 : (x < 0.0 ? -1 : 0));
- +}
- +
- //invoked each frame to handle the inputs and animate the model
- function update_frame(dt, engine, brake, steering) {
- if (this.started != 1)
- @@ -124,33 +166,48 @@ function update_frame(dt, engine, brake,
- var khm = absSpeed * 3.6;
- var wheels = absSpeed / (this.wheelRadius * (2 * Math.PI));
- - var gears;
- - if (this.speed() >= 0) {
- - gears = this.forwardGears;
- - } else {
- - gears = this.reverseGears;
- + //automatic gear shifting
- + var rpm = wheels * this.gear.ratio * this.finalRatio * 60;
- + if (rpm < 1 && this.gear.index != 0) {
- + this.gear = this.neutroGear;
- + this.direction = 0;
- +// this.log_inf("neutro");
- + }
- + if (engine != 0.0 && sign(engine) != this.direction) {
- + this.direction = sign(engine);
- + if (this.direction == 1.0) {
- + this.gear = this.forwardGears[0];
- +// this.log_inf("forward, gear " + this.gear.index);
- + } else if (this.direction == -1.0) {
- + this.gear = this.reverseGears[0];
- +// this.log_inf("reverse, gear " + this.gear.index);
- + }
- + rpm = wheels * this.gear.ratio * this.finalRatio * 60;
- }
- - var rpm = wheels * gears[this.gear].ratio * this.finalRatio * 60;
- -
- - //automatic gear shifting
- - var currentGear = gears[this.gear];
- - if (rpm > currentGear.shiftUp && this.gear < gears.length - 1) {
- - this.gear += 1;
- - } else if (rpm < currentGear.shiftDown && this.gear > 0) {
- - this.gear -= 1;
- + if (rpm > this.gear.shiftUp && this.gear.next) {
- + this.gear = this.gear.next;
- +// this.log_inf("gear up " + this.gear.index);
- + } else if (rpm < this.gear.shiftDown && this.gear.prev) {
- + this.gear = this.gear.prev;
- +// this.log_inf("gear down " + this.gear.index);
- }
- - var force = 0;
- - if (this.maximumSpeed < 0 || absSpeed < this.maximumSpeed) {
- - force = engine * this.torque(rpm) * this.torqueMultiplier;
- + rpm = wheels * this.gear.ratio * this.finalRatio * 60;
- +
- + if (this.maximumSpeed < 0 || this.speed < this.maximumSpeed) {
- + var force = engine * this.torque(rpm) * this.torqueMultiplier * this.gear.ratio;
- + this.wheel_force(2, force);
- + this.wheel_force(3, force);
- }
- - this.wheel_force(2, force);
- - this.wheel_force(3, force);
- + //From Torcs engine code
- + var static_friction = 0.1;
- + var engineBrake = this.maxPowerTP.torque * this.engineBrakeCoefficient * this.torqueMultiplier * this.gear.ratio *
- + (static_friction + (1.0 - static_friction) * rpm / this.maxRPM);
- brake *= BF;
- - this.wheel_brake(-1, brake);
- + this.wheel_brake(-1, brake + engineBrake);
- this.animate_wheels();
- @@ -165,7 +222,7 @@ function update_frame(dt, engine, brake,
- }
- if (this.sound > 0) {
- - var pitch = rpm / 5000.0;
- + var pitch = this.pitchMultiplier * rpm / this.maxRPM;
- this.snd.set_pitch(0, pitch + this.basepitch[this.sound]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement