Thanks a lot ! I now got it working after a day of desperate trials. Though to get the sound looping forever I had to do some other modifications as well - copying from the script of the BMW model provided with the new outerra version. Just one thing now i don't know how to stop the sound when switching off the engine. I'll figure out.
Here is the working script for reference:
/
var EF = 3000.0;
var BF = 13000.0;
var ST = 0.7
var maxkmh = 150;
var forceloss = EF / (0.2*maxkmh + 1);
var Wh_rad = 0.45
var CarMass = 1120
var WHEEL_FR = "WHEEL-FR"
var WHEEL_FL = "WHEEL-FL"
var WHEEL_RR = "WHEEL-RR"
var WHEEL_RL = "WHEEL-RL"
var STEERING_WHEEL = "STEER-WHEEL"
var steerAngle
var swheel_id
var _snd = -1;
var _snd_src = -1;
var soundfile = "drive.ogg" //<-- sound file
var rpm_src_on = 0;
//=============================================================================================
function init_chassis()
{
var wheelparam = {
radius: Wh_rad,
width: 0.2,
suspension_max: 0.1,
suspension_min: -0.12,
suspension_stiffness: 30.0,
damping_compression: 0.4,
damping_relaxation: 0.12,
grip: 0.5,
roll_influence: 0.1};
this.add_wheel(WHEEL_FR,wheelparam);
this.add_wheel(WHEEL_FL,wheelparam);
this.add_wheel(WHEEL_RR,wheelparam);
this.add_wheel(WHEEL_RL,wheelparam);
this.geom = this.get_geomob(0)
swheel_id = this.geom.get_joint(STEERING_WHEEL);
_snd = this.load_sound(soundfile);
_snd_src = this.add_sound_emitter(WHEEL_FR );
_snd_on = this.load_sound(soundfile);
_src_on = this.add_sound_emitter(WHEEL_FR );
return {mass:CarMass, steering:2.0, steering_ecf:60, centering: 9.6, centering_ecf:1};
//return {mass:CarMass, steering:2.0, steering_ecf:60, centering: 2.6, centering_ecf:20};
}
//------------------------------------------------------------------------------------------------
function init_vehicle(){
this.set_fps_camera_pos({x:-0.39,y:-0.50,z:1.50});
this.snd = this.sound();
this.snd.set_ref_distance(0, 9.0);
this.snd.set_ref_distance(_src_on, 9.0);
this.started = 0;
}
//-----------------------------------------------------------------
function engine(start){
if(start) {
this.snd.play(_snd_src, _snd, false, false);
this.started=1;
}
}
//==================================================================
function update_frame(dt, engine, brake, steering)
{
var kmh = this.speed()*3.6;
var redux = engine>=0 ? 0.2 : 0.6;
var esign = engine<0 ? -1 : 1;
engine = EF*Math.abs(engine);
var force = (esign>0) == (kmh>=0)
? engine/(redux*Math.abs(kmh) + 1)
: engine;
force -= forceloss;
force = Math.max(0.0, Math.min(force, engine));
engine = esign*force;
brake *= BF;
steering *= ST;
this.steer(-2, steering); // -2 = front wheels used for steering
steerAngle = steering *= 2.05;
this.geom.rotate_joint_orig(swheel_id, steerAngle, {x:0,y:0,z:1});
this.wheel_force(-1, engine);
this.wheel_brake(-11, brake);
this.animate_wheels();
if(this.started==1 && !this.snd.is_playing(0)) {
this.started=2;
this.snd.play_loop(_src_on, _snd_on);
}
else if(this.started==2) {
var pitch = Math.abs(kmh)/20.0;
var g = kmh>0 ? Math.floor(pitch) : 0;
var f = pitch - g;
f += 0.5*g;
this.snd.set_pitch(_src_on, 0.5*f + 1.0);
}
}