Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Rotation in steps

Lewis Nerd
Nerd by name and nature!
Join date: 9 Oct 2005
Posts: 3,431
07-07-2006 05:49
Ok..... what I'm trying to do is move something 5m, rotate 5 degrees, repeat 18 times for something automated going round a curve.

This is the relevant part of the script... yet it seems to rotate once then stop. Evidently there's a trick to nested loops but I haven't figured it out. If anyone can help that would be appreciated.

Lewis

CODE

{
for (y = 1; y < 18; y++)

llSetPrimitiveParams([PRIM_ROTATION, <0, 0, 5*y, 1>] ); // rotate 5 degrees

for (x = 1; x < 20; x++)
{
llSetPos (llGetPos()+<0,0.4,0>); // move forward a total of 5m
}
}
_____________________
Second Life Stratics - your new premier resource for all things Second Life. Free to join, sign up today!

Pocket Protector Projects - Rosieri 90,234,84 - building and landscaping services
Joannah Cramer
Registered User
Join date: 12 Apr 2006
Posts: 1,539
07-07-2006 06:02
From: Lewis Nerd
CODE

llSetPrimitiveParams([PRIM_ROTATION, <0, 0, 5*y, 1>] ); // rotate 5 degrees

This doesn't rotate things by 5 degrees. Rotations by default are given in radians, not to mention quaternions aren't like vectors.

Use:
CODE

llSetPrimitiveParams([PRIM_ROTATION, llEuler2Rot( <0, 0, 5*y*DEG_TO_RAD> ) ] );

instead to build the correct rotation quaternion.

In addition would say your loops aren't nested in a way that'll give you what you want. You have one loop that rotates object all way 18*5 degree in small steps, _then_ another that moves things all way forward by 20*0.4 m again in small steps. Maybe it's what you're after, though o.O;

edit: also note that rotation code sets fixed rotation value, rather than rotate from current angle.
Lewis Nerd
Nerd by name and nature!
Join date: 9 Oct 2005
Posts: 3,431
07-07-2006 06:17
The degrees/quat problem certainly won't help... I'll try that instead, thanks for noticing :) Did I ever mention I'm a beginner scripter?

The other part I need to go away and think about, I think you're right that I'm approaching it the wrong way, but I'm not quite sure how the right way would be. Perhaps I need to be thinking along an arc instead of actual regular X/Y movement.

Why LSL uses quaternions when the build interface uses degrees I will never figure out.

Lewis
_____________________
Second Life Stratics - your new premier resource for all things Second Life. Free to join, sign up today!

Pocket Protector Projects - Rosieri 90,234,84 - building and landscaping services
Bitzer Balderdash
Dazed and Confused
Join date: 21 Dec 2005
Posts: 246
07-07-2006 06:25
From: Lewis Nerd

CODE

llSetPos (llGetPos()+<0,0.4,0>); // move forward a total of 5m


Also note that the above line will _always_ move the prim in the +ve y direction of the sim that it is on regardless of the orientation of the prim itself.

try something like
CODE

for (y = 0; y < 18; y++)
{
llSetRot(llEuler2Rot( <0, 0, 5*y*DEG_TO_RAD> ));
for (x = 0; x < 10; x++)
{
llSetPos (llGetPos()+<0,0.5,0> * llGetRot()); // move forward a total of 5m
}
}


the above is completely untried and untested - even if it compiles I don't know if it will work. I _think_ that it is correct to multply the vector position by the quarernion rotation, but without playing with it in world, I can't be sure.

What I think that will do is eighteen repetitions of a small rotation and a 10-step move (5m total)
Joannah Cramer
Registered User
Join date: 12 Apr 2006
Posts: 1,539
07-07-2006 06:34
From: Lewis Nerd
The other part I need to go away and think about, I think you're right that I'm approaching it the wrong way, but I'm not quite sure how the right way would be. Perhaps I need to be thinking along an arc instead of actual regular X/Y movement.

The small steps are probably quite viable way to go, as long as you keep in mind your object keeps rotating... which means you can't use fixed vector to move it around and have it appear like it's moving along curve ^^;

the loop would be something like (pseudocode)

* retrieve current rotation
* extract forward vector component from it
* add this vector multiplied by object speed to current world position, to simulate movement forward
* alter the current rotation vector by your fixed rotation step, apply it to your object to simulate rotation along arc

... you can do this all with llGetRot(), llRot2Fwd(), llGetPos() and llSetRot() in this exact order

From: someone
Why LSL uses quaternions when the build interface uses degrees I will never figure out.

They're annoying, but at the same time very convenient to work with -- allow for very smooth rotations without common lock up issue you'll experience with Euler system. E.g. to rotate a quaternion by some amount (in your case 5 degree of z-axis rotation) you simply multiply current rotation quaternion by quaternion built from that <0, 0, 5> thing and you're good to go (although order of multiplying is important so watch for that)
Bitzer Balderdash
Dazed and Confused
Join date: 21 Dec 2005
Posts: 246
07-07-2006 06:37
From: Lewis Nerd
The degrees/quat problem certainly won't help... I'll try that instead, thanks for noticing :) Did I ever mention I'm a beginner scripter?

The other part I need to go away and think about, I think you're right that I'm approaching it the wrong way, but I'm not quite sure how the right way would be. Perhaps I need to be thinking along an arc instead of actual regular X/Y movement.

Why LSL uses quaternions when the build interface uses degrees I will never figure out.

Lewis


for an arc, try..... again, off the top of my head
CODE

float radius = 10.0;
vector center = llGetPos() - <0,radius,0>; // I _think_ I got the sign and axis right

for (theta=0; theta <= PI_BY_TWO ; theta += PI / 36)
{
float x = llSin(theta) * radius;
float y = llCos(theta) * radius;
llSetPos(center + <x,y,0>);
}


should give you 90 degrees of arc over 18 steps (so 5 deg each).

I'm pretty sure I offset the center pos correctly so you start with the prim at the start of the arc.... if you want more steps, increas the 36 that PI is divided by.

Again, you'll be lucky if that compiles, and even more lucky if it does what I expect, but it should be close
Lewis Nerd
Nerd by name and nature!
Join date: 9 Oct 2005
Posts: 3,431
07-07-2006 23:29
From: Bitzer Balderdash
Again, you'll be lucky if that compiles, and even more lucky if it does what I expect, but it should be close


It didn't compile ... heh it sat down at

CODE

for (theta=0; theta <= PI_BY_TWO ; theta += PI / 36)


with a "Type Mismatch" somewhere in that line.

Lewis
_____________________
Second Life Stratics - your new premier resource for all things Second Life. Free to join, sign up today!

Pocket Protector Projects - Rosieri 90,234,84 - building and landscaping services
Joannah Cramer
Registered User
Join date: 12 Apr 2006
Posts: 1,539
07-08-2006 05:49
From: Lewis Nerd
"Type Mismatch" somewhere in that line.

You'll need to declare

float theta;

somewhere before that loop. Type mismatch will happen if you declare it instead as integer or something similar ^^