Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Partial rotational calulator needed.

Foolish Frost
Grand Technomancer
Join date: 7 Mar 2005
Posts: 1,433
04-22-2007 17:30
To be honest, I HATE rotational math. Never seems to make sense...

I need a function in the line of this:

rotation partialrotation(rotation start, rotation end, float percentbetween)

This idea is that it it returns a rotation the percent between the two rotations given. .5 would offer the rotation halfway between the two.

If someone could be kind enough to offer information on this, I would be grateful. Is it even possible?

Thank you all for your time.
Lex Neva
wears dorky glasses
Join date: 27 Nov 2004
Posts: 1,361
04-23-2007 08:43
Okay, let's see if my rotation-fu is up to the challenge.

CODE

rotation partialRotation(rotation start, rotation end, rotation ratioBetween) {
rotation between = end / start;

vector axis = llRot2Axis(between);
float angle = llRot2Angle(between);

angle *= ratioBetween;

return llAxisAngle2Rot(axis, angle);
}



So, theoretically, if an object is at the orientation start, if you want to rotate it to 1/4 of the way to the orientation end, you could do this:

CODE

llSetRot(partialRotation(start, end, 0.25) * llGetRot());


I think that's right. Caveat: I just woke up. Hopefully Seifert will come along and fix my math if I goofed. I'm not sure on the order of that multiplication in the line above.
Seifert Surface
Mathematician
Join date: 14 Jun 2005
Posts: 912
04-24-2007 12:25
From: Lex Neva
CODE

llSetRot(partialRotation(start, end, 0.25) * llGetRot());


I think that's right. Caveat: I just woke up. Hopefully Seifert will come along and fix my math if I goofed. I'm not sure on the order of that multiplication in the line above.
I think it should be the other way round, but I'd just code it up and flip things around until it works right :)

I'd be a little concerned about the axis and angle stuff too. I don't remember if the returned angle is in [0, 2*Pi), or (-Pi, Pi], and those will give different results, and you may want one or the other result. Presumably you'd want it to be in (-Pi, Pi], so maybe you have to do some fiddling to get it to do things right.
_____________________
-Seifert Surface
2G!tGLf 2nLt9cG
DancesWithRobots Soyer
Registered User
Join date: 7 Apr 2006
Posts: 701
04-24-2007 13:07
I knew it HAD to be Lex who would have the answer here.

quaternions <shudder>

/me goes back to counting curly braces.
Merry Mousehold
Registered User
Join date: 1 Jul 2005
Posts: 15
04-24-2007 16:29
From: Seifert Surface
I think it should be the other way round, but I'd just code it up and flip things around until it works right :)

I'd be a little concerned about the axis and angle stuff too. I don't remember if the returned angle is in [0, 2*Pi), or (-Pi, Pi], and those will give different results, and you may want one or the other result. Presumably you'd want it to be in (-Pi, Pi], so maybe you have to do some fiddling to get it to do things right.


Lex... i first want to thank you for your great start on this! It DOES work...

About 50% of the time.

Truth is this, sometimes, it takes the long way around the prim. I have been looking at the rotational math for a bit now, and realized that I just don't have the knowledge to understand at what point it needs to be reversed, or how to compare and angle.

What the function needs, i think, is to see if it can tell the difference between an angle greater than PI (180 deg), and then use the OTHER way around the prim.

I'm still looking, but if you 3d math specialists can offer another hand to a poor data-processing scripter, It would help alot.
Merry Mousehold
Registered User
Join date: 1 Jul 2005
Posts: 15
04-24-2007 16:47
Okay. Foolish has this now, and I'm posting it here too for all of you to use...

headtoward( vector there_pos, rotation there_rot, vector here_pos, rotation here_rot, float distance);


CODE
headtoward(	vector there_pos, rotation there_rot, 
vector here_pos, rotation here_rot,
float distance)
{


float totaldistance = llVecDist(here_pos, there_pos)/distance;
vector nextstep = ((there_pos - here_pos) / totaldistance ) + here_pos;
rotation nextrot = partialRotation(here_rot, there_rot, 1/totaldistance) * here_rot;

llSay(0,(string)totaldistance);

if ( within(there_pos, here_pos, distance * 1.1) )
{
//oldrot = llGetRot();
nextstep = there_pos;
nextrot = there_rot;
pointnumber = pointnumber + direction;
}

llSetPrimitiveParams([PRIM_POSITION, nextstep, PRIM_ROTATION, nextrot]);
//llSetPrimitiveParams([PRIM_POSITION, nextstep, PRIM_ROTATION, partialRotation(here_rot, there_rot, 1/totaldistance)]);
}


CODE
integer within (vector withintarget, vector withincurrent, float range) 
{
integer checksum = TRUE;
if (withincurrent.x < withintarget.x - range || withincurrent.x > withintarget.x + range )
checksum = FALSE;
if (withincurrent.y < withintarget.y - range || withincurrent.y > withintarget.y + range )
checksum = FALSE;
if (withincurrent.z < withintarget.z - range || withincurrent.z > withintarget.z + range )
checksum = FALSE;
return checksum;
}


CODE
rotation partialRotation(rotation start, rotation end, float ratioBetween) {
rotation between = end / start;

vector axis = llRot2Axis(between);
float angle = llRot2Angle(between);

if (angle > PI)
angle = angle - (PI*2);

angle *= ratioBetween;

return llAxisAngle2Rot(axis, angle);
}


I hope this helps a lot of scripters.
Foolish Frost
Grand Technomancer
Join date: 7 Mar 2005
Posts: 1,433
04-24-2007 16:52
Thanks to every single ONE of you people. I don't know HOW it works, but it seems to work perfectly now.

Lex, Merry: If I can ever do you two a favor, name it.
Lex Neva
wears dorky glasses
Join date: 27 Nov 2004
Posts: 1,361
04-25-2007 08:43
From: Seifert Surface
I think it should be the other way round, but I'd just code it up and flip things around until it works right :)


Yeah, I stared at that multiplication order for awhile, but I was pretty sure that the order I said was right. The partial rotation I was calculating was relative to the start rotation due to that quaternion division, so I needed to left-multiply. It does look weird though :)

From: someone

I'd be a little concerned about the axis and angle stuff too. I don't remember if the returned angle is in [0, 2*Pi), or (-Pi, Pi], and those will give different results, and you may want one or the other result. Presumably you'd want it to be in (-Pi, Pi], so maybe you have to do some fiddling to get it to do things right.


Looks like you were right on this one. Good on Merry for figuring out how to fix this :)

Quick note, Merry: the TWO_PI constant is likely to be more accurate (and ever so slightly more efficient).

Foolish: no problem, all in a day's play :D