Outerra forum

Please login or register.

Login with username, password and session length
Advanced search  

News:

Outerra Tech Demo download. Help with graphics driver issues

Pages: 1 2 [3] 4 5 ... 8

Author Topic: Vehicle script [version 1.4.0]  (Read 59742 times)

M7

  • Hero Member
  • *****
  • Posts: 736
  • newbie
Re: Vehicle script [version 1.2.1]
« Reply #30 on: May 11, 2013, 12:42:44 am »

Hehe! Are you going to animate all those gears? I can post the sketchup file if you want.

Don't know but Lego should pay someone to have that car animated. It could  make them a good promo to have it on youtube.
Logged

ZeosPantera

  • ||>>-Z-<<||
  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2520
  • #1 Outerra Fan Boy
    • My Youtube
Re: Vehicle script [version 1.2.1]
« Reply #31 on: May 11, 2013, 12:58:47 am »

Move all the gears.
Logged
"Fear accompanies the possibility of death, Calm shepherds its certainty" - General Ka Dargo

ZeosPantera

  • ||>>-Z-<<||
  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2520
  • #1 Outerra Fan Boy
    • My Youtube
Re: Vehicle script [version 1.2.1]
« Reply #32 on: May 11, 2013, 01:35:33 am »



Joy.
Logged
"Fear accompanies the possibility of death, Calm shepherds its certainty" - General Ka Dargo

PytonPago

  • Hero Member
  • *****
  • Posts: 2284
  • It´s way too complex, dont let me try to explain !
Re: Vehicle script [version 1.2.1]
« Reply #33 on: May 11, 2013, 04:15:36 am »

That Lego model is wonderful, but i think a HUD showing the RPM may be more effective ;)

Quote
Quote from: M7
Sorry the model has too many vertices , more than 65535 says the error message. which seems to be the limit

Just found this line   ...  dont like that number 

Actually, meshes have the 65535 limit, not models. Models can have more than one mesh.

... yes, i know the difference - but still made me check my meshes trough ... at the end, i found out, that Blender makes fun of me, showing the overall number of the model even if i select a single mesh - till edit mode. :D
« Last Edit: May 11, 2013, 04:18:37 am by PytonPago »
Logged
We are still undeveloped as long as we don´t realize, that all our science is still descriptive, and than beyond that description lies a whole new world we just haven´t even started to fully understand.

M7

  • Hero Member
  • *****
  • Posts: 736
  • newbie
Re: Vehicle script [version 1.2.1]
« Reply #34 on: May 11, 2013, 11:48:16 am »

Move all the gears.

After the gears animation , i'm just thinking of the possibilities for crash animation on this thing, kind of like Keva planks destruction animation with bulltet physics. The meshes would not need to twist but every piece could come apart depending on the force of the impact. I don't know, that would be so cool to crash in a lego car.
Logged

GHAO

  • Member
  • **
  • Posts: 82
  • Thinking
Re: Vehicle script [version 1.2.1]
« Reply #35 on: May 11, 2013, 04:13:07 pm »

Reminds of a game on the Lego site itself where you drive that car around a course full of jumps - you have to do enough tricks to pass the level. Anyone want to make a video of that car barrelling round the mountains?
Logged

M7

  • Hero Member
  • *****
  • Posts: 736
  • newbie
Re: Vehicle script [version 1.2.1]
« Reply #36 on: May 15, 2013, 12:46:22 am »

Am i the only who getting this with the latest BMW version? First spawn, everything is fine, car drive perfectly. But if i try to spawn another one, it will just hang in the air (just like when you first import a vehicle).

Same kind of thing happen when loading a Belair. First spawn, everything is fine, second one, the engine will start but then go silent and it wont move.



Logged

cameni

  • Brano Kemen
  • Outerra Administrator
  • Hero Member
  • *****
  • Posts: 6721
  • No sense of urgency.
    • outerra.com
Re: Vehicle script [version 1.2.1]
« Reply #37 on: May 15, 2013, 01:07:49 am »

