Car Code

Expired

float theta_func()
{
    if (speed <1) return 0.02;
    if (speed < 4) return 0.1;
    if (speed < 6) return .4;
    if (speed < 10) return .6;
    if (speed < 15) return .5;
    if (speed < 20) return .5;
    if (speed < 25) return .4;
     return .3;
    
}  

float power_curve()
{
    if (speed < 1) return 15.0;
    if (speed < 4) return 15.0;
    if (speed < 6) return 15.0;
    if (speed < 10) return 15.0;
    if (speed < 15) return 10.0;
    if (speed < 20) return 4.0;
    if (speed < 30) return 2.0;
    return 2.0;
}
 

float THROTTLE = 1.2;
float STEERING = 1;
float TIMER = .05;
float ANGULAR_TAU = .1;            // decrease for faster turn
float ANGULAR_DAMPING = 3;        // not sure what this should be yet
float THETA_INCREMENT = 0.05;        // increase for bigger turn response


//float C_FRICTION = 40;
//float FRONTAL_AREA = 20;
//float TOT_FRICTION;


float mass;
float speed;
vector velocity;


float turn;
float counter = 0;
float brake = 1;
// linear constants


integer a = 10;


integer LEVELS = 0;                 // we'll set this later

// global variables
vector gTargetPos;
float gTargetTheta;
key gToucher;


default
{
    state_entry()
    {
        llSay(0,"car starting");
        llSetStatus(STATUS_PHYSICS, FALSE);
        llSetStatus(STATUS_ROTATE_X | STATUS_ROTATE_Y, FALSE);
        llCollisionFilter("", "", TRUE);
        llStopSound();
        mass = llGetMass();
        
        
        llSleep(0.1);
        //SetHoverHeight(1,1,10);
        llSetStatus(STATUS_ROTATE_X | STATUS_ROTATE_Y | STATUS_ROTATE_Z , TRUE);
        
        llMoveToTarget(llGetPos() + <0,0,1>, 0);
        llRotLookAt(llGetRot(), 0, 0);
        
        
        LEVELS = CONTROL_FWD | CONTROL_BACK | CONTROL_ROT_LEFT | CONTROL_ROT_RIGHT | CONTROL_UP | CONTROL_DOWN | CONTROL_LEFT | CONTROL_RIGHT;
    }
    
    
    touch_start(integer total_number)
    {
        
        llStopSound();
        gToucher = llDetectedKey(0);
        integer perm = llGetPermissions();
        
        
        if (perm)
        {
            llReleaseControls();
        }
        else
        {
            state StateDriving;
        }
    }
}

state StateDriving
{
    state_entry()
    {
        llSetTimerEvent(TIMER);
        brake = 1;
        
        llSetStatus(STATUS_PHYSICS, TRUE);
        llSetStatus(STATUS_ROTATE_X | STATUS_ROTATE_Y | STATUS_ROTATE_Z , TRUE);
        llWhisper(0, "StateDriving");
        //llSetHoverHeight(a,1,.4);
        llRequestPermissions(gToucher, PERMISSION_TAKE_CONTROLS);
        //llMoveToTarget(llGetPos(), LINEAR_TAU);
        llRotLookAt(llGetRot(), ANGULAR_TAU, 1.0);        
    }
    touch_start(integer total_number)
    {
        llWhisper(0, "Touched.");
        integer perm = llGetPermissions();
        if (perm)
        {
            llReleaseControls();
        }
        state default;
    }
    run_time_permissions(integer perm)
    {
        if (perm == PERMISSION_TAKE_CONTROLS)
        {
            llTakeControls(LEVELS, TRUE, FALSE);        }
        else
        {
             state default;
        }
    }
    control(key driver, integer levels, integer edges)
    {
//llWhisper(0, "l = " + (string)levels + "  e = " + (string)edges);
        // linear control
        //llSetHoverHeight(a,1,.4);
        
        integer nudge = FALSE;
        float direction = 0;
        gTargetTheta *= theta_func();
        
        
        if (levels & CONTROL_FWD)
        {
            direction = 1;
            brake = 0;
        }
        if (levels & CONTROL_BACK)
        {
            direction = -.1;
            brake = 5;
            gTargetTheta *= 2;
        }
        if (levels & CONTROL_LEFT)
        {
            
        }
        if (levels & CONTROL_RIGHT)
        {
           
        }
        if (levels & CONTROL_UP)
        {
            a +=1;
        }
        if (levels & CONTROL_DOWN)
        {
           a-=1;
        }
        // angular control
        if (levels & CONTROL_ROT_LEFT)
        {
            if (gTargetTheta <0)
            {
                gTargetTheta = 0;
            }
            else
            {
                gTargetTheta += THETA_INCREMENT;
            }
            nudge = TRUE;
        }
        if (levels & CONTROL_ROT_RIGHT)
        {
            if (gTargetTheta > 0)
            {
                gTargetTheta = 0;
            }
            else
            {
                gTargetTheta -= THETA_INCREMENT;
            }
            nudge = TRUE;
        }
        
        if (direction)
        {
            //sound control
            if (speed <24) { llLoopSound("car accel", .4);}
            else { llLoopSound("car go",.4); }
            
            //applies forward boost
            
           // llSay( 0, (string) power + "  " + (string) direction + "  " + (string) mass + (string) power_curve() + (string) theta_func() );
            
            
            llApplyImpulse( <1,0,0> * direction * mass * power_curve()* THROTTLE , 1);
            
        }
    
        if (direction == 0)
        
        {llLoopSound("geardown", .4);
        }
        
        if (nudge)
        {
                
            // angular motion -- we compute the raw quaternion about z-axis
            counter++;
            float st = 0.5 * llSin(gTargetTheta*STEERING);
            float ct = 0.5 * llCos(gTargetTheta*STEERING);
            rotation rot = <0,0,st, ct> * llGetRot(); 
            llRotLookAt(rot, ANGULAR_TAU, ANGULAR_DAMPING);
        }
        
        else {counter = 0;}
    }

land_collision(vector position)
    {
      //llApplyImpulse( <0,0, .5> * mass, 1 );
     }

    
collision (integer total_number)
    {
      // llWhisper( 0, "collision 2");
       //llSetHoverHeight(4,1,.2);
    }
    

timer() //side ways supression
    {
    
      vector pos = llGetPos();
       
     if (llGround(<0,0,0>) > (pos.z - 3) )
    {
        velocity = llGetVel();
        speed = llVecMag(velocity);
        
        float l_speed = llRot2Left( llGetRot() ) * velocity;
        float f_speed = llRot2Fwd( llGetRot() ) * velocity;
      
      
        if (  l_speed > 8 || (brake > 1 && speed > 8) )   llTriggerSound("tires burn",10);
        if (speed <1) llStopSound();
        
        
        if (counter * speed > 100)
        { 
         l_speed /= 4;
         brake = 2;
        }
        if (speed > 4) brake = 2;
        f_speed *= brake;
        
      
        llApplyImpulse( <0,-1,0> * l_speed * mass *.8, 1 );
        llApplyImpulse( <-1,0,0> * f_speed * mass *.05, 1 );
       }
     
    }
        
}

 

Cubey Terra's Airplane Script

Expired

This is the main script which needs to go in the Pilots seat:

 

// Cubey Terra DIY Plane 1.0
// September 10, 2005

// Distribute freely. Go ahead and modify it for your own uses. Improve it and share it around.
// Some portions based on the Linden airplane script originall posted in the forums.


// llSitTarget parameters:
vector avPosition = <0.0,0.0,-0.6>; // position of avatar in plane, relative to parent
rotation avRotation = <0,-0.2,0,1>; // rotation of avatar, relative to parent

// Thrust variables:
// thrust = fwd * thrust_multiplier
// Edit the maxThrottle and thrustMultiplier to determine max speed.
integer fwd; // this changes when pilot uses throttle controls
integer maxThrottle = 10; // Number of "clicks" in throttle control. Arbitrary, really.
integer thrustMultiplier = 3; // Amount thrust increases per click.

// Lift and speed
float cruiseSpeed = 25.0; // speed at which plane achieves full lift
float lift;
float liftMax = 0.977; // Maximum allowable lift
float speed;

float timerFreq = 0.5;

integer fwdLast; 
integer sit;
key owner;

