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
Useful Subroutines Rotate_object_towards_another_objec  

Rotate_object_towards_another_objec

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.

Category: Useful Subroutines
By : Chalice Yao
Created: 2010-11-16 Edited: 2010-11-16
Worlds: Second Life

the Zip file

Download all files for Rotate_object_towards_another_objec
Contents are in zip format, with .LSL (text) source code and LSLEdit (text + Solution) formats.
Get file # 1. Rotate_object_towards_another_objec_1.lsl
Get file # 2. Rotate_object_towards_another_objec_2.lsl
Get file # 3. Rotate_object_towards_another_objec_3.lsl
Get file # 4. Rotate_object_towards_another_objec_4.lsl
Get file # 5. Rotate_object_towards_another_objec_5.lsl
1 llSetRot(llRotBetween(<1,0,0>,llVecNorm(targetPosition - llGetPos())));

Rotate_object_towards_another_objec

Awesome, isn't it? That one line does al...

Category: Useful Subroutines
By : Chalice Yao
Created: 2010-11-16 Edited: 2010-11-16
Worlds: Second Life

1 vector vTarget=target position;
2 vector vPos=llGetPos(); //object position
3 float fDistance=llVecDist(<vTarget.x,vTarget.y,0>,<vPos.x,vPos.y,0>); // XY Distance, disregarding height differences.

Rotate_object_towards_another_objec

And then we get back to the little first line of code in this post, modified:

Category: Useful Subroutines
By : Chalice Yao
Created: 2010-11-16 Edited: 2010-11-16
Worlds: Second Life

1 llRotBetween(<1,0,0>,llVecNorm(<fDistance,0,vTarget.z - vPos.z>))

Rotate_object_towards_another_objec

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:

Category: Useful Subroutines
By : Chalice Yao
Created: 2010-11-16 Edited: 2010-11-16
Worlds: Second Life

1 llRotBetween(<1,0,0>,llVecNorm(<vTarget.x - vPos.x,vTarget.y - vPos.y,0>))

Rotate_object_towards_another_objec

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!

Category: Useful Subroutines
By : Chalice Yao
Created: 2010-11-16 Edited: 2010-11-16
Worlds: Second Life

1 vector vTarget=llList2Vector(llGetObjectDetails("targetkey",[OBJECT_POS]),0);
2 vector vPos=llGetPos(); //object position
3 float fDistance=llVecDist(<vTarget.x,vTarget.y,0>,<vPos.x,vPos.y,0>); // XY Distance, disregarding height differences.
4 llSetRot(llRotBetween(<1,0,0>,llVecNorm(<fDistance,0,vTarget.z - vPos.z>)) * llRotBetween(<1,0,0>,llVecNorm(<vTarget.x - vPos.x,vTarget.y - vPos.y,0>)));

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