Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Simple airplane vehicle script

Andrew Linden
Linden staff
Join date: 18 Nov 2002
Posts: 692
11-25-2003 19:50
Here's a simple airplane vehicle script that might be a good place to start. Note, it uses the motorcycle_sit animation, which might not be right for your airplane.

CODE

// simple airplane script example

// assumes that the root primitive is oriented such that its
// local x-axis points toward the nose of the plane, and its
// local z-axis points toward the top

// control flags that we set later
integer gAngularControls = 0;
integer gLinearControls = 0;

// we keep track of angular history for more responsive turns
integer gOldAngularLevel = 0;

// the linear motor uses an accumulator model rather than keeping track
// of the linear control level history
vector gLinearMotor = <0, 0, 0>;

default
{
state_entry()
{
llSetSitText("Fly");
llCollisionSound("", 0.0);

// the sit and camera placement is very shape dependent
// so modify these to suit your vehicle
llSitTarget(<0.6, 0.05, 0.20>, ZERO_ROTATION);
llSetCameraEyeOffset(<-10.0, 0.0, 2.0>);
llSetCameraAtOffset(<3.0, 0.0, 1.0>);

llSetVehicleType(VEHICLE_TYPE_AIRPLANE);

// weak angular deflection
llSetVehicleFloatParam(VEHICLE_ANGULAR_DEFLECTION_EFFICIENCY, 0.1);
llSetVehicleFloatParam(VEHICLE_ANGULAR_DEFLECTION_TIMESCALE, 1.0);

// strong linear deflection
llSetVehicleFloatParam(VEHICLE_LINEAR_DEFLECTION_EFFICIENCY, 1.0);
llSetVehicleFloatParam(VEHICLE_LINEAR_DEFLECTION_TIMESCALE, 0.2);

// somewhat responsive linear motor
llSetVehicleFloatParam(VEHICLE_LINEAR_MOTOR_TIMESCALE, 0.5);
llSetVehicleFloatParam(VEHICLE_LINEAR_MOTOR_DECAY_TIMESCALE, 20);

// somewhat responsive angular motor, but with 3 second decay timescale
llSetVehicleFloatParam(VEHICLE_ANGULAR_MOTOR_TIMESCALE, 0.5);
llSetVehicleFloatParam(VEHICLE_ANGULAR_MOTOR_DECAY_TIMESCALE, 3);

// very weak friction
llSetVehicleVectorParam(VEHICLE_LINEAR_FRICTION_TIMESCALE, <1000.0, 1000.0, 1000.0>);
llSetVehicleVectorParam(VEHICLE_ANGULAR_FRICTION_TIMESCALE, <1000.0, 1000.0, 1000.0>);

llSetVehicleFloatParam(VEHICLE_VERTICAL_ATTRACTION_EFFICIENCY, 0.25); // almost wobbly
llSetVehicleFloatParam(VEHICLE_VERTICAL_ATTRACTION_TIMESCALE, 1.5); // mediocre response

llSetVehicleFloatParam(VEHICLE_BANKING_EFFICIENCY, 0.4); // medium strength
llSetVehicleFloatParam(VEHICLE_BANKING_TIMESCALE, 0.1); // very responsive
llSetVehicleFloatParam(VEHICLE_BANKING_MIX, 0.95); // more banking when moving

// hover can be better than sliding along the ground during takeoff and landing
// but it only works over the terrain (not objects)
//llSetVehicleFloatParam(VEHICLE_HOVER_HEIGHT, 3.0);
//llSetVehicleFloatParam(VEHICLE_HOVER_EFFICIENCY, 0.5);
//llSetVehicleFloatParam(VEHICLE_HOVER_TIMESCALE, 2.0);
//llSetVehicleFlags(VEHICLE_FLAG_HOVER_UP_ONLY);

// non-zero buoyancy helps the airplane stay up
// set to zero if you don't want this crutch
llSetVehicleFloatParam(VEHICLE_BUOYANCY, 0.2);

// define these here for convenience later
gAngularControls = CONTROL_RIGHT | CONTROL_LEFT | CONTROL_ROT_RIGHT
| CONTROL_ROT_LEFT | CONTROL_DOWN | CONTROL_UP;
gLinearControls = CONTROL_FWD | CONTROL_BACK;

llSetStatus(STATUS_PHYSICS, TRUE);
}

changed(integer change)
{
if (change & CHANGED_LINK)
{
key agent = llAvatarOnSitTarget();
if (agent)
{
if (agent != llGetOwner())
{
// only the owner can use this vehicle
llSay(0, "You aren't the owner");
llUnSit(agent);
llPushObject(agent, <0,0,10>, ZERO_VECTOR, FALSE);
}
else
{
// clear linear motor on successful sit
gLinearMotor = <0, 0, 0>;
llSetVehicleVectorParam(VEHICLE_LINEAR_MOTOR_DIRECTION, gLinearMotor);

//llSetStatus(STATUS_PHYSICS, TRUE);
llSetVehicleFloatParam(VEHICLE_LINEAR_FRICTION_TIMESCALE, 1000.0);
llSetVehicleFloatParam(VEHICLE_ANGULAR_FRICTION_TIMESCALE, 1000.0);
llRequestPermissions(agent, PERMISSION_TRIGGER_ANIMATION | PERMISSION_TAKE_CONTROLS);
}
}
else
{
// stop the motors
gLinearMotor = <0, 0, 0>;
llSetVehicleVectorParam(VEHICLE_LINEAR_MOTOR_DIRECTION, gLinearMotor);
llSetVehicleVectorParam(VEHICLE_ANGULAR_MOTOR_DIRECTION, gLinearMotor);

// use friction to stop the vehicle rather than pinning it in place
//llSetStatus(STATUS_PHYSICS, FALSE);
llSetVehicleFloatParam(VEHICLE_LINEAR_FRICTION_TIMESCALE, 1.0);
llSetVehicleFloatParam(VEHICLE_ANGULAR_FRICTION_TIMESCALE, 1.0);

// driver is getting up
llReleaseControls();
llStopAnimation("motorcycle_sit");
}
}

}

run_time_permissions(integer perm)
{
if (perm)
{
llStartAnimation("motorcycle_sit");
llTakeControls(gAngularControls | gLinearControls, TRUE, FALSE);
}
}

control(key id, integer level, integer edge)
{
// only change linear motor if one of the linear controls are pressed
vector motor;
integer motor_changed = level & gLinearControls;
if (motor_changed)
{
if(level & CONTROL_FWD)
{
if (gLinearMotor.x < 0)
{
gLinearMotor.x = 0;
}
else if (gLinearMotor.x < 30)
{
gLinearMotor.x += 5;
}
motor_changed = TRUE;
}
if(level & CONTROL_BACK)
{
if (gLinearMotor.x > 0)
{
gLinearMotor.x = 0;
}
else if (gLinearMotor.x > -30)
{
gLinearMotor.x -= 5;
};
motor_changed = TRUE;
}
llSetVehicleVectorParam(VEHICLE_LINEAR_MOTOR_DIRECTION, gLinearMotor);
}

// only change angular motor if the angular levels have changed
motor_changed = (edge & gOldAngularLevel) + (level & gAngularControls);
if (motor_changed)
{
motor = <0,0,0>;
if(level & (CONTROL_RIGHT|CONTROL_ROT_RIGHT))
{
// add roll component ==> triggers banking behavior
motor.x += 10;
}
if(level & (CONTROL_LEFT|CONTROL_ROT_LEFT))
{
motor.x -= 10;
}
if(level & (CONTROL_UP))
{
// add pitch component ==> causes vehicle lift nose (in local frame)
motor.y -= 8;
}
if(level & (CONTROL_DOWN))
{
motor.y += 8;
}
llSetVehicleVectorParam(VEHICLE_ANGULAR_MOTOR_DIRECTION, motor);
}
// store the angular level history for the next control callback
gOldAngularLevel = level & gAngularControls;
}
}


Edit -- script now sets vehicle physical in the default state_entry() callback.

Edit -- scaled buoyancy down to 0.2 from 0.7. The airplane will probably work fine wihout any buoyancy for those who want to remove it.

Edit -- testing the PHP default colorization of LSL script code
Darzus Strutt
Registered User
Join date: 3 Sep 2003
Posts: 67
Original Thread
11-29-2003 06:36
/54/81/69229/1.html

edit- Discussion moved to scritping tips.


-----------------------------------------------



I am having some trouble with this script, I have tested it as is and it does not seem to doa anything..It takes control of animations..and keys..but when i press any keys to move..nothing happens.
Sinatra Cartier
From Beta to Zeta!
Join date: 8 Jan 2003
Posts: 533
Darzus,
11-29-2003 12:06
You will need to make the object physical for this to work.
_____________________

...and I think to myself, what a wonderful world.



Sinatra's Spook House Ride: Noyo (100, 150, 25)
Andrew Linden
Linden staff
Join date: 18 Nov 2002
Posts: 692
11-30-2003 21:34
I think Sinatra hit it on the nose. The script doesn't change the STATUS_PHYSICS flag. It should probably set it true in the default state_entry() callback since everywhere else the script assumes that the vehicle has been set physical.

I'll edit the initial post so that it sets the physical flag.
Andrew Linden
Linden staff
Join date: 18 Nov 2002
Posts: 692
12-08-2003 11:29
Buoyancy works right now, so I'm going to scale back the buoyancy in the airplane example script.
Alondria LeFay
Registered User
Join date: 2 May 2003
Posts: 725
12-09-2003 05:32
Hey Andrew, I was testing this script out and it ended up throwing myself and my passenger when flying around 1500 meters, which of course means I have a fairly detailed plane consuming a horid amount of taxes up there somewhere. I also then enbedded a prime with it and ended up flying it off world. When I re-rezed the prime, it shot practically straight up, thus loosing another object somewhere way up high in scary tax land. Any ideas?
Andrew Linden
Linden staff
Join date: 18 Nov 2002
Posts: 692
12-09-2003 10:36
Objects can't exist above about 512 or 768 meters. I forget which hieght because there are several pieces of logic that prevent them, and they currently disagree as to the limit (that's a bug on my plate... gotta fix that someday). My guess is that the plane got deleted. It should have been put back in your inventory, but there is be a path by which it can be deleted but does not go back into inventory: when a simulator restarts, it will not load objects that are too high. If your plane was too high before this mornings update then it got ignored when the world came back online.
Alondria LeFay
Registered User
Join date: 2 May 2003
Posts: 725
12-14-2003 23:49
Yup. No horidously large object taxes since then. :)