default 
{
    
    state_entry() 
    { 
        llSitTarget(avPosition, avRotation); // Position and rotation of pilot's avatar.
        
        llSetCameraEyeOffset(<-7, 0, 1.5>); // Position of camera, relative to parent. 
        llSetCameraAtOffset(<0, 0, 1.9>);   // Point to look at, relative to parent.
        

        llSetSitText("Fly"); // Text that appears in pie menu when you sit

        llCollisionSound("", 0.0); // Remove the annoying thump sound from collisions  
 
        //SET VEHICLE PARAMETERS -- See www.secondlife.com/badgeo for an explanation
        llSetVehicleType(VEHICLE_TYPE_AIRPLANE);
             
        // uniform angular friction 
        llSetVehicleFloatParam( VEHICLE_ANGULAR_FRICTION_TIMESCALE, 2 );
        
        // linear motor
        llSetVehicleVectorParam( VEHICLE_LINEAR_FRICTION_TIMESCALE, <200 , 20, 20> );
        llSetVehicleVectorParam( VEHICLE_LINEAR_MOTOR_DIRECTION, <0, 0, 0> );
        llSetVehicleFloatParam( VEHICLE_LINEAR_MOTOR_TIMESCALE, 2 );
        llSetVehicleFloatParam( VEHICLE_LINEAR_MOTOR_DECAY_TIMESCALE, 120 );
        
        // agular motor
        llSetVehicleVectorParam( VEHICLE_ANGULAR_MOTOR_DIRECTION, <0, 0, 0> );
        llSetVehicleFloatParam( VEHICLE_ANGULAR_MOTOR_TIMESCALE, 0 ); 
        llSetVehicleFloatParam( VEHICLE_ANGULAR_MOTOR_DECAY_TIMESCALE, 0.4);
        
        // hover 
        llSetVehicleFloatParam( VEHICLE_HOVER_HEIGHT, 0 );
        llSetVehicleFloatParam( VEHICLE_HOVER_EFFICIENCY, 0 ); 
        llSetVehicleFloatParam( VEHICLE_HOVER_TIMESCALE, 10 );
        llSetVehicleFloatParam( VEHICLE_BUOYANCY, .977 );
        
        // no linear deflection 
        llSetVehicleFloatParam( VEHICLE_LINEAR_DEFLECTION_EFFICIENCY, 0.2 );
        llSetVehicleFloatParam( VEHICLE_LINEAR_DEFLECTION_TIMESCALE, 0.5 ); 
        
        // no angular deflection 
        llSetVehicleFloatParam( VEHICLE_ANGULAR_DEFLECTION_EFFICIENCY, 1);
        llSetVehicleFloatParam( VEHICLE_ANGULAR_DEFLECTION_TIMESCALE, 0.6);
            
        // no vertical attractor 
        llSetVehicleFloatParam( VEHICLE_VERTICAL_ATTRACTION_EFFICIENCY, 0.8 );
        llSetVehicleFloatParam( VEHICLE_VERTICAL_ATTRACTION_TIMESCALE, 2 );
        
        /// banking
        llSetVehicleFloatParam( VEHICLE_BANKING_EFFICIENCY, 1);
        llSetVehicleFloatParam( VEHICLE_BANKING_MIX, .85);
        llSetVehicleFloatParam( VEHICLE_BANKING_TIMESCALE, 0.01);
        
        
        // default rotation of local frame
        llSetVehicleRotationParam( VEHICLE_REFERENCE_FRAME, <0,0,0,1>);
        
        // remove these flags 
        llRemoveVehicleFlags( VEHICLE_FLAG_NO_DEFLECTION_UP 
                              | VEHICLE_FLAG_HOVER_WATER_ONLY 
                              | VEHICLE_FLAG_HOVER_TERRAIN_ONLY 
                              | VEHICLE_FLAG_HOVER_GLOBAL_HEIGHT 
                              | VEHICLE_FLAG_HOVER_UP_ONLY 
                              | VEHICLE_FLAG_LIMIT_MOTOR_UP );

        // set these flags 
        llSetVehicleFlags( VEHICLE_FLAG_LIMIT_ROLL_ONLY ); 

    }
    
    on_rez(integer num) 
    {
        llResetScript();
    } 
    

    // DETECT AV SITTING/UNSITTING AND TAKE CONTROLS
    changed(integer change)
    {
       if(change & CHANGED_LINK)
       {
            key agent = llAvatarOnSitTarget();
           
            // Pilot gets off vehicle
            if((agent == NULL_KEY) && (sit))
            {
                llSetStatus(STATUS_PHYSICS, FALSE);
                llSetTimerEvent(0.0);
                llMessageLinked(LINK_SET, 0, "unseated", "");
                llStopSound();
                llReleaseControls();
                sit = FALSE;
            }
            
            // Pilot sits on vehicle
            else if((agent == llGetOwner()) && (!sit))
            {
                llRequestPermissions(agent, PERMISSION_TAKE_CONTROLS | PERMISSION_TRIGGER_ANIMATION);
                llSetTimerEvent(timerFreq);
                llMessageLinked(LINK_SET, 0, "seated", "");
                sit = TRUE;
                llSetStatus(STATUS_PHYSICS, TRUE);
            }
        }
    }


    //CHECK PERMISSIONS AND TAKE CONTROLS
    run_time_permissions(integer perm) 
    {
        if (perm & (PERMISSION_TAKE_CONTROLS)) 
        {            
            llTakeControls(CONTROL_UP | CONTROL_DOWN | CONTROL_FWD | CONTROL_BACK | CONTROL_RIGHT | CONTROL_LEFT | CONTROL_ROT_RIGHT | CONTROL_ROT_LEFT, TRUE, FALSE);
        }
    }
            
            
    //FLIGHT CONTROLS     
    control(key id, integer level, integer edge) 
    {

            integer throttle_up = CONTROL_UP;
            integer throttle_down = CONTROL_DOWN;
            integer yoke_fwd = CONTROL_FWD;
            integer yoke_back = CONTROL_BACK;

            vector angular_motor; 
            
            
            // THROTTLE CONTROL--------------
            if (level & throttle_up) 
            {
                if (fwd  0)
                {
                    fwd -= 1;
                }
            }
            
            if (fwd != fwdLast)
            {
                llSetVehicleVectorParam(VEHICLE_LINEAR_MOTOR_DIRECTION, <(fwd * thrustMultiplier),0,0>);
                
                // calculate percent of max throttle and send to child prims as link message
                float thrustPercent = (((float)fwd/(float)maxThrottle) * 100);
                llMessageLinked(LINK_SET, (integer)thrustPercent, "throttle", "");
                llOwnerSay("Throttle at "+(string)((integer)thrustPercent)+"%");
                
                fwdLast = fwd;
                llSleep(0.15); // crappy kludge :P
            }

            
            // PITCH CONTROL ----------------
            if (level & yoke_fwd) 
            {
                angular_motor.y = 3.0;
            }
            else if (level & yoke_back) 
            {
                angular_motor.y = -3.0;
            }
            else
            {
                angular_motor.y = 0;
            }
            

            // BANKING CONTROL----------------

            if ((level & CONTROL_RIGHT) || (level & CONTROL_ROT_RIGHT)) 
            {
                angular_motor.x = TWO_PI;
            }   
            else if ((level & CONTROL_LEFT) || (level & CONTROL_ROT_LEFT)) 
            {
                angular_motor.x = -TWO_PI;
            }
            else
            {
                angular_motor.x = 0;
            }

            llSetVehicleVectorParam(VEHICLE_ANGULAR_MOTOR_DIRECTION, angular_motor);
            
    } 
    
    timer()
    {
        // Calculate speed in knots
        speed = (integer)(llVecMag(llGetVel()) * 1.94384449 + 0.5);
        
        // Provide lift proportionally to speed
        cruiseSpeed = 25.0; // speed at which you achieve full lift
        lift = (speed/cruiseSpeed); //Lift is varied proportionally to the speed
        if (lift > liftMax) lift = liftMax;
        llSetVehicleFloatParam( VEHICLE_BUOYANCY, lift );
    }

}


 This is for the passenger's seat:

 

default
{
    state_entry()
    {
        llSitTarget(<0.0,0.0,-0.6>, <0,-0.2,0,1>);
    }
}

 

Collision Rocket

Expired

default
{
    state_entry()
    {
        llSetStatus(STATUS_PHYSICS, TRUE);
        llSetForce(<0,0,40>, TRUE);
        llSay(0,"Woosh!");
        llSleep(.1);
        llSetForce(<0,0,0>, TRUE);
    }
    collision_start(integer num)
    {
        llSay(0, "KaBoom!");
        llDie();
    }
}

 

Jump Rocket Script

Expired

default { state_entry() { llSetStatus(STATUS_PHYSICS, TRUE); llSetForce(<0,0,40>, TRUE); llSay(0,"Woosh!"); llSleep(.1); llSetForce(<0,0,0>, TRUE); } }