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 Car_Script  

Car_Script

Car Script.lsl

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

the Zip file

Download all files for Car_Script
Contents are in zip format, with .LSL (text) source code and LSLEdit (text + Solution) formats.
Get file # 1. Car_Script_1.lsl
1
2 float theta_func()
3 {
4 if(speed < 1) return 0.02;
5 if(speed < 4) return 0.1;
6 if(speed < 6) return .4;
7 if(speed < 10) return .6;
8 if(speed < 15) return .5;
9 if(speed < 20) return .5;
10 if(speed < 25) return .4;
11 return .3;
12
13 }
14
15 float power_curve()
16 {
17 if(speed < 1) return 15.0;
18 if(speed < 4) return 15.0;
19 if(speed < 6) return 15.0;
20 if(speed < 10) return 15.0;
21 if(speed < 15) return 10.0;
22 if(speed < 20) return 4.0;
23 if(speed < 30) return 2.0;
24 return 2.0;
25 }
26
27
28 float THROTTLE = 1.2;
29 float STEERING = 1;
30 float TIMER = .05;
31 float ANGULAR_TAU = .1; // decrease for faster turn
32 float ANGULAR_DAMPING = 3; // not sure what this should be yet
33 float THETA_INCREMENT = 0.05; // increase for bigger turn response
34
35
36 //float C_FRICTION = 40;
37 //float FRONTAL_AREA = 20;
38 //float TOT_FRICTION;
39
40
41 float mass;
42 float speed;
43 vector velocity;
44
45
46 float turn;
47 float counter = 0;
48 float brake = 1;
49 // linear constants
50
51
52 integer a = 10;
53
54
55 integer LEVELS = 0; // we'll set this later
56
57 // global variables
58 vector gTargetPos;
59 float gTargetTheta;
60 key gToucher;
61
62
63 default
64 {
66 {
67 llSay(0,"car starting");
69 llSetStatus(STATUS_ROTATE_X | STATUS_ROTATE_Y, FALSE);
70 llCollisionFilter("", "", TRUE);
72 mass = llGetMass();
73
74
75 llSleep(0.1);
76 //SetHoverHeight(1,1,10);
77 llSetStatus(STATUS_ROTATE_X | STATUS_ROTATE_Y | STATUS_ROTATE_Z , TRUE);
78
79 llMoveToTarget(llGetPos() + <0,0,1>, 0);
80 llRotLookAt(llGetRot(), 0, 0);
81
82
84 }
85
86
87 touch_start(integer total_number)
88 {
89
91 gToucher = llDetectedKey(0);
93
94
95 if(perm)
96 {
98 }
99 else
100 {
101 state StateDriving;
102 }
103 }
104 }
105
106 state StateDriving
107 {
109 {
110 llSetTimerEvent(TIMER);
111 brake = 1;
112
114 llSetStatus(STATUS_ROTATE_X | STATUS_ROTATE_Y | STATUS_ROTATE_Z , TRUE);
115 llWhisper(0, "StateDriving");
116 //llSetHoverHeight(a,1,.4);
118 //llMoveToTarget(llGetPos(), LINEAR_TAU);
119 llRotLookAt(llGetRot(), ANGULAR_TAU, 1.0);
120 }
121 touch_start(integer total_number)
122 {
123 llWhisper(0, "Touched.");
125 if(perm)
126 {
128 }
129 state default;
130 }
132 {
134 {
135 llTakeControls(LEVELS, TRUE, FALSE); }
136 else
137 {
138 state default;
139 }
140 }
141 control(key driver, integer levels, integer edges)
142 {
143 //llWhisper(0, "l = " + (string)levels + " e = " + (string)edges);
144 // linear control
145 //llSetHoverHeight(a,1,.4);
146
147 integer nudge = FALSE;
148 float direction = 0;
149 gTargetTheta *= theta_func();
150
151
152 if(levels & CONTROL_FWD)
153 {
154 direction = 1;
155 brake = 0;
156 }
157 if(levels & CONTROL_BACK)
158 {
159 direction = -.1;
160 brake = 5;
161 gTargetTheta *= 2;
162 }
163 if(levels & CONTROL_LEFT)
164 {
165
166 }
167 if(levels & CONTROL_RIGHT)
168 {
169
170 }
171 if(levels & CONTROL_UP)
172 {
173 a +=1;
174 }
175 if(levels & CONTROL_DOWN)
176 {
177 a-=1;
178 }
179 // angular control
180 if(levels & CONTROL_ROT_LEFT)
181 {
182 if(gTargetTheta < 0)
183 {
184 gTargetTheta = 0;
185 }
186 else
187 {
188 gTargetTheta += THETA_INCREMENT;
189 }
190 nudge = TRUE;
191 }
192 if(levels & CONTROL_ROT_RIGHT)
193 {
194 if(gTargetTheta > 0)
195 {
196 gTargetTheta = 0;
197 }
198 else
199 {
200 gTargetTheta -= THETA_INCREMENT;
201 }
202 nudge = TRUE;
203 }
204
205 if(direction)
206 {
207 //sound control
208 if(speed < 24) { llLoopSound("car accel", .4);}
209 else { llLoopSound("car go",.4); }
210
211 //applies forward boost
212
213 // llSay( 0, (string) power + " " + (string) direction + " " + (string) mass + (string) power_curve() + (string) theta_func() );
214
215
216 llApplyImpulse( <1,0,0> * direction * mass * power_curve()* THROTTLE , 1);
217
218 }
219
220 if(direction == 0)
221
222 {llLoopSound("geardown", .4);
223 }
224
225 if(nudge)
226 {
227
228 // angular motion -- we compute the raw quaternionabout z-axis
229 counter++;
230 float st = 0.5 * llSin(gTargetTheta*STEERING);
231 float ct = 0.5 * llCos(gTargetTheta*STEERING);
232 rotation rot = <0,0,st, ct> * llGetRot();
233 llRotLookAt(rot, ANGULAR_TAU, ANGULAR_DAMPING);
234 }
235
236 else {counter = 0;}
237 }
238
239 land_collision(vector position)
240 {
241 //llApplyImpulse( <0,0, .5> * mass, 1 );
242 }
243
244
245 collision (integer total_number)
246 {
247 // llWhisper( 0, "collision 2");
248 //llSetHoverHeight(4,1,.2);
249 }
250
251
252 timer() //side ways supression
253 {
254
255 vector pos = llGetPos();
256
257 if(llGround(<0,0,0>) > (pos.z - 3) )
258 {
259 velocity = llGetVel();
260 speed = llVecMag(velocity);
261
262 float l_speed = llRot2Left( llGetRot() ) * velocity;
263 float f_speed = llRot2Fwd( llGetRot() ) * velocity;
264
265
266 if( l_speed > 8 || (brake > 1 && speed > 8) ) llTriggerSound("tires burn",10);
267 if(speed < 1) llStopSound();
268
269
270 if(counter * speed > 100)
271 {
272 l_speed /= 4;
273 brake = 2;
274 }
275 if(speed > 4) brake = 2;
276 f_speed *= brake;
277
278
279 llApplyImpulse( <0,-1,0> * l_speed * mass *.8, 1 );
280 llApplyImpulse( <-1,0,0> * f_speed * mass *.05, 1 );
281 }
282
283 }
284
285 }
286
287 // END //

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