Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Rotation help please?

Galena Qi
Registered User
Join date: 9 Sep 2006
Posts: 249
02-16-2008 12:10
In spite of all the threads on this topic, I can't figure out what I'm doing wrong. I have a dialog that either moves an animated object forward or backward on its y-axis or turns it 45 degrees to the left. As soon as I put in the code for the left turn it messed up the forward/back part of the script. The local rotation of the root isn't changing as the object turns, so it moves at an angle instead of forward/back. Here's the script:

integer CHANNEL = 42; // dialog channel
list MENU_MAIN = ["Walk", "Back Up", "Turn Left"]; // the main menu
integer counter = 0;
float delay = 0.4;
float forward = 1.0;
float backward = -1.0;

default {
state_entry() {
llListen(CHANNEL, "", NULL_KEY, "";); // listen for dialog answers (from multiple users)
}

touch_start(integer total_number)
{
llDialog(llDetectedKey(0), "", MENU_MAIN, CHANNEL); // present dialog on click
}

listen(integer channel, string name, key id, string message)
{

if (llListFindList(MENU_MAIN, [message]) != -1) // verify dialog choice
{
llWhisper(0, name + " picked the option '" + message + "'.";); // output the answer
// here you have the name and key of the user and can easily verify if they have the permission to use that option or not

}

if (message == "Walk";)
{
do {
integer frame = 1;
llMessageLinked(LINK_ALL_CHILDREN, 0, (string)frame, NULL_KEY);
llSleep(delay);
llSetPos(llGetPos() + <0, forward, 0>;);
frame = 2;
llMessageLinked(LINK_ALL_CHILDREN, 0, (string)frame, NULL_KEY);
llSleep(delay);
llSetPos(llGetPos() + <0, forward, 0>;);
frame = 3;
llMessageLinked(LINK_ALL_CHILDREN, 0, (string)frame, NULL_KEY);
llSleep(delay);
llSetPos(llGetPos() + <0, forward, 0>;);
frame = 4;
llMessageLinked(LINK_ALL_CHILDREN, 0, (string)frame, NULL_KEY);
llSleep(delay);
llSetPos(llGetPos() + <0, forward, 0>;);
counter = counter + 1;
}
while (counter < 4); // Repeat cycle set number of times.
llResetScript();
}
else if (message == "Back Up";)
{
do {
integer frame = 4;
llMessageLinked(LINK_ALL_CHILDREN, 0, (string)frame, NULL_KEY);
llSleep(delay);
llSetPos(llGetPos() + <0, backward, 0>;);
frame = 3;
llMessageLinked(LINK_ALL_CHILDREN, 0, (string)frame, NULL_KEY);
llSleep(delay);
llSetPos(llGetPos() + <0, backward, 0>;);
frame = 2;
llMessageLinked(LINK_ALL_CHILDREN, 0, (string)frame, NULL_KEY);
llSleep(delay);
llSetPos(llGetPos() + <0, backward, 0>;);
frame = 1;
llMessageLinked(LINK_ALL_CHILDREN, 0, (string)frame, NULL_KEY);
llSleep(delay);
llSetPos(llGetPos() + <0, backward, 0>;);
counter = counter + 1;
}
while (counter < 4); // Repeat cycle set number of times.
llResetScript();
}
else if (message == "Turn Left";)
{
// a rotation of 45 degrees around the z-axis
rotation z_45 = llEuler2Rot( <0,0,45 * DEG_TO_RAD> );
rotation new_rot = z_45 * llGetRot(); // compute local rotation
llSetRot(new_rot); // orient the object accordingly
llResetScript();
}

}

on_rez(integer param)
{
llResetScript();
}

}
Hewee Zetkin
Registered User
Join date: 20 Jul 2006
Posts: 2,702
02-16-2008 13:01
llSetPos() uses global coordinates. It sounds like you always want to move in the direction of the objects (positive or negative) y-axis. You'll want something like:

CODE

llSetPos(llGetPos()+<0, forward, 0>*llGetRot());


or

CODE

llSetPos(llGetPos()+forward*llRot2Left(llGetRot()));


Note that SL's default frame of reference is with the x-axis "forward", the y-axis to the "left", and the z-axis "up". That's why I used llRot2Left() (local y-axis) instead of llRot2Fwd() (local x-axis).
Stephen Psaltery
Registered User
Join date: 4 Jan 2005
Posts: 19
02-16-2008 13:03
I'f I'm understanding what you are saying properly then the problem is that you are not rotating your normalized forward vector after having rotated the prim.

Simply change this line:

llSetPos(llGetPos() + <0, forward, 0>;);

with this line:
llSetPos(llGetPos() + (<0, forward, 0> * llGetRot()));

Do the same for the backwards function. :)
_____________________
Respectfully,
St Psaltery
Galena Qi
Registered User
Join date: 9 Sep 2006
Posts: 249
02-16-2008 15:05
Thank you all - this forum is the best!