Join us in Outworldz at www.outworldz.com:9000 or follow us:

Search dozens of selected web sites for OpenSim and LSL script

New! Script Meta-Search will search thousands of scripts here and at other sites for LSL or Opensim scripts.
Loading

Want to add a script or a project? Upload it and a half million people will see it and your name here this year.

Home   Show All
Category: Contributor: Creator
Vehicles Simple_Vehicle_1  

Simple_Vehicle_1

Simple Vehicle 1.0.lsl

Category: Vehicles
By : Cory Linden
Created: 2010-01-10 Edited: 2010-01-10
Worlds: Second Life

the Zip file

Download all files for Simple_Vehicle_1
Contents are in zip format, with .LSL (text) source code and LSLEdit (text + Solution) formats.
Get file # 1. Simple_Vehicle_1_1.lsl
1
2 //Basic Motorcycle Script
3 //
4 // by Cory
5 // commented by Ben
6 // Last edited by Nada Epoch on 01-01-2004 at 08:51 PM
7
8 //The new vehicle action allows us to make any physical object in Second
9 //Life a vehicle. This script is a good example of a
10 // very basic vehicle that is done very well.
11
12 default
13 {
14
15 //There are several things that we need to do to define vehicle,
16 // and how the user interacts with it. It makes sense to
17 // do this right away, in state_entry.
19 {
21 //We can change the text in the pie menu to more accurately
22 // reflect the situation. The default text is "Sit" but in
23 // some instances we want you to know you can drive or ride a
24 // vehicle by sitting on it. The llSetSitText function will
25 // do this.
26 llSetSitText("Ride");
27
28 //Since you want this to be ridden, we need to make sure that
29 // the avatar "rides" it in a acceptable position
30 // and the camera allows the driver to see what is going on.
31 //
32 //llSitTarget is a new function that lets us define how an avatar will orient itself when sitting.
33 // The vector is the offset that your avatar's center will be
34 // from the parent object's center. The
35 // rotation is based off the positive x axis of the parent. For
36 // this motorcycle, we need you to sit in a way
37 // that looks right with the motorcycle sit animation, so we
38 // have your avatar sit slightly offset from the seat.
39 llSitTarget(<0.6, 0.03, 0.20>, ZERO_ROTATION);
40
41 //To set the camera, we need to set where the camera is, and
42 // what it is looking at. By default, it will
43 // be looking at your avatar's torso, from a position above and
44 // behind. It will also be free to rotate around your
45 // avatar when "turning."
46 //
47 //For the motorcycle, we are going to set the camera to be
48 // behind the cycle, looking at a point in front of it.
49 // Due to the orientation of the parent object, this will appear to be looking down on the avatar.
50 llSetCameraEyeOffset(<-5.0, -0.00, 2.0> );
51 llSetCameraAtOffset(<3.0, 0.0, 2.0> );
52
53
54 //To make an object a vehicle, we need to define it as a
55 // vehicle. This is done by assigning it a vehicle type.
56 // A vehicle type is a predefined set of parameters that describe how the physics engine should let your
57 // object move. If the type is set to VEHICLE_TYPE_NONE it will no longer be a vehicle.
58 //
59 //The motorcycle uses the car type on the assumption that this
60 // will be the closest to how a motorcycle should work.
61 // Any type could be used, and all the parameters redefined later.
63
64
65 //While the type defines all the parameters, a motorcycle is
66 // not a car, so we need to change some parameters
67 // to make it behave correctly.
68
69 //The vehicle flags let us set specific behaviors for a vehicle
70 // that would not be covered by the more general
71 // parameters. For instance, a motorcycle shouldn't me able to
72 // push itself into the sky and fly away, so we
73 // want to limit its ability to push itself up if pointed that way. There are several flags that help when
74 // making various vehicles.
76
77 //To redefine parameters, we use the function
78 // llSetVehicleHippoParam where Hippo is the variable type of the
79 // parameter (float, vector, or rotation).
80 //
81 //Most parameters come in pairs, and efficiency and a timescale. The efficiency defines <more>, while the timescale
82 // defines the time it takes to achieve that effect.
83 //
84 //In a virtual world, a motorcycle is a motorcycle because it looks and moves like a motorcycle. The look is
85 // up to the artist who creates the model. We get to define
86 // how it moves. The most basic properties of movement
87 // can be thought of as the angular deflection (points in the
88 // way it moves) and the linear deflection (moves in the
89 // way it points). A dart would have a high angular deflection, and a low linear deflection. A motorcycle has
90 // a low linear deflection and a high linear deflection, it goes where the wheels send it. The timescales for these
91 // behaviors are kept pretty short.
96
97 //A bobsled could get by without anything making it go or turn
98 // except for a icey hill. A motorcycle, however, has
99 // a motor and can be steered. In LSL, these are linear and
100 // angular motors. The linear motor is a push, the angular
101 // motor is a twist. We apply these motors when we use the
102 // controls, but there is some set up to do. The motor
103 // timescale controls how long it takes to get the full effect
104 // of the motor, basically acceleration. The motor decay
105 // timescale defines how long the motor stays at that strength
106 // - how slowly you let off the gas pedal.
107 llSetVehicleFloatParam(VEHICLE_LINEAR_MOTOR_TIMESCALE, 1.0);
110 llSetVehicleFloatParam(VEHICLE_ANGULAR_MOTOR_DECAY_TIMESCALE, 0.5);
111
112 //Real world vehicles are limited in velocity and slow to a
113 // stop due to friction. While a vehicle that continues
114 // moving forever is kinda neat, it is hard to control, and not
115 // very realistic. We can define linear and angular
116 // friction for a vehicle, how quickly you will slow down while
117 // moving or rotating.
118 //
119 //A motorcycle moves easily along the line defined by the
120 // wheels, and not as easily against the wheels. A motorcycle
121 // falling out of the air shouldn't feel very much friction at
122 // all. For the most part, our angular frictions don't
123 // matter, as they are handled by the vertical attractor. The
124 // one component that is not handled by the vertical
125 // attractor is the rotation around the z axis, so we give it
126 // some friction to make sure we don't spin forever.
129
130 //We are using a couple of tricks to make the motorcycle look
131 // like a real motorcycle. We use an animated texture to
132 // spin the wheels. The actual object can not rely on the real
133 // world physics that lets a motorcycle stay upright.
134 // We use the vertical attractor parameter to make the object
135 // try to stay upright. The vertical attractor also allows
136 // us to make the vehicle bank, or lean into turns.
137 //
138 //The vertical attraction efficiency is slightly misnamed, as
139 // it should be "coefficient." Basically, it controls
140 // if we critically damp to vertical, or "wobble" more. It also
141 // has a secondary effect that it will limit the roll
142 // of the vehicle. The timescale will control how fast we go
143 // back to vertical, and
144 // thus how strong the vertical attractor is.
145 //
146 //We want people to be able to lean into turns, not fall down,
147 // and not wobble to much while coming back up.
148 // A vertical attraction efficiency of .5 is nicely in the middle, and it won't wobble to badly because of the
149 // inherent ground friction. As shorter timescale will make it
150 // hard to roll, a longer one will let us roll a lot
151 // (and get a bit queasy). We will find that the controls are
152 // also affected by the vertical attractor
153 // as we tune the banking features, and that sometimes finding
154 // good values for these numbers is more an art than a science.
157
158 //Banking means that if we rotate on the roll axis, we will
159 // also rotate on the yaw axis, meaning that our motorcycle will lean to the
160 // side as we turn. Not only is this one of the things that it look like a real motorcycle, it makes it look really cool too. The
161 // higher the banking efficiency, the more "turn" for your
162 // "lean". This motorcycle is made to be pretty responsive, so we have a high
163 // efficiency and a very low timescale. The banking mix lets
164 // you decide if you can do the arcade style turn while not moving, or make
165 // a realistic vehicle that only banks with velocity. You can
166 // also input a negative banking mix value to make it bank the wrong way,
167 // which might lead to some interesting vehicles.
171
172 //Because the motorcycle is really just skidding along the
173 // ground, its colliding with every bump it can find, the default behavior
174 // will have us making loud noises every bump, which isn't very
175 // desirable, so we can just take those out.
176 llCollisionSound("", 0.0);
177 }
178
179
180 //A sitting avatar is treated like a extra linked primitive, which
181 // means that we can capture when someone sits on the
182 // vehicle by looking for the changed event, specifically, a link
183 // change.
184 changed(integer change)
185 {
186 //Make sure that the change is a link, so most likely to be a
187 // sitting avatar.
188 if(change & CHANGED_LINK)
189 {
190 //The llAvatarSitOnTarget function will let us find the key
191 // of an avatar that sits on an object using llSitTarget
192 // which we defined in the state_entry event. We can use
193 // this to make sure that only the owner can drive our vehicle.
194 // We can also use this to find if the avatar is sitting, or is getting up, because both will be a link change.
195 // If the avatar is sitting down, it will return its key, otherwise it will return a null key when it stands up.
196 key agent = llAvatarOnSitTarget();
197
198 //If sitting down.
199 if(agent)
200 {
201 //We don't want random punks to come stealing our
202 // motorcycle! The simple solution is to unsit them,
203 // and for kicks, send um flying.
204 if(agent != llGetOwner())
205 {
206 llSay(0, "You aren't the owner");
207 llUnSit(agent);
208 llPushObject(agent, <0,0,100>, ZERO_VECTOR, FALSE);
209 }
210 // If you are the owner, lets ride!
211 else
212 {
213 //The vehicle works with the physics engine, so in
214 // order for a object to act like a vehicle, it must first be
215 // set physical.
217 //There is an assumption that if you are going to
218 // choose to sit on a vehicle, you are implicitly giving
219 // permission to let that vehicle act on your controls, and to set your permissions, so the end user
220 // is no longer asked for permission. However, you
221 // still need to request these permissions, so all the
222 // paperwork is filed.
224 //We will play a little "startup" sound.
225 llPlaySound("SUZ_start (2).wav", 0.7);
226 // All the messageLinked calls are communicating
227 // with other scripts on the bike. There is a script that controls
228 // particle systems, and one that controls sounds.
229 // This way we can make a simple "motorcycle" script that is modular
230 // and you can put in your own sounds/particles,
231 // and still use the same base script.
232 llMessageLinked(LINK_SET, 0, "get_on", "");
233 }
234 }
235 //The null key has been returned, so no one is driving anymore.
236 else
237 {
238 //Clean up everything. Set things nonphysical so they
239 // don't slow down the simulator. Release controls so the
240 // avatar move, and stop forcing the animations.
243 llStopAnimation("motorcycle_sit");
244 // Here we let the other scripts know the cycle is done.
245 llMessageLinked(LINK_SET, 0, "idle", "");
246 }
247 }
248
249 }
250
251 //Because we still need to request permissions, the run_time_permissions event still occurs, and is the perfect
252 // place to start
253 // the sitting animation and take controls.
255 {
256 if(perm)
257 {
258 llStartAnimation("motorcycle_sit");
260 }
261 }
262
263 //If we want to drive this motorcycle, we need to use the controls.
264 control(key id, integer level, integer edge)
265 {
266 //We will apply motors according to what control is used. For
267 // forward and back, a linear motor is applied with a vector
268 // parameter.
269 vector angular_motor;
270
271 if(level & CONTROL_FWD)
272 {
273 //The Maximum linear motor direction is 50, and will try to
274 // get us up to 50 m/s - things like friction and the
275 // motor decay timescale can limit that.
277
278 }
279 if(level & CONTROL_BACK)
280 {
282 }
284 {
285 //The Maximum angular motor direction is 4Pi radians/second.
286 //We are being a little sloppy in the scripting here,
287 // just to ensure
288 // that we turn quickly.
289 angular_motor.x += PI*4;
290 angular_motor.z -= PI*4;
291 }
293 {
294 angular_motor.x -= PI*4;
295 angular_motor.z += PI*4;
296 }
297 if(level & (CONTROL_UP))
298 {
299 angular_motor.y -= 50;
300 }
301
302 if((edge & CONTROL_FWD) && (level & CONTROL_FWD))
303 {
304 // We have a few message links to communicate to the other
305 // scritps when we start to accelerate and let off the gas.
306 llMessageLinked(LINK_SET, 0, "burst", "");
307 }
308 if((edge & CONTROL_FWD) && !(level & CONTROL_FWD))
309 {
310 llMessageLinked(LINK_SET, 0, "stop", "");
311 }
312
313 //The angular motor is set last, just incase there is a sum of
314 // the right and left controls (you have to swing the handlebars back to center)
316 }
317
318 }// END //

Back to the Best Free Tools in Second Life and OpenSim.