That is a bug in the vehicle script, some global properties attached only to 'this' of the first vehicle in init_chassis. I've posted a fixed version somewhere, but every vehicle that was derived from the original script needs to be fixed.
Logged

Chaoz

  • Full Member
  • ***
  • Posts: 156
  • newbie
Re: Vehicle script [version 1.2.1]
« Reply #38 on: May 15, 2013, 01:35:50 am »

//vehicle script file
//see http://xtrac.outerra.com/index.fcgi/wiki/vehicle for example and documentation

var chassis={};

//invoked only the first time the model is loaded, or upon reload
function init_chassis(){
    var wheelparam = {
        radius: 0.42,
        width: 10,
        suspension_max: 0.1,
        suspension_min: -0.1,
        suspension_stiffness: 20.0,
        damping_compression: 0.5,
        damping_relaxation: 0.2,
        slip: 5,
        roll_influence: 0.01,
        rotation: -1
             };
   var wheelparam2 = {
        radius: 0.64,
        width: 100,
        suspension_max: 0.1,
        suspension_min: -0.3,
        suspension_stiffness: 15.0,
        damping_compression: 0.5,
        damping_relaxation: 0.2,
        slip: 5,
        roll_influence: 0.01,
        rotation: 1
             };
  this.add_wheel('AV_G', wheelparam);
  this.add_wheel('AV_D', wheelparam);
  this.add_wheel('AR_G', wheelparam2);
  this.add_wheel('AR_D', wheelparam2);
 
    this.load_sound("EngineStart.ogg"); //startup
    this.load_sound("EngineIdle.ogg"); //idle
    this.load_sound("EngineOff.ogg");    //off
    this.load_sound("EngineLow.ogg"); //on
   
    chassis.basepitch = [ 0.2, 0.6, 0.6, 0.4 ];             //base pitch for start, idle, throttle
  this.add_sound_emitter("AR_G");
    //engine properties -- modify these to change the engine behaviour
    chassis.wheelRadius = 0.53;

    chassis.torquePoints = [ { rpm: 0,    torque: 180},
                          { rpm: 1000, torque: 120},
                          { rpm: 2000, torque: 80},
                          { rpm: 3000, torque: 60},
                          { rpm: 4000, torque: 20},
                          { rpm: 5000, torque: 0},
                          { rpm: 6000, torque: 0},
                          { rpm: 7000, torque: 0},
                          { rpm: 8000, torque: 0},
                          { rpm: 9000, torque: 0  } ];
   
    chassis.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 } ];
    chassis.reverseGears = [ { ratio: 4.20, shiftUp: -1, shiftDown: -1 } ];
   
    chassis.finalRatio = 1;
    chassis.torqueMultiplier = 40;
    chassis.maximumSpeed = -1; // speed in m/s. a value < 0 means there's no maximum speed
    chassis.engineBrakeCoefficient = 0.2;
    chassis.breakingForce = 50000.0;
    chassis.pitchMultiplier = 1;
    //end of engine properties

    chassis.maxPowerTP = chassis.torquePoints[0];
    chassis.maxRPM = 0;
    var maxPw = 0;
    for (var i = 1; i < chassis.torquePoints.length; ++i) {
        var tp = chassis.torquePoints;
        if (tp.rpm * tp.torque > maxPw) {
            chassis.maxPowerTP = tp;
        }
        if (tp.rpm > chassis.maxRPM) {
            chassis.maxRPM = tp.rpm;
        }
    }

    chassis.neutroGear = { ratio: 0.0, shiftUp: -1, shiftDown: -1, index: 0 };
    var prev = null;
    for (var i = 0; i < chassis.forwardGears.length; ++i) {
        var gear = chassis.forwardGears;
        if (prev)
            prev.next = gear;
        gear.prev = prev;
        gear.index = i + 1;
        prev = gear;
    }

    prev = null;
    for (var i = 0; i < chassis.reverseGears.length; ++i) {
        var gear = chassis.reverseGears;
        if (prev)
            prev.next = gear;
        gear.prev = prev;
        gear.index = -i - 1;
        prev = gear;
    }


    chassis.torque = engineTorque;

    return {mass:1500, com:{y:0, z:-0.82}, steering:10, steering_ecf:60, centering: 30.6, centering_ecf: 40};
}

//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 = false;
    this.gear = chassis.neutroGear;
    this.direction = 0;
}

//invoked when engine starts or stops
function engine(start) {
    if (start) {
        this.started = true;
        this.sound = 0;
        this.snd.play(0, 0, false, false);
    }
}

function engineTorque(rpm) {
    var min = 0;
    var max = chassis.torquePoints.length - 1;

    if (rpm > chassis.torquePoints[max].rpm || rpm < chassis.torquePoints[min].rpm)
        return 0;

    while (max - min > 1) {
        var mid = Math.floor(min + (max - min) / 2);
        var tp = chassis.torquePoints[mid];
        if (tp.rpm == rpm) {
            return tp.torque;
        } else if (tp.rpm > rpm) {
            max = mid;
        } else {
            min = mid;
        }
    }

    var minTp = chassis.torquePoints[min];
    var maxTp = chassis.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;
}

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)
        return;

    if (this.sound == 0 && this.snd.is_playing(0)) { // still starting up
        return;
    }

  steering *= 0.2;
  this.steer(0, steering);
  this.steer(1, steering);

    var absSpeed = Math.abs(this.speed());
    var khm = absSpeed * 3.6;
    var wheels = absSpeed / (chassis.wheelRadius * (2 * Math.PI));

    //automatic gear shifting
    var rpm = wheels * this.gear.ratio * chassis.finalRatio * 60;
    if (rpm < 10 && this.gear.index != 0) {
        this.gear = chassis.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 = chassis.forwardGears[0];
//             this.log_inf("forward, gear " + this.gear.index);
        } else if (this.direction == -1.0) {
            this.gear = chassis.reverseGears[0];
//             this.log_inf("reverse, gear " + this.gear.index);
        }
        rpm = wheels * this.gear.ratio * chassis.finalRatio * 60;
    }

    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);
    }

    rpm = wheels * this.gear.ratio * chassis.finalRatio * 60;

    if (chassis.maximumSpeed < 0 || this.speed < chassis.maximumSpeed) {
        var force = engine * chassis.torque(rpm) * chassis.torqueMultiplier * this.gear.ratio;
        this.wheel_force(0, force*0.5);
        this.wheel_force(1, force*0.5);
        this.wheel_force(2, force);
        this.wheel_force(3, force);
    }

    //From Torcs engine code
    var static_friction = 0.1;
    var engineBrake = chassis.maxPowerTP.torque * chassis.engineBrakeCoefficient * chassis.torqueMultiplier * this.gear.ratio *
                      (static_friction + (1.0 - static_friction) * rpm / chassis.maxRPM);

    brake *= chassis.breakingForce;
    this.wheel_brake(-1, brake + engineBrake);

    this.animate_wheels();

    if (this.gear.index == 0 && (!this.snd.is_playing(0) || this.sound != 1)) {
        this.snd.stop(0);
        this.snd.play(0, 1, true, false);
        this.sound = 1;
    } else if (this.gear.index != 0 && engine == 0 && (!this.snd.is_playing(0) || this.sound != 2)) { //idle
        this.snd.stop(0);
        this.snd.play(0, 2, true, false);
        this.sound = 2;
    } else if (this.gear.index != 0 && engine != 0 && (!this.snd.is_playing(0) || this.sound != 3)) { //throttle
        this.snd.stop(0);
        this.snd.play(0, 3, true, false);
        this.sound = 3;
    }

    var pitch = chassis.pitchMultiplier * rpm / chassis.maxRPM;
    this.snd.set_pitch(0, pitch + chassis.basepitch[this.sound]);
}


This is a fixed script from another vehicle, notice the var chassis at the beginning...
Logged

giucam

  • Full Member
  • ***
  • Posts: 171
  • It's an ugly pile of bones... like me.
Re: Vehicle script [version 1.2.1]
« Reply #39 on: May 15, 2013, 11:35:34 am »

