Sunday, May 20, 2007

Rotations and other irritations..

Ok, appears the solution to the rotation problem is simple. So simple that the only excuse for not finding it right away might be the tiredness.

Here's the code to rotate the prim around a fixed local point on it, expressed as an offset from its center in prim's local coordinates. There are far too many excess variables, I kept them just for the sake of clarity, indeed in the real implementation this would be way shorter. The below code rotates the prim 45 degrees around the Y-axis in local coordinates, around the point in the center of its Z-coordinate top.


// how much to rotate per iteration (in degrees).
integer rotateAngle = 45;

// current rotation of the prim.
rotation rot = llGetRot();

// "size" of the prim.
vector scale = llGetScale();

// offset of the rotation point in prim's local coordinates.
// (center of the top in this case).
vector offset = <0, 0, scale.z/2>;

// "delta" rotation expressed as a vector in radians.
vector euler = <0, -1*rotateAngle*DEG_TO_RAD, 0>;

// "delta" rotation expressed as a rotation.
rotation deltarot = llEuler2Rot(euler);

// new rotation for the prim after applying the "delta" rotation.
rotation newrot = deltarot * rot;

// current offset of the rotation point vs. the prim center.
// now expressed in global coordinates.
vector vvv = offset * rot;

// new offset of the rotation point vs. the prim center.
// expressed in global coordinates.
vector vvv2 = offset * newrot;

// current position of the prim.
vector currentPos = llGetPos();

// current rotation point, not needed except for visualization.
//vector rotPoint = llGetPos() + vvv;

// new position for the prim.
// it is different from the current position.
// by the difference of the rotation points offsets.
vector newPos = currentPos + (vvv - vvv2);

// visualize the rotation point if needed.
//llRezObject("ball", rotPoint, ZERO_VECTOR, ZERO_ROTATION, 0);

// set the new position and rotation for the prim.
llSetPos(newPos);
llSetRot(newrot);



p.s. very nice, ths template seems to mess up the formatting.. If you copypaste - either do it from "view source" or manually fix the comment lines - i end each of them with a dot. (I know, I should've fixed the template for the blog instead, but i am lazy :)

No comments: