xtc4u Posted November 14, 2012 Share Posted November 14, 2012 Trying to write a general rotator of child prim routine. The code below works but only rotates on a single axis whether I pass in x, y or z. I'm sure it's a matter of basic misunderstanding of rotation but I am stumped. Whether I call MoveChildsRot as below I get the same rotation on the Y axis:MoveChildsRot(10, "x", 2);llSleep(2);MoveChildsRot(10, "y", 2);llSleep(2);MoveChildsRot(10, "z", 2); // calc the rotation rotation rotAmount ( float amt, string xyz) { vector x_angles = < amt,0,0 >; vector y_angles = < 0,amt,0 >; vector z_angles = < 0,0,amt >; vector angles_in_radians; if (xyz = "x") { angles_in_radians = x_angles* DEG_TO_RAD; // Change to Radians } if (xyz = "y") { angles_in_radians = y_angles* DEG_TO_RAD; // Change to Radians } if (xyz = "z") { angles_in_radians = z_angles* DEG_TO_RAD; // Change to Radians } if (debug) llOwnerSay("Angle in Radians: " + (string)angles_in_radians); return llEuler2Rot ( angles_in_radians ); // Change to a Rotation } // Rotate a child prim // nAmount: number of degrees to rotate (i.e 10, -10) // str_xyz: axis to rotate (i.e. "x", "y", "z") // nun: child prim number MoveChildsRot(integer nAmount, string str_xyz, integer num) { // rotate a child prim list lRot = llGetLinkPrimitiveParams(num, [PRIM_ROT_LOCAL]); rotation localRot = llList2Rot(lRot, 0); // current Rot rotation deltaRot; if (debug) llOwnerSay( (string)nAmount); if (debug) llOwnerSay("xyz: " + str_xyz + " " + (string)localRot); deltaRot = rotAmount ((float)nAmount,str_xyz) ; llSleep(.5); if (debug) llOwnerSay("xyz2: " + str_xyz + " Delta: " + (string)deltaRot); llSetLinkPrimitiveParamsFast(num , [ PRIM_ROT_LOCAL, (localRot * deltaRot)] ) ; } Link to comment Share on other sites More sharing options...
Rolig Loon Posted November 14, 2012 Share Posted November 14, 2012 Two mistakes, one fatal: (1) You are using "=" (assignment) in the if statements in the rotAmount function, instead of "==" (equivalence), and (2) Your second and third if tests should be else if tests. rotation rotAmount ( float amt, string xyz){ vector x_angles = < amt,0,0 >; vector y_angles = < 0,amt,0 >; vector z_angles = < 0,0,amt >; vector angles_in_radians; if (xyz == "x") { angles_in_radians = x_angles* DEG_TO_RAD; // Change to Radians } else if (xyz == "y") { angles_in_radians = y_angles* DEG_TO_RAD; // Change to Radians } else if (xyz == "z") { angles_in_radians = z_angles* DEG_TO_RAD; // Change to Radians } if (debug) llOwnerSay("Angle in Radians: " + (string)angles_in_radians); return llEuler2Rot ( angles_in_radians ); // Change to a Rotation} 1 Link to comment Share on other sites More sharing options...
xtc4u Posted November 14, 2012 Author Share Posted November 14, 2012 Wow, talk about a big time rookie mistake. I have stared at this code for you can't imagine how long, I really should have spotted that. Thanks much. As to the if vs. if else. I agree I should have used the if else's but wouldn't it have worked even with just the if's. I'm assuming it would have but would be less efficient code. I guess that is what you meant by one fatal and one "not fatal". Again thank you. Link to comment Share on other sites More sharing options...
Rolig Loon Posted November 14, 2012 Share Posted November 14, 2012 Yeah. That one's not fatal, but there's no point in continuing with extra if tests once you have found the one that works, so else if is the better choice. Link to comment Share on other sites More sharing options...
Ela Talaj Posted November 14, 2012 Share Posted November 14, 2012 An interesting thing here is that if left as is it should always rotate around all 3 coordinates because xyz = <whateverstring> evaluates to TRUE so condition if(xyz=<whateverstring>) (with "=" instead of "==") is always satisfied. Link to comment Share on other sites More sharing options...
Nexii Malthus Posted November 14, 2012 Share Posted November 14, 2012 Not quite correct. It would always use the Z coordinate because the last assignment would win every time. Link to comment Share on other sites More sharing options...
xtc4u Posted November 15, 2012 Author Share Posted November 15, 2012 OK here is the final version, tightened up a bit and only one function. Thanks for the help and second set of eyes. Note it does not check if the prim number is valid. // Rotate a child prim // degrees: number of degrees to rotate (i.e 10, -10)// xyz: axis to rotate (i.e. "x", "y", "z")// prim_nun: child prim numberMoveChildsRot(float degrees, string xyz, integer prim_num){ list lRot = llGetLinkPrimitiveParams(prim_num, [PRIM_ROT_LOCAL]); vector angles_in_radians; if (xyz == "x") { angles_in_radians = < degrees,0,0 > * DEG_TO_RAD; } else if (xyz == "y") { angles_in_radians = < 0,degrees,0 > * DEG_TO_RAD; } else if (xyz == "z") { angles_in_radians = < 0,0,degrees > * DEG_TO_RAD; } // rotate the child prim llSetLinkPrimitiveParamsFast(prim_num , [ PRIM_ROT_LOCAL,(llList2Rot(lRot, 0) * llEuler2Rot ( angles_in_radians ))] ) ; } Link to comment Share on other sites More sharing options...
Ela Talaj Posted November 15, 2012 Share Posted November 15, 2012 Correct. Didn't notice that each conditional writes the same variable so the last value remains. Late at nite when seeing several if's in a row and no else's, one might assume they would write into different variables Link to comment Share on other sites More sharing options...
Recommended Posts
Please take a moment to consider if this thread is worth bumping.
Please sign in to comment
You will be able to leave a comment after signing in
Sign In Now