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
Walk Walk Anywhere  

Walk Anywhere

Walk anywhere on any surface with a ray caster

Category: Walk
By : Adeon Writer
Created: 2015-01-15 Edited: 2015-01-15
Worlds: Second Life

the Zip file

Download all files for Walk Anywhere
Contents are in zip format, with .LSL (text) source code and LSLEdit (text + Solution) formats.
Get file # 1. Script.lsl
1
2 // BOF
3 // Raycasting Wall Walker v1 by Adeon Writer
4 // This script is licenced under the following terms and conditions: http://www.wtfpl.net/txt/copying/
5
6 //// Global Constants (Mod these!)
7 float Z_MODIFIER = 0.8; // Move avatar up or down along prim's z axis (mod while sitting on it to put your avatar's feet on the ground)
8 float JUMP_FORCE = 0.35; // Initial upward boost from jumps. (careful, this is powerful.)
9 float MOVE_SPEED = 0.075; // Movement speed in meters per server step
10 float TURN_SPEED = 0.05; // Turn speed in radians per server step
11 float RUN_BOOST = 2.5; // Speed multiplier while running instead of walking (to run, hold forward and back at the same time... yeah I know it's dumb.)
12 vector CAMERA_FLOATAT_POSITION = <-2.5, 0, 2.0>; // Local position that camera will be positioned relative to the vehicle
13 vector CAMERA_LOOKAT_POSITION = <0, 0, 1.0>; // Local position that camera point at relative to the vehicle
14 string STAND_ANIM = "Stand";
15 string WALK_ANIM = "Walk";
16 string RUN_ANIM = "Run";
17
18 //// Internal global varables, script will modify these values during runtime.
19 float boost;
20 float jumpHeight;
21 float jumpSpeed;
22 float jumpGravity = 0.02;
23 vector rot2fwd;
24 vector rot2left;
25 vector rot2up;
26 vector movementOffset;
27 integer left;
28 integer right;
29 integer notJumping = TRUE;
30 rotation rot;
31
32 //// Methods
33 // Spherical linear interpolation from a to b at t (where a is t=0.0 and b is t=1.0)
34 rotation slerp(rotation a, rotation b, float t)
35 {
36 return llAxisAngle2Rot(llRot2Axis(b/=a), t*llRot2Angle(b))*a; // Example taken from http://wiki.secondlife.com/wiki/Slerp
37 }
38
39 // Positional interpolation from a to b at t (where a is t=0.0 and b is t=1.0)
40 vector perp(vector a, vector b, float t)
41 {
42 return a+(b-a)*t;
43 }
44
45 // Tweens object's rotation from A to B while position from C to D over 'steps' steps.
46 smoothRotate(rotation A, rotation B, vector C, vector D, integer steps)
47 {
48 integer i;
49 for(i=1; i<steps; i++) llSetLinkPrimitiveParamsFast(LINK_THIS, [PRIM_ROTATION, slerp(A, B, i/(float)steps), PRIM_POSITION, perp(C, D, i/(float)steps)]);
50 jumpHeight = 0.0; // Cancel any jump velocity.
51 }
52
53 // Terribly akward simulation of acceleration due to gravity!
54 gravity()
55 {
56 jumpSpeed -= jumpGravity;
57 jumpHeight += jumpSpeed;
58 if(jumpHeight < 0.0)
59 {
60 if(!notJumping)
61 {
62 notJumping = TRUE;
63 if(movementOffset == ZERO_VECTOR) llStartAnimation(STAND_ANIM);
64 else llStartAnimation(WALK_ANIM);
65 llStopAnimation("jump");
66 }
67 jumpHeight = 0;
68 }
69 // prevent float precission errors by capping the values:
70 if(jumpHeight < -10) jumpHeight = -5;
71 if(jumpSpeed < -5) jumpSpeed = -2;
72 }
73
74 zMod() // Sets avatar's height based on Z_MODIFIER
75 {
76 llSetLinkPrimitiveParamsFast(2, [PRIM_POS_LOCAL, <0,0,Z_MODIFIER>]);
77 }
78
79 default
80 {
81 on_rez(integer start)
82 {
83 vector scale = llGetScale();
84 llSetPos(llGetPos() - <0,0,scale.z*0.4>);
85 }
86
88 {
89 zMod();
90 llSetCameraEyeOffset(CAMERA_FLOATAT_POSITION);
91 llSetCameraAtOffset(CAMERA_LOOKAT_POSITION);
95 llSetVehicleFlags(VEHICLE_FLAG_CAMERA_DECOUPLED); // When in mouselook, object rotation should not cause mouselook rotation.
98 llSetColor(<1,1,1>, ALL_SIDES);
99 llSitTarget(<0.0,0,0.18>, ZERO_ROTATION);
101 {
103 llSetTimerEvent(0.01);
104 }
105 }
106
107 changed(integer change)
108 {
109 if(change & CHANGED_LINK)
110 {
112 {
114 llOwnerSay("Ok. Arrows or WASD move you. You can also jump. Press M for mouselook (it's neat).");
115 llSetTexture(TEXTURE_TRANSPARENT, ALL_SIDES);
116 zMod();
117 llSetTimerEvent(0.01);
118 }
119 else
120 {
123 }
124 }
125 }
126
127 timer()
128 {
129 gravity();
130 rot = llGetRot();
131 rot2fwd = llRot2Fwd(rot);
132 rot2left = llRot2Left(rot);
133 rot2up = llRot2Up(rot);
134 vector pos = llGetPos();
135 list cast = llCastRay(pos, pos + movementOffset*0.25, [RC_REJECT_TYPES, RC_REJECT_PHYSICAL, RC_DATA_FLAGS, RC_GET_NORMAL, RC_MAX_HITS, 2]); // Our first cast is to check for walls in direction of movement.
136 integer smooth = TRUE;
137 if(llList2Key(cast, 0) == llGetOwner()) cast = llDeleteSubList(llListReplaceList(cast, [llList2Integer(cast, -1)-1], -1, -1), 0, 2);
138 if(llList2Integer(cast, -1) <= 0)
139 {
140 smooth = FALSE;
141 cast = llCastRay(pos, pos + rot2up*(-1-jumpHeight), [RC_REJECT_TYPES, RC_REJECT_PHYSICAL, RC_DATA_FLAGS, RC_GET_NORMAL, RC_MAX_HITS, 2]); // There's no walls, so check down at our riding surface.
142 }
143 if(llList2Key(cast, 0) == llGetOwner()) cast = llDeleteSubList(llListReplaceList(cast, [llList2Integer(cast, -1)-1], -1, -1), 0, 2);
144 if(llList2Integer(cast, -1) <= 0) // Ground is gone, we must have just walked off a ledge, so check rail-wise under us to hit the ledge face.
145 {
146 smooth = TRUE;
147 cast = llCastRay(pos + (rot2up*-0.275) + (movementOffset*0.275), pos + (rot2up*-0.275) + (movementOffset*-0.275), [RC_REJECT_TYPES, RC_REJECT_PHYSICAL, RC_DATA_FLAGS, RC_GET_NORMAL, RC_MAX_HITS, 2]);
148 }
149 if(llList2Key(cast, 0) == llGetOwner()) cast = llDeleteSubList(llListReplaceList(cast, [llList2Integer(cast, -1)-1], -1, -1), 0, 2);
150 if(llList2Integer(cast, -1) <= 0) // There's no ledge face either? Do a big cast down to see if we hit something.
151 {
152 smooth = FALSE;
153 cast = llCastRay(pos, pos+<0,0,-30>, [RC_REJECT_TYPES, RC_REJECT_PHYSICAL, RC_DATA_FLAGS, RC_GET_NORMAL, RC_MAX_HITS, 2]);
154 }
155 if(llList2Key(cast, 0) == llGetOwner()) cast = llDeleteSubList(llListReplaceList(cast, [llList2Integer(cast, -1)-1], -1, -1), 0, 2);
156 if(llList2Integer(cast, -1) > 0)
157 {
158 integer i;
159 vector normal = llList2Vector(cast, i+2);
160 rot *= llRotBetween(rot2up, normal);
161 if(smooth) jumpHeight = 0;
162 pos = llList2Vector(cast, i+1) + normal*(0.25+jumpHeight);
163 if(left) rot = llEuler2Rot(<0,0,TURN_SPEED>) * rot;
164 if(right) rot = llEuler2Rot(<0,0,-TURN_SPEED>) * rot;
165 pos += llVecNorm(movementOffset)*MOVE_SPEED*boost;
166 if(llVecDist(llGetPos(), pos) < 10.0)
167 if(smooth) smoothRotate(llGetRot(), rot, llGetPos(), pos, 300);
168 llSetLinkPrimitiveParamsFast(LINK_THIS, [PRIM_POSITION, pos, PRIM_ROTATION, rot]);
169 }
170 else // ... There was nothing when we casted from below either! We're lost. Continue acceleration due gravity and fall. We're bound to hit something eventually.
171 {
172 if(!notJumping)
173 llSetLinkPrimitiveParamsFast(LINK_THIS, [PRIM_POSITION, llGetPos() + <0,0,jumpSpeed>*rot]);
174 }
175 }
176
178 {
180 {
182 llStopAnimation("sit");
183 llStartAnimation(STAND_ANIM);
184 }
185 }
186
187 control(key id, integer level, integer edge)
188 {
189 rot = llGetRot();
190 rot2fwd = llRot2Fwd(rot);
191 rot2left = llRot2Left(rot);
192 rot2up = llRot2Up(rot);
194 boost = 1.0;
195 integer run; // simple test to see if we should run or not.
196 movementOffset = <0,0,0>;
197 if(info & AGENT_MOUSELOOK)
198 {
200 rotation face = llEuler2Rot(<0,0,eul.z>);
201 llSetLinkPrimitiveParamsFast(2, [PRIM_ROT_LOCAL, face]);
202 if(CONTROL_FWD & level)
203 {
204 run++;
205 movementOffset += llRot2Fwd(face * llGetRot());
206 }
207 if(CONTROL_BACK & level)
208 {
209 run++;
210 movementOffset -= llRot2Fwd(face * llGetRot());
211 }
212 if(CONTROL_LEFT & level)
213 {
214 movementOffset += llRot2Left(face * llGetRot());
215 }
216 if(CONTROL_RIGHT & level)
217 {
218 movementOffset -= llRot2Left(face * llGetRot());
219 }
220 if(run == 2)
221 {
222 if(notJumping)
223 llStartAnimation(RUN_ANIM);
224 movementOffset += llRot2Fwd(face * llGetRot());
225 boost = 2.5;
226 }
227 else if(notJumping)
228 {
229 llStartAnimation(WALK_ANIM);
230 }
231 }
232 else
233 {
234 llSetLinkPrimitiveParamsFast(2, [PRIM_ROT_LOCAL, ZERO_ROTATION]);
235 if(CONTROL_FWD & level)
236 {
237 run++;
238 movementOffset += rot2fwd;
239 }
240 if(CONTROL_BACK & level)
241 {
242 run++;
243 movementOffset -= rot2fwd;
244 }
245 if(CONTROL_LEFT & level) movementOffset += rot2left;
246 if(CONTROL_RIGHT & level) movementOffset -= rot2left;
247 if(run == 2)
248 {
249 if(notJumping)
250 llStartAnimation(RUN_ANIM);
251 movementOffset += rot2fwd;
252 boost = RUN_BOOST;
253 }
254 else if(notJumping) llStartAnimation(WALK_ANIM);
255 }
256 if(movementOffset == ZERO_VECTOR)
257 {
258 if(notJumping) llStartAnimation(STAND_ANIM);
259 llStopAnimation(WALK_ANIM);
260 llStopAnimation(RUN_ANIM);
261 }
262 if(CONTROL_FWD & ~level && CONTROL_BACK & ~level && CONTROL_LEFT & ~level && CONTROL_RIGHT & ~level)
263 {
264 llStopAnimation(WALK_ANIM);
265 llStopAnimation(RUN_ANIM);
266 }
267 if(CONTROL_ROT_LEFT & level)
268 {
269 left = TRUE;
270 }
271 else
272 {
273 left = FALSE;
274 }
275 if(CONTROL_ROT_RIGHT & level)
276 {
277 right = TRUE;
278 }
279 else
280 {
281 right = FALSE;
282 }
283 if(CONTROL_UP & level & edge)
284 {
285 if(notJumping)
286 {
287 notJumping = FALSE;
288 llStartAnimation("jump");
289 llStopAnimation(WALK_ANIM);
290 llStopAnimation(RUN_ANIM);
291 llStopAnimation(STAND_ANIM);
292 jumpSpeed = JUMP_FORCE;
293 }
294 }
295 }
296 }
297 //EOF

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