I think I need to throw in safeguards (auto-dieing timers) into everything I do with physics from now on...
Kyle Chaos
Member
Join date: 23 Oct 2003
Posts: 50
12-21-2003 10:52
w00t! This script is AWSOME! With a little tweaking I finally got a good script for my Wright Brother's Plane! :D
_____________________
"Before you make a bad comment about someone you should walk a mile in their shoes. That way you'll be a mile away AND you'll have their shoes!"
Mike Sandgrain
Junior Member
Join date: 23 Dec 2003
Posts: 2
12-24-2003 01:46
Could someone comment on how to modify the script or prim its attached to so that the local axis is set the correct way.
Huns Valen
Don't PM me here.
Join date: 3 May 2003
Posts: 2,749
12-27-2003 04:14
From: someone
Originally posted by Mike Sandgrain
Could someone comment on how to modify the script or prim its attached to so that the local axis is set the correct way.


in the source:

// assumes that the root primitive is oriented such that its
// local x-axis points toward the nose of the plane, and its
// local z-axis points toward the top
Tyoril Satyr
Junior Member
Join date: 7 Jan 2004
Posts: 2
01-10-2004 06:35
Hm.. i applid this script to a bodgy jetpack looken thing made out of prims, and doesnt realyl work.. theres no 'fly' or 'drive' options available in the pie. am i doing something wrong?
Andrew Linden
Linden staff
Join date: 18 Nov 2002
Posts: 692
01-15-2004 11:15
Mike, if the root primitive of your airplane does not have the same orientation as the whole airplane (i.e. local x-axis = forward, local y-axis = left, local z-axis = up) then the script will not push your airplane in the "correct" direction. You need to either:

1. orient your root primitive to agree with the airplane's axes of motion

2. pick a different primitive to be the root (one whose local axes agree with those of the airplane's motion)

3. use the VEHICLE_REFERENCE_FRAME parameter (this is an advanced topic and is outside the scope of a _simple_ airplane script)


Tyoril, not sure why things aren't working for you. You aren't attaching the scripted jetpack to your avatar are you? The script above is not designed to be attached; it is supposed to be sat upon.
Huns Valen
Don't PM me here.
Join date: 3 May 2003
Posts: 2,749
02-01-2004 19:56
Tyoril,

There is a marked difference between attaching something and sitting on it. When you attach something, you become the root primitive, and it is linked to you. When you sit on something, it becomes the root primitive, and you are linked to it. In the first case, the root prim (the avatar) already has a built-in movement system. The vehicle system is designed with the second case in mind.

If you want a good jetpack script, search the forums for the FinalFlight script. You can put that into a jetpack and then attach it, and it will augment your velocity while you are flying.
Ivan Lorentz
Junior Member
Join date: 26 Jan 2004
Posts: 2
how do you chane were you sit?
02-05-2004 22:47
my avatar sits on the plane upside down..how do you change it so he sits in the seat?
Andrew Linden
Linden staff
Join date: 18 Nov 2002
Posts: 692
02-11-2004 08:25
CODE
llSitTarget(local_position, local_rotation)


Find where that call is made in the script above (hint, its in the detault state_entry()) and change it accordingly.

However, if your avatar cuffently sits "upside down" then you probably will have similar problems as Mike and may have to do either 1., 2., or 3. as mentioned in my last post above.
Huns Valen
Don't PM me here.
Join date: 3 May 2003
Posts: 2,749
04-06-2004 07:45
If you are upside down, sideways, etc., try this:

llSitTarget(local_position, llEuler2Rot(<x, y, z>;));

Plug PI_BY_TWO and/or PI into x, y, and z to see what happens. This is how I figured out how to get a secondary sit target working on a funny-shaped prim. I think I wound up with something like <-PI_BY_TWO, 0, PI>. Change the x, y, and z vars one at a time so you can narrow down which one rotates the avatar in which direction.
4zero4 Farber
Junior Member
Join date: 24 May 2004
Posts: 2
Ahh
05-25-2004 15:54
I am very new and when I get on my object (doesn't really resemble a plane) to fly it my nose points to the ground every time.. I have no clue why could someone help?
Andrew Linden
Linden staff
Join date: 18 Nov 2002
Posts: 692
05-28-2004 22:58
4zero4, you probably need to read my post (to Mike) above about making the root primitive of your airplane rotated such that its local x-, y-, and z- axes point along the plane's at- left- and up- axes.
Tyg Jarrico
Registered User
Join date: 29 May 2005
Posts: 98
06-02-2005 06:03
I am very new. What keys do I use to move the airplane?
a lost user
Join date: ?
Posts: ?
08-05-2005 20:37
From: Tyg Jarrico
I am very new. What keys do I use to move the airplane?


The 4 arrows, and Page Up / Page Down
Frog Bakalava
Registered User
Join date: 30 Sep 2005
Posts: 4
10-01-2005 22:43
Im a scripting noob and just trying to figure this out.. i tried adding this script(unchanged) to a crappy looking plane i made.. i try to go to sit.. but i see no controls or "fly"button nothing... what am i suppose to change if anything or what am i doing wrong? thanks-
just trying to figure this stuff out!
Cubey Terra
Aircraft Builder
Join date: 6 Sep 2003
Posts: 1,725
10-02-2005 09:12
Frog, the script has to be in the parent prim for it to work correctly. The parent prim is the one that glows yellow when you're editing an object -- the the prim that was selected last before you linked the object.
_____________________
C U B E Y · T E R R A
planes · helicopters · blimps · balloons · skydiving · submarines
Available at Abbotts Aerodrome and XstreetSL.com

Torley Linden
Enlightenment!
Join date: 15 Sep 2004
Posts: 16,530
10-02-2005 10:49
And since Cubey posted here, it is a tremendeliciously good idea to acquire his DIY Plane Kit for a more complete package of how to build and fly a plane in SL. It's the next generation of what's refered to heretoin!
_____________________
Torley Linden
Enlightenment!
Join date: 15 Sep 2004
Posts: 16,530
10-02-2005 10:52
Apologies, I forgot to emphasize it is FULLPERMS, so if you would like to give it a fly, please IM me inworld Frog (even if I'm offline) and I would be more than happy to help. :)
_____________________
1 2