Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

really really heavily commented vehicle script

jester Knox
Sculpter of Water
Join date: 22 Apr 2004
Posts: 204
06-27-2005 10:51
i decided to make a flying car a bit back, but even with a few sample scripts i couldnt figure out how to make it fly like i wanted it to. mostly because i didnt know what i was doing or what all the functions wee supposed to be doing. so i started from scratch and wrote a new script with descriptions of what each function does and what that means. i hope this helps anyone else working on vehicles.

if you use this script it will fly reasonable well as it is, more of a hover type flight than an airplane, but needs quite a bit of tweaking before i would say it flies really well.

if there is some grievous error somewhere in it please let me know, im not the best scripter and this was my first attempt at a vehicle.

CODE

// heavily commented script by: jester Knox
// please IM him with any problems, corrections, or improvements you find.
// if you use this for a vehicle you wish to sell, or you just think is really cool
// please send me a copy, i really like vehicles.
// charging for this script is forbidden. it is meant to be a learning tool to
// be freely passed along.

vector linear = <0, 0, 0> ; // declare the value for the linear motor
vector angular = <0, 0, 0> ; // declare the value for the angular motor



default
{
state_entry()
{



// what the pie menu says if you want to get in
llSetSitText("Drive") ;

// hearing collision sounds when on the ground would suck, so we mute them.
llCollisionSound("", 0.0) ;

// where the AV sits (this assumes that the root prim is the drivers seat)
llSitTarget(<0.1, 0.0, -0.70>, ZERO_ROTATION) ;

// readjust the camera so the driver can see
llSetCameraEyeOffset(<-10.0, 0.0, 2.0> ) ;
llSetCameraAtOffset(<3.0, 0.0, 1.0> ) ;

// define the vehicle as a vehicle and what kind it will be
// possibilities inclide:
// VEHICLE_TYPE_SLED
// VEHICLE_TYPE_CAR
// VEHICLE_TYPE_BOAT
// VEHICLE_TYPE_AIRPLANE
// VEHICLE_TYPE_BALLOON
// if this script is unmodified from its original form the settings
// and controls are assuming a flying craft.
llSetVehicleType(VEHICLE_TYPE_AIRPLANE) ;

// linear friction: friction on a straight line x,y,z planes.
// or how long it will take to stop moving on each axis
// (not a measure of time as far as i can tell).
// higher numbers give less friction. max value = 1000
llSetVehicleVectorParam(VEHICLE_LINEAR_FRICTION_TIMESCALE, <100.0, 100.0, 100.0>) ;

// angular friction: resistance to turning
// or how easy it is to twist, and how long it will take to level off
// (not a measure of time as far as i can tell).
// higher numbers give less friction. max value = 1000
llSetVehicleFloatParam(VEHICLE_ANGULAR_FRICTION_TIMESCALE, 100.0) ;

//linear motor: as motor implies the push that makes your craft go

// the linear velocity that the craft attempt to achieve. max = 40
llSetVehicleVectorParam(VEHICLE_LINEAR_MOTOR_DIRECTION, <100.0, 100.0, 100.0>) ;
// the time it takes to reach top speed min = 0.06
llSetVehicleFloatParam(VEHICLE_LINEAR_MOTOR_TIMESCALE, 1.0) ;
// time during which the motor's effectiveness exponentially decays max = 120
llSetVehicleFloatParam(VEHICLE_LINEAR_MOTOR_DECAY_TIMESCALE, 2.0) ;

// angular motor: motor that twists the path of the craft

// angular velocity (radians/sec) that the vehicle will try to rotate.
// max = 4*PI (2 revolutions/sec)
llSetVehicleVectorParam(VEHICLE_ANGULAR_MOTOR_DIRECTION, <100.0, 100.0, 100.0>) ;
// the time it takes to be spinning at full speed. min = 0.06
llSetVehicleFloatParam(VEHICLE_ANGULAR_MOTOR_TIMESCALE, 1.0) ;
// time during which the spin exponentially decays max = 120
llSetVehicleFloatParam(VEHICLE_ANGULAR_MOTOR_DECAY_TIMESCALE, 2) ;


// time for craft to stabilize at hover height
// **values greater than 300 disable hover**
llSetVehicleFloatParam(VEHICLE_HOVER_TIMESCALE, 360.0) ;
// height to hover at. what craft will use as the base height
// to base this from depends on the flags set
// VEHICLE_FLAG_HOVER_WATER_ONLY
//| VEHICLE_FLAG_HOVER_TERRAIN_ONLY
//| VEHICLE_FLAG_HOVER_GLOBAL_HEIGHT
// we will set these (or not) later
llSetVehicleFloatParam(VEHICLE_HOVER_HEIGHT, 0.0) ;
// min = 0.0 max = 1.0
// 0.0 = bouncey 1.0 = smooth
// how tightly the craft adheres to the hover height
// timescale will be the oscillation period of the bounce.
llSetVehicleFloatParam(VEHICLE_HOVER_EFFICIENCY, 0.0) ;
// 0 = normal phsycs apply
// 1 = anti-gravity, the force of gravity will not apply
// -1 = adds extra gravity to craft
llSetVehicleFloatParam(VEHICLE_BUOYANCY, 0.98) ;



// linear deflection: any push from any angle will attempt to be
// redirected along the axis that the craft wishes to go (forward)
// i.e. a car will want to go in the direction its wheels are facing

// time till craft moves in direction of outside push
llSetVehicleFloatParam(VEHICLE_LINEAR_DEFLECTION_TIMESCALE, 1.0) ;
// no deflection = (0.0) max deflection = 1.0
llSetVehicleFloatParam(VEHICLE_LINEAR_DEFLECTION_EFFICIENCY, 0.5) ;



// angular deflection: an outside push will attempt to reorient craft
// to match direction of push.
// i.e. a collision from the side will turn the craft to point
// the same direction as the impact was going.

// time till craft twists to match direction of outside push
llSetVehicleFloatParam(VEHICLE_ANGULAR_DEFLECTION_TIMESCALE, 100.0) ;
// no deflection = (0.0) max deflection = 1.0
llSetVehicleFloatParam(VEHICLE_ANGULAR_DEFLECTION_EFFICIENCY, 0.25) ;


// vertical attraction: keeps the top side up
// i.e. the x axis of the craft is locked to the world x axis

// set above 300 to disable
llSetVehicleFloatParam(VEHICLE_VERTICAL_ATTRACTION_TIMESCALE, 1.0) ;
// min = 0.0 = wobbly max = 1.0 = steady
llSetVehicleFloatParam(VEHICLE_VERTICAL_ATTRACTION_EFFICIENCY, 0.75) ;
// * Note that by default the vertical attractor will prevent the craft
// * from diving and climbing. So, if you wanted to make a airplane you
// * would probably want to set the VEHICLE_FLAG_LIMIT_ROLL_ONLY flag
// * to allow pitch (nose up/down)

// amount of lean during turns
// min = -1 lean out of turns max = 1 lean into turns
llSetVehicleFloatParam(VEHICLE_BANKING_EFFICIENCY, 0.1) ;
// min = 0 = static max = 1 = dynamic
// i have no clear idea what this does, in some way it effects
// the craft banking while stopped and moving
llSetVehicleFloatParam(VEHICLE_BANKING_MIX, 1.0) ;
// time to reach the full tilt of the banking
llSetVehicleFloatParam(VEHICLE_BANKING_TIMESCALE, 360.0) ;

// 2 possible uses.
// if your root prim is *not* <0,0,0> you can use this to orient your
// craft correctly
// or if you need to change the orentation of your craft.
// i.e. to tilt up on end for a take off/landing mode.
// typically you should not need to set this to anything
llSetVehicleRotationParam(VEHICLE_REFERENCE_FRAME, <0.0, 0.0, 0.0, 1.0>) ;


// flags to set special attributes or behaiviors to the craft

// VEHICLE_FLAG_NO_DEFLECTION_UP Prevents linear deflection parallel to the
// world-z axis. or in plain english linear deflection will not push up
// VEHICLE_FLAG_LIMIT_ROLL_ONLY Removes vertical attraction for changes in
// vehicle pitch allows craft that use vertical attraction to nose up/down
// VEHICLE_FLAG_HOVER_WATER_ONLY Hover only pays attention to water level
// hover keeps craft at a fixed height above the water height (ignoring terrain height)
// VEHICLE_FLAG_HOVER_TERRAIN_ONLY Hover only pays attention to terrain height
// hover keeps craft at a fixed height above the terrain height (ignoring water height)
// VEHICLE_FLAG_HOVER_GLOBAL_HEIGHT Hover only pays attention to global height
// hover at global altitude ignoring terrain and water levels
// VEHICLE_FLAG_HOVER_UP_ONLY Hover only pushes up
// hover will not push down, hover height will be the lowest the craft can go
// VEHICLE_FLAG_LIMIT_MOTOR_UP Prevents ground vehicles from motoring into the sky
// keeps ground craft from flying up or steering midair
//
// VEHICLE_FLAG_MOUSELOOK_STEER Makes vehicle try to turn toward mouselook direction
// VEHICLE_FLAG_MOUSELOOK_BANK Makes vehicle try to turn toward mouselook
// direction assuming banking is enabled
// VEHICLE_FLAG_CAMERA_DECOUPLED Causes the camera look-at axis to NOT
// move when the vehicle rotates


// flags we are not applying to this craft.
// may be unneccessary unless craft will change flags when entering different modes
// but seems like a good idea o have them anyway. if the craft will change modes
// i.e. enter a staet to be a car not an airplane then you would remove the airplane
// type flags and add car type ones.
llRemoveVehicleFlags(VEHICLE_FLAG_NO_DEFLECTION_UP
| VEHICLE_FLAG_HOVER_WATER_ONLY
| VEHICLE_FLAG_HOVER_TERRAIN_ONLY
| VEHICLE_FLAG_LIMIT_MOTOR_UP
| VEHICLE_FLAG_LIMIT_ROLL_ONLY
|VEHICLE_FLAG_HOVER_GLOBAL_HEIGHT
| VEHICLE_FLAG_HOVER_UP_ONLY) ;


// flags we are applying to this craft
//llSetVehicleFlags(VEHICLE_FLAG_HOVER_GLOBAL_HEIGHT
// | VEHICLE_FLAG_HOVER_UP_ONLY) ;

// keeps craft from moving on rez before driver/pilot is seated
llSetStatus(STATUS_PHYSICS, FALSE) ;
}


// now that the charistics of the craft's handeling are set we need to
// add the ability to controll the craft

on_rez(integer param)
{
llResetScript();
}


// checks to make sure that the craft owner is the the driver/pilot
// by seeing if the links have changed. an AV sitting on something changes
// the link status and we are detecting that here.
changed(integer change)
{
if (change & CHANGED_LINK)
{
// get the key of the AV sitting down
key agent = llAvatarOnSitTarget() ;
if (agent)
{
if (agent != llGetOwner())
{
// only the owner can use this craft. if you are not the owner
// the craft will say...
llSay(0, "You aren't the owner -- only the owner can drive this craft.") ;
// then stand you up
llUnSit(agent) ;
// and give you a bit of a shove out of the craft
llPushObject(agent, <0,0,10>, ZERO_VECTOR, FALSE) ;
}
else
{
// if you are the owner you are permitted to sit.
// and we reset the linear motor to keep the craft at a stop.
linear = <0, 0, 0> ;
llSetVehicleVectorParam(VEHICLE_LINEAR_MOTOR_DIRECTION, linear) ;


llSetVehicleFloatParam(VEHICLE_LINEAR_FRICTION_TIMESCALE, 100.0) ;
llSetVehicleFloatParam(VEHICLE_ANGULAR_FRICTION_TIMESCALE, 100.0) ;
// gets permission to make the driver/pilot use the sit anim and
// take over the controls
llRequestPermissions(agent, PERMISSION_TRIGGER_ANIMATION | PERMISSION_TAKE_CONTROLS) ;
}
}
else
{
// if the drive/pilot stands up we want to stop the craft so
// it dosent wander off.
linear = <0, 0, 0> ;
llSetVehicleVectorParam(VEHICLE_LINEAR_MOTOR_DIRECTION, linear) ;
llSetVehicleVectorParam(VEHICLE_ANGULAR_MOTOR_DIRECTION, linear) ;
// angular isnt linear, but its one less vector we need to define

// here we use friction to stop the vehicle very quickly
// rather than llSetStatus(STATUS_PHYSICS, FALSE) ; which
// will work but be far more abrupt.
llSetVehicleFloatParam(VEHICLE_LINEAR_FRICTION_TIMESCALE, 1.0) ;
llSetVehicleFloatParam(VEHICLE_ANGULAR_FRICTION_TIMESCALE, 1.0) ;

// driver/pilot is no longer at the controls, so we stop acting on them
llReleaseControls() ;
// and we dont need them sitting anymore, as they are standing up
llStopAnimation("sit") ;
// and we make the craft non-phsycal to keep it from being moved away.
// this also cuts down the load on the sim if you wish to park somewhere.
llSetStatus(STATUS_PHYSICS, FALSE) ;
}
}

}



// script grants self permissions to act on control inputs
run_time_permissions(integer permissions)
{
if (permissions & PERMISSION_TAKE_CONTROLS)
{
// these are all the possible controls to act on
llTakeControls( CONTROL_FWD
| CONTROL_BACK
| CONTROL_LEFT
| CONTROL_RIGHT
| CONTROL_ROT_LEFT
| CONTROL_ROT_RIGHT
| CONTROL_UP
| CONTROL_DOWN
| CONTROL_LBUTTON
| CONTROL_ML_LBUTTON,
TRUE, FALSE) ;
// what are true and false doing here?
// the first one is refering to accepting controlls,
// you want this on in most cases.
// the second value is pass_on. what that means is that
// it is asking if the commands are sent to the AV also
// or just the vehicle. if CONTROL_ML_LBUTTON is used
// it will make a mouselook button appere on the bottom
// of the screen, but only if the second true/false is
// set to false.

// turn physics on so we can move
llSetStatus(STATUS_PHYSICS, TRUE) ;
}
}

// if each control is pressed, what the craft's responce is.
control(key name, integer level, integer edge)
{


// there are 4 different states of each controll button
// it can be either up or pressed down
// or being pressed or realesed
// yes there is a difference.
// i.e. if the up arrow means go forward and held down means go
// then as long as the key is held down it will go and when it is realesed
// then that command will stop.
// if pressed means go then if you tap the up arrow it will go untill
// there is another command to tell it not to.
// or in other words edge fwd can mean go
// not edge fwd can mean stop
// then you will only go fwd when the button is down
//
// sorta, its confusing
//
// level means the button is down
// !level (not level) means the button is up
// edge means the button is being pressed or realesed
// !edge (not edge) means the button has not been touched
// so the act of perssing and then releasing the button would be
// edge & level, !edge & level, edge & !level, !edge & !level
//
// that may be an over simplification, and may be misleading.




// it should be noted that i didn't put any speed limits in the script in
// the interest of clarity and keeping things simple to follow.
// that gives the craft a max speed of about 40 m/s.
// 40 m/s is usually too fast for an enjoyable ride, but fast can be lots of fun.


// when the up arrow or w is pressed
if (level & CONTROL_FWD)
{
// if we are moving backwards stop
if (linear.x < 0)
{
linear.x = 0 ;
}
// otherwise go forward
else {linear.x += 14.0 ; }
}

// when the up arrow or w is released
else if ((edge & ~level & CONTROL_FWD))
{
linear.x = 0 ;
}

// when the down arrow or s is pressed
if (level & CONTROL_BACK)
{
// if we are moving forward stop
if (linear.x > 0)
{
linear.x = 0 ;
}
// otherwise go backwards
else
{
linear.x -= 14.0 ;
}
}

// when the down arrow or s is released
else if ((edge & ~level & CONTROL_BACK))
{
linear.x = 0 ;
}
// when shift and the left arrow or shift and a is pressed
if (level & CONTROL_LEFT)
// strafe left
{
linear.y += 8.0 ;
}

// when shift and the left arrow or shift and a is released
else if (!(level & CONTROL_LEFT))
// stop strafeing left
{
linear.y = 0 ;
}

// when shift and the right arrow or shift and d is pressed
if (level & CONTROL_RIGHT)
// strafe right
{
linear.y -= 8.0 ;
}

// when shift and the right arrow or shift and d is released
else if (!(level & CONTROL_RIGHT))
// stop strafe right
{
linear.y = 0 ;
}

// when the left arrow or a is pressed
if (level & CONTROL_ROT_LEFT)
// turn left
// tilt slightly
{
angular.z += (PI / 180) * 55.0 ;
// turn
angular.x -= PI * 4 ;
}

// when the left arrow or a is released
else if ((edge & ~level & CONTROL_ROT_LEFT))
// stop turning
// level off
{
angular.z = 0 ;
// stop turning
angular.x = 0 ;
}

// when the right arrow or d is pressed
if (level & CONTROL_ROT_RIGHT)
// turn right
// tilt slightly
{
angular.z -= (PI / 180) * 55.0 ;
// turn
angular.x += PI * 4 ;
}

// when the right arrow or d is released
else if ((edge & ~level & CONTROL_ROT_RIGHT))
// stop turning
// level off
{
angular.z = 0 ;
// stop turning
angular.x = 0 ;
}

// when page up or e is pressed
if (level & CONTROL_UP)
// move up
{
linear.z += 48.0 ;
}

// when page up or e is released
else if (!(level & CONTROL_UP))
// stop moving up
{
linear.z = 0 ;
}

// when page down or c is pressed
if (level & CONTROL_DOWN)
// move down
{
linear.z -= 24.0 ;
}
// when page down or c is released
else if ((edge & ~level & CONTROL_DOWN))
// stop moveing down
{
linear.z = 0 ;
}

// the following commented out for lack of a good use in my basic example
// however these are valid controlls and usefull in many kids of craft.

// when the left mouse button is pressed while in mouselook
//if ((level & CONTROL_ML_LBUTTON) && (edge & CONTROL_ML_LBUTTON))
//llOwnerSay ("CONTROL_ML_LBUTTON - pressed");
// when the left mouse button is released while in mouselook
//if (!(level & CONTROL_ML_LBUTTON) && (edge & CONTROL_ML_LBUTTON))
//llOwnerSay ("CONTROL_ML_LBUTTON - released");

// when the left mouse button is pressed
//if ((level & CONTROL_LBUTTON) && (edge & CONTROL_LBUTTON))
//llOwnerSay ("CONTROL_LBUTTON - pressed");
// when the left mouse button is released
//if (!(level & CONTROL_LBUTTON) && (edge & CONTROL_LBUTTON))
//llOwnerSay ("CONTROL_LBUTTON - released");

// allows us to alter the speed based on the pilot/driver's input
llSetVehicleVectorParam(VEHICLE_LINEAR_MOTOR_DIRECTION, linear);
// lets us turn
llSetVehicleVectorParam(VEHICLE_ANGULAR_MOTOR_DIRECTION, angular);
}
}


also feel free to IM me with feedback or for a copy of the script



jester

.
_____________________
people tell me my fountains are cool, come check them out at JesAma Fountains, Alviso (190,45) or if you arent in SL try Gigas (secondserver.net) or SLBoutique to shop for my products.
Nada Epoch
The Librarian
Join date: 4 Nov 2002
Posts: 1,423
Discussion thread
06-30-2005 13:09
/54/96/52097/1.html
_____________________
i've got nothing. ;)
Nodge Nico
Registered User
Join date: 29 Jun 2005
Posts: 15
question
06-30-2005 19:38
how do i get all that text into the game do i have to type all that???
Jim Perhaps
Registered User
Join date: 10 Dec 2005
Posts: 65
Left Strafe Not Working
01-23-2008 07:04
I have downloaded this twice and the left strafe does not work. Right strafe works fine. Anyone have any idea why?

Thanks
Jim