That doesn't happen to me, and i can't read the line number of the error in the screenshot, i guess due of high compression.
The index property is of gears though, i don't see what the "var chassis={};" would do. I guess it is using a null gear variable for some reason.
Logged
ResidualVM 0.1.1 is OUT! www.residualvm.org

Chaoz

  • Full Member
  • ***
  • Posts: 156
  • newbie
Re: Vehicle script [version 1.2.1]
« Reply #40 on: May 15, 2013, 11:40:24 am »

"chassis" is an array that is global and not like "this" locally, so if you put something in "chassis" then every car can access it, like the gear tables. With "this" it's only possible to have one car functioning at the same time.
Logged

cameni

  • Brano Kemen
  • Outerra Administrator
  • Hero Member
  • *****
  • Posts: 6721
  • No sense of urgency.
    • outerra.com
Re: Vehicle script [version 1.2.1]
« Reply #41 on: May 15, 2013, 11:41:26 am »

'this' in init_chassis is for the first vehicle, so whatever custom property you attach there is available only to the first vehicle instance. The chassis global object was added instead, to have these accessible from every vehicle instance.
Logged

giucam

  • Full Member
  • ***
  • Posts: 171
  • It's an ugly pile of bones... like me.
Re: Vehicle script [version 1.2.1]
« Reply #42 on: May 15, 2013, 11:51:38 am »

Ah i see, thanks. I'll work on it
Logged
ResidualVM 0.1.1 is OUT! www.residualvm.org

giucam

  • Full Member
  • ***
  • Posts: 171
  • It's an ugly pile of bones... like me.
Re: Vehicle script [version 1.2.2]
« Reply #43 on: May 21, 2013, 03:43:13 pm »

Version 1.2.2.

Code: [Select]
/*
    Vehicle script file
    Copyright (C) 2012 - 2013  Giulio Camuffo <giuliocamuffo@gmail.com>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public
    License as published by the Free Software Foundation; either
    version 3.0 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

//see http://xtrac.outerraworld.com/trac.fcgi/wiki/vehicle for example and documentation

// VERSION 1.2.2

var chassis = {};

//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: 5.0,
        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("e90_startup.ogg"); //startup
    this.load_sound("e90_onidle.ogg"); //idle
    this.load_sound("e90_offlow.ogg"); //off
    this.load_sound("e90_onlow.ogg"); //on
    chassis.basepitch = [ 1, 1, 0.4, 0.4 ];             //base pitch for start, idle, off, on
    this.add_sound_emitter("wheel_FL");

    //engine properties -- modify these to change the engine behaviour
    chassis.wheelRadius = 0.24;

    chassis.torquePoints = [ { rpm: 200,  torque: 0},
                             { 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  } ];

    chassis.forwardGears = [ { ratio: 4.06, shiftUp: 8000, shiftDown: -1   },
                             { ratio: 2.40, shiftUp: 8000, shiftDown: 4500 },
                             { ratio: 1.58, shiftUp: 8000, shiftDown: 5000 },
                             { ratio: 1.19, shiftUp: 8000, shiftDown: 5500 },
                             { ratio: 1.00, shiftUp: 8000, shiftDown: 6000 },
                             { ratio: 0.87, shiftUp: -1,   shiftDown: 6000 } ];
    chassis.reverseGears = [ { ratio: 4.68, shiftUp: -1, shiftDown: -1 } ];

    chassis.differentialRatio = 3.85;
    chassis.efficiency = 0.9;
    chassis.speedLimiter = -1; // maximum speed in m/s. a value < 0 means there's no limiter
    chassis.engineBrakeCoefficient = 0.4;
    chassis.breakingForce = 5000.0;
    chassis.pitchMultiplier = 1.5;
    chassis.engineInertia = 0.05;
    chassis.minimumRPM = 1500;
    chassis.minimum = 0.3;
    //end of engine properties

    chassis.maxPowerTP = chassis.torquePoints[0];
    chassis.maxRPM = 0;
    var maxPw = 0;
    for (var i = 1; i < chassis.torquePoints.length; ++i) {
        var tp = chassis.torquePoints[i];
        if (tp.rpm * tp.torque > maxPw) {
            chassis.maxPowerTP = tp;
        }
        if (tp.rpm > chassis.maxRPM) {
            chassis.maxRPM = tp.rpm;
        }
    }

    chassis.neutroGear = { ratio: 0.0, shiftUp: -1, shiftDown: -1, index: 0 };
    var prev = null;
    for (var i = 0; i < chassis.forwardGears.length; ++i) {
        var gear = chassis.forwardGears[i];
        if (prev)
            prev.next = gear;
        gear.prev = prev;
        gear.index = i + 1;
        prev = gear;
    }

    prev = null;
    for (var i = 0; i < chassis.reverseGears.length; ++i) {
        var gear = chassis.reverseGears[i];
        if (prev)
            prev.prev = gear;
        gear.next = prev;
        gear.index = -i - 1;
        prev = gear;
    }

    return {mass:1570, steering:1.0, steering_ecf:10000, centering: 2.0, centering_ecf: 10};
}

var ClutchMode = { Manual: 0, Automatic: 1, Mixed: 2 };
var ClutchState = { Engaged: 0, Disengaged: 1, Engaging: 2, Disengaging: 3 };

//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.automaticTransmission = false;
    this.clutchMode = ClutchMode.Automatic;

    this.started = false;
    this.gear = chassis.neutroGear;
    this.direction = 0;
    this.engineRPM = 0;
    this.starting = false;
    this.clutch = 1.0;
    this.clutchState = ClutchState.Disengaged;
    this.clutchOverride = false;

    this.torque = engineTorque;
    this.shiftUp = shift_up;
    this.shiftDown = shift_down;
    this.selectGear = select_gear;
    this.die = die;
    this.updateAutomaticTransmission = automatic_transmission;
    this.automaticClutch = automatic_clutch;
    this.animate = animate;
}

//invoked when engine starts or stops
function engine(start) {
    if (start) {
        this.started = false;
        this.soundId = 0;
        this.starting = true;
    } else {
        this.started = false;
    }
}

function engineTorque(rpm) {
    var min = 0;
    var max = chassis.torquePoints.length - 1;

    if (rpm > chassis.torquePoints[max].rpm || rpm < chassis.torquePoints[min].rpm)
        return 0;

    while (max - min > 1) {
        var mid = Math.floor(min + (max - min) / 2);
        var tp = chassis.torquePoints[mid];
        if (tp.rpm == rpm) {
            return tp.torque;
        } else if (tp.rpm > rpm) {
            max = mid;
        } else {
            min = mid;
        }
    }

    var minTp = chassis.torquePoints[min];
    var maxTp = chassis.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;
}

function sign(x) {
    return (x > 0.0 ? 1 : (x < 0.0 ? -1 : 0));
}

function action(key, value, dt)
{
    if (key == AAuxb1 && value == 0) {
        this.automaticTransmission = !this.automaticTransmission;
        this.log_inf("Automatic transmission " + (this.automaticTransmission ? "enabled" : "disabled"));
    }

    if (key == AAuxb2) {
        if (this.clutchMode != ClutchMode.Manual) {
            this.clutchState = (value == 0 ? ClutchState.Disengaging : ClutchState.Engaging);
        } else {
            this.clutchState = (value == 0 ? ClutchState.Disengaged : ClutchState.Engaged);
            this.clutch = 1 - value;
        }
        this.clutchOverride = value;
    }

    if (value == 1 && !this.automaticTransmission) {
        switch(key) {
            case AShiftUp:
                this.shiftUp();
                break;
            case AShiftDown:
                this.shiftDown();
                break;
            default:
                break;
        }
    }
}

function select_gear(gear)
{
    var i = this.gear.index;
    this.gear = gear;
    if (i == 0 && this.clutchMode == ClutchMode.Mixed && this.clutchState != ClutchState.Engaged) {
        this.clutchState = ClutchState.Disengaging;
    }
    this.log_inf("gear " + i + " => " + this.gear.index);
}

function shift_up()
{
    var i = this.gear.index;
    if (this.gear.next) {
        this.selectGear(this.gear.next);
    } else if (i == 0) {
        this.selectGear(chassis.forwardGears[0]);
    } else if (i == -1) {
        this.selectGear(chassis.neutroGear);
    }
}

function shift_down()
{
    var i = this.gear.index;
    if (this.gear.prev) {
        this.selectGear(this.gear.prev);
    } else if (i == 0) {
        this.selectGear(chassis.reverseGears[0]);
    } else if (i == 1) {
        this.selectGear(chassis.neutroGear);
    }
}

function die()
{
    this.started = false;
}

function automatic_transmission(engine)
{
    if (this.engineRPM < 10 && this.gear.index != 0) {
        this.gear = chassis.neutroGear;
        this.direction = 0;
        this.selectGear(chassis.neutroGear);
    }
    if (engine != 0.0) {
        if (sign(engine) != this.direction) {
            this.direction = sign(engine);
            if (this.direction == 1.0) {
                this.selectGear(chassis.forwardGears[0]);
            } else if (this.direction == -1.0) {
                this.selectGear(chassis.reverseGears[0]);
            }
        }

        if (this.engineRPM > this.gear.shiftUp && this.gear.next) {
            this.shiftUp();
        } else if (this.engineRPM < this.gear.shiftDown && this.gear.prev) {
            this.shiftDown();
        }
    }
}

function automatic_clutch(wheelsRpm)
{
    var clutched = (this.clutchState == ClutchState.Disengaged  || this.clutchState == ClutchState.Disengaging) && this.gear.index != 0;
    var clutchRPM = chassis.minimumRPM + 2000;

    if (this.clutchMode == ClutchMode.Automatic && !this.clutchOverride) {
        if (clutched && this.engineRPM < chassis.minimumRPM) {
            this.clutchState = ClutchState.Engaging;
        } else if (!clutched && this.engineRPM > clutchRPM) {
            this.clutchState = ClutchState.Disengaging;
        }
    }

    if (this.clutchState == ClutchState.Disengaging) {
        if (this.gear.index != 0) {
            var w = wheelsRpm * this.gear.ratio * chassis.differentialRatio;
            this.clutch = (w > chassis.minimumRPM ? chassis.minimumRPM : w) / chassis.minimumRPM;

            // This makes an horizontal S-like shape. (mirrored, like Z)
            var f = this.clutch * 2;
            if (f < 0.5) {
                this.clutch = -(f * f * f * f - 1) / 5;
            } else {
                f = f - 1;
                this.clutch = (f * f * f * f)  + 0.2;
            }
            this.clutch += 0.2;

            var r = this.engineRPM / clutchRPM;
            this.clutch *= (r > 1 ? 1 : (r < 0 ? 0 : r));
        } else {
            this.clutch = 1;
        }

        if (this.clutch >= 1) {
            this.clutch = 1;
            this.clutchState = ClutchState.Disengaged;
        }
    } else if (this.clutchState == ClutchState.Engaging) {
        this.clutch = 0;
        this.clutchState = ClutchState.Engaged;
    }
    return this.clutch;
}

function animate()
{
    this.animate_wheels();
}

//invoked each frame to handle the inputs and animate the model
function update_frame(dt, engine, brake, steering) {
    steering *= 0.6;
    this.steer(-2, steering);

    var wheelsRpm = this.max_rpm();
    if (sign(this.speed()) != sign(this.gear.index) && this.gear.index != 0) {
        wheelsRpm = 0;
    }

    if (this.automaticTransmission) {
        this.updateAutomaticTransmission(engine);
    }
    if (engine < 0) {
        engine = -engine;
    }

    var clutch = this.clutch;
    if (this.gear.index == 0) {
        clutch = 0;
    }
    if (this.clutchMode != ClutchMode.Manual) {
        clutch = this.automaticClutch(wheelsRpm);
    }

    var appliedTorque = 0;
    if (this.starting) {
        if (this.engineRPM > chassis.minimumRPM) {
            this.starting = false;
            this.started = true;
        } else {
            appliedTorque = 100 * (1 - this.engineRPM / chassis.minimumRPM);
        }
    }

    var oldRPM = this.engineRPM;

    if (this.gear.index != 0) {
        this.engineRPM = this.engineRPM * (1 - clutch) + wheelsRpm * clutch * this.gear.ratio * chassis.differentialRatio;
    }
this.log_inf(this.engineRPM);
    var engineStarted = this.started;
    if (engineStarted || this.starting) {
        if (this.engineRPM < chassis.minimumRPM && engine < chassis.minimum) {
            engine = chassis.minimum;
        }
        if (chassis.speedLimiter < 0 || Math.abs(this.speed()) < chassis.speedLimiter) {
            appliedTorque += engine * this.torque(this.engineRPM);
        }
    }

    var friction = 0.2;
    appliedTorque -= sign(this.engineRPM) * chassis.maxPowerTP.torque * chassis.engineBrakeCoefficient *
                          (friction + (1.0 - friction) * this.engineRPM / chassis.maxRPM);

    this.engineRPM += 2 * appliedTorque * dt / chassis.engineInertia; // torque*2 because of the mean below
    this.engineRPM = (this.engineRPM + oldRPM) / 2.0; // stabilize the fluctuations when the wheelsRpm and engineRPM
                                                      // are very different
    if (this.engineRPM < 300) {
        this.die();
    }

    var tq = appliedTorque + chassis.engineInertia * (oldRPM - this.engineRPM) / dt;
    var force = tq * clutch * this.gear.ratio * chassis.differentialRatio * chassis.efficiency / chassis.wheelRadius;
    if (this.gear.index < 0) {
        force = -force;
    }

    this.wheel_force(2, force / 2);
    this.wheel_force(3, force / 2);

    brake *= chassis.breakingForce;
    this.wheel_brake(-1, brake);

    this.animate();

    if (this.gear.index == 0 && (!this.snd.is_playing(0) || this.soundId != 1)) {
        this.snd.stop(0);
        this.snd.play(0, 1, true, false);
        this.soundId = 1;
    } else if (this.gear.index != 0 && engine != 1 && (!this.snd.is_playing(0) || this.soundId != 2)) { //idle
        this.snd.stop(0);
        this.snd.play(0, 2, true, false);
        this.soundId = 2;
    } else if (this.gear.index != 0 && engine == 1 && (!this.snd.is_playing(0) || this.soundId != 3)) { //throttle
        this.snd.stop(0);
        this.snd.play(0, 3, true, false);
        this.soundId = 3;
    }

    var pitch = chassis.pitchMultiplier * (this.engineRPM - chassis.minimumRPM) / chassis.maxRPM;
    this.snd.set_pitch(0, pitch + chassis.basepitch[this.soundId]);
    var g = this.engineRPM / chassis.minimumRPM * 2.0;
    this.snd.set_gain(0, Math.min(g * g * g, 1.0));
}

Changelog:
  • Fixes spawning of multiple cars
  • Improves the clutch: has a Automatic mode witch is, well, automatic, a Manual mode that is fully manual and a Mixed mode which is only partially automatic: when you release the clutch it will not immediately fully connect the engine to the wheels but it will do it gently over some time, but it will not do anything if the engine RPM is below the minimum and it is dieing.
    You can engage/disengage the clutch by using the second auxiliary button.


cameni, I tried to use AAuxa[1,2] with the clutch, using my wheel's brake or accelerator, but i didn't succed. It seems that when i set it as primary for AAuxa1 it behaves like a button, increasing or decreasing the value i get in the action handler, but i couldn't manage to make it work as an axis. Setting it as secondary didn't work at all, i didn't receive anything for it.
Logged
ResidualVM 0.1.1 is OUT! www.residualvm.org

cameni

  • Brano Kemen
  • Outerra Administrator
  • Hero Member
  • *****
  • Posts: 6721
  • No sense of urgency.
    • outerra.com
Re: Vehicle script [version 1.2.2]
« Reply #44 on: May 21, 2013, 03:56:31 pm »

Axis handling will be changed because currently it's a bit limited, I already modified it in the dev version.
Logged
Pages: 1 2 [3] 4 5 ... 8