Jump to content

Rotate an object around some axis


Wulfie Reanimator
 Share

You are about to reply to a thread that has been inactive for 1277 days.

Please take a moment to consider if this thread is worth bumping.

Recommended Posts

This is a repost/rephrasing of my original comment from another thread:

 

Let's say you have some rotation (for example, another object, or some orientation of your choice) and you want to calculate a new rotation around that. This can be done by converting the rotation to a vector, and changing one(!) of its components.

In this case, let's say you want your object to rotate around a vertical (Z) axis, just like how you would spin if you were turning around while standing up. This can be done by simply adding the desired angle to the Z component like this: ("rad" is short for "radian", instead of degree)

rotation rotate(rotation R, float rad_angle)
{
    vector V = llRot2Euler(R);
    V.z += rad_angle;

    return llEuler2Rot(V);
}

 

So what's so special about this? Well, the cool thing is that if instead of ZERO_ROTATION (where the Z component matches the world's Z axis or "up" direction) we had some totally arbitrary rotation, such as <0.297147, 0.872802, -0.380856, 0.069782>, the Z component would still be the "up" direction of that rotation. Let's use proof-of-concept to show that it works:

rotation rotate(rotation R, float rad_angle)
{
    vector V = llRot2Euler(R);
    V.z += rad_angle;

    return llEuler2Rot(V);
}

default
{
    state_entry()
    {
        key target = "9c5cce0f-5559-6fd8-d023-8672ad91b9a0";

        while (1)
        {
            rotation target_rot = llList2Rot(llGetObjectDetails(target, [OBJECT_ROT]), 0);
            rotation r = rotate(target_rot, llGetTime());
            llSetRot(r);
        }
    }
}

 

The target is the blue arrow, pointing along its Z axis. Every time the loop repeats, the white prim (with the above script) will match its rotation with the blue arrow, with an angle (based on time) added to it to make it appear to spin around itself. The same method can be used to move the object itself around some point.

rotation rotate(rotation R, float rad_angle)
{
    vector V = llRot2Euler(R);
    V.z += rad_angle;

    return llEuler2Rot(V);
}

default
{
    state_entry()
    {
        key target = "955211a9-9398-a1c3-a63b-b34d03efb805";

        while (1)
        {
            rotation target_rot = llList2Rot(llGetObjectDetails(target, [OBJECT_ROT]), 0);
            vector   target_pos = llList2Vector(llGetObjectDetails(target, [OBJECT_POS]), 0);

            rotation r = rotate(target_rot, llGetTime());
            vector   offset = <0.3, 0, 0>;

            llSetPos(target_pos + (offset * r));
        }
    }
}

 

  • Like 3
Link to comment
Share on other sites

You are about to reply to a thread that has been inactive for 1277 days.

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
 Share

×
×
  • Create New...