Join us in Outworldz at or follow us:

[Table of Contents]

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: Description: Creator:
Useful Subroutines This is something I've seen asked for a lot on various forums and in world. It could be useful for a few things I suppose, combat systems, path finding, what have you. // // The work here is done by a function that given the starting positions of two objects, and their velocities, it will tell you how close they'll get. In a combat system you might do something like assume all avatars have a radius of about one meter, and then use this to calculate how close an imaginary projectile could come to hitting someone. If it comes closer than one meter, it would be a hit. // // Here's the code in the form of an "Imaginary Gun".
Useful Subroutines integer_left_or_rightvector_baselin
Useful Subroutines List_Find_Last_Index
Useful Subroutines List_Get_Reverse_Order
Useful Subroutines List_Get_Reverse_Order_faster
Useful Subroutines List_MultiFind_Index_First_or_Last
Useful Subroutines llLookAt_any_axis
Useful Subroutines Reverse_String_Order_faster_version
Useful Subroutines Reverse_String_Order_smaller_memory
Useful Subroutines Here is a little snippet for the rotationally-challenged, or those who have some complex solution but want something much simpler. // How to rotate an object so it points its X axis (which is the 'forward' axis on prims) towards another object? // One complex solution is to take the object's rotation, take the other object's position, try to calculate the angle/rotation in between, adding it to the current rotation... // // Way too complex. It's much simpler to calculate the total rotation that needs to be set in one swoop.
Useful Subroutines Awesome, isn't it? That one line does all the magic. The logic here is that the llRotBetween returns the rotation between the norm'd vector <1,0,0>, which would be the forward vector of an object without any rotation applied (remember, X axis = forward in prims), and the normal vector that points from your object to the target. And that's the whole rotation that needs to be set. // // // Code: // // B // * // | Z height // | // A XYdistance | // *--------------------------- X axis (<1,0,0>) // // // Now, this is already dead seksi, but it can have an effect that some people want to avoid: the object that does the pointing will rotate around its own length. I.e. it will roll left/right along its body axis. How to avoid that? // // Solution: Do two different rotations. one that does the left/right turning, followed by a rotation that simply tilts the object up/down. Think of a classic concept like a gun turret. The turret barrel never rotates around the axis it points at, it just moves up/down and left/right. // // Hokay, let's do the up/down first. The easiest way to get that rotation is to act like both object positions have the same Y value, and just are their combined XY distance apart on the X axis. Think two-dimensional.
Useful Subroutines And then we get back to the little first line of code in this post, modified:
Useful Subroutines Ta-dah. This will return the rotation that will make our object point upward correctly at the other object. Not left/right, just the correct upwards angle. the 'vTarget.z - vPos.z' gets done to get the height difference between the two objects. // // Now how to do left/right? Easy, it uses the same idea as the above concept of getting the needed up/down rotation, except this time we disregard the Z coordinate and just use the XY difference. // // Like this: // Code: // // Y // ^ // | // | *B // | // | // | *A // | // ------------------> X (<1,0,0>) // // This time we don't even need to take care of any distance considerations. What we want, we'll get with:
Useful Subroutines There we go. The needed left/right rotation. Now, let's combine and get the result! // // We take the up/down rotation, and multiply the left/right roation to it..which is the way of combining rotations...and it basically goes in reverse order. Think of the left/right applied first before the up/down gets done to the result rotation. Basically read from right to left when mupltiplying rotations. // // And there we go. Correct rotation towards a target without rotating around the own axis. Hope this was educational!
Useful Subroutines Following the llGetObjectDesc code, the script goes through a series of if and else if statements comparing the string to my special selected settings. In this case, ‘debug on’ and ‘debug off’. Finding either of those, the script turns a debug flag on or off. You can get as complex as you want here as long as you don’t go past the 127 character limit. The object description could hold several keywords to be used to set several options within your script.
Useful Subroutines String_Find_Last_Index
Useful Subroutines Unix_time_code_to_list_format
Useful Subroutines example: // integer x = iif( z<0 , -1 , +1 ); // string hello = iif( sex == "M", "Man.", "Woman" );

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