Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //vehicle script file
- //see http://xtrac.outerra.com/index.fcgi/wiki/vehicle for example and documentation
- var sound = -1;
- //invoked only the first time the model is loaded, or upon reload
- function init_chassis(){
- var wheelparam = {
- radius: 0.30,
- width: 0.20,
- suspension_max: 0.1,
- suspension_min: -0.05,
- suspension_stiffness: 50.0,
- damping_compression: 0.06,
- damping_relaxation: 0.05,
- slip: 2.2,
- roll_influence: 0.1,
- rotation: -1
- };
- this.add_wheel('wheel_FL', wheelparam);
- this.add_wheel('wheel_FR', wheelparam);
- this.add_wheel('wheel_RL', wheelparam);
- this.add_wheel('wheel_RR', wheelparam);
- this.load_sound("diesel-engine-start.ogg"); //start
- this.load_sound("18L16V_onidle_ex.ogg"); //idle
- this.load_sound("18L16V_onverylow_in.ogg"); //throttle
- this.basepitch = [ 0, 1, 0.7 ]; //base pitch for start, idle, throttle
- this.add_sound_emitter("wheel_FL");
- //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.reverseGears = [ { ratio: 4.20, shiftUp: -1, shiftDown: -1 } ];
- this.finalRatio = 3;
- this.torqueMultiplier = 10;
- this.maximumSpeed = -1; // speed in m/s. a value < 0 means there's no maximum speed
- //end of engine properties
- this.torque = engineTorque;
- return {mass:1500, steering:2.0, steering_ecf:60, centering: 2.6, centering_ecf:20};
- }
- //invoked for each new instance of the vehicle
- function init_vehicle() {
- this.set_fps_camera_pos({x:-0.33,y:-0.25,z:1.15});
- this.snd = this.sound();
- this.snd.set_ref_distance(0, 9.0);
- this.started = 0;
- this.gear = 0;
- }
- //invoked when engine starts or stops
- function engine(start) {
- if (start) {
- this.started = 1;
- this.sound = 0;
- this.snd.play(0, 0, false, false);
- }
- }
- const BF = 4000.0;
- function engineTorque(rpm) {
- var min = 0;
- var max = this.torquePoints.length - 1;
- if (rpm > this.torquePoints[max].rpm || rpm < this.torquePoints[min].rpm)
- return 0;
- while (max - min > 1) {
- var mid = Math.floor(min + (max - min) / 2);
- var tp = this.torquePoints[mid];
- if (tp.rpm == rpm) {
- return tp.torque;
- } else if (tp.rpm > rpm) {
- max = mid;
- } else {
- min = mid;
- }
- }
- var minTp = this.torquePoints[min];
- var maxTp = this.torquePoints[max];
- var TM = maxTp.torque;
- var Tm = minTp.torque;
- var RM = maxTp.rpm;
- var Rm = minTp.rpm;
- var a = (TM - Tm) / (RM - Rm);
- return rpm * a + Tm - Rm * a;
- }
- //invoked each frame to handle the inputs and animate the model
- function update_frame(dt, engine, brake, steering) {
- if (this.started != 1)
- return;
- steering *= 0.6;
- this.steer(-2, steering);
- var absSpeed = Math.abs(this.speed());
- 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;
- }
- 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;
- }
- var force = 0;
- if (this.maximumSpeed < 0 || absSpeed < this.maximumSpeed) {
- force = engine * this.torque(rpm) * this.torqueMultiplier;
- }
- this.wheel_force(2, force);
- this.wheel_force(3, force);
- brake *= BF;
- this.wheel_brake(-1, brake);
- this.animate_wheels();
- if (engine == 0 && ((this.sound == 0 && !this.snd.is_playing(0)) || this.sound == 2)) { //idle
- this.snd.stop(0);
- this.snd.play(0, 1, true, false);
- this.sound = 1;
- } else if (engine != 0 && ((this.sound == 0 && !this.snd.is_playing(0)) || this.sound == 1)) { //throttle
- this.snd.stop(0);
- this.snd.play(0, 2, true, false);
- this.sound = 2;
- }
- if (this.sound > 0) {
- var pitch = rpm / 5000.0;
- this.snd.set_pitch(0, pitch + this.basepitch[this.sound]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement