User:Tapple Gao/3D Spinning Pendulum Motion

From Second Life Wiki
Jump to navigation Jump to search

3D Spinning Pendulum Motion

Generalized undamped pendulum motion in 3 dimensions. Can swing in simple arcs, circles, elipses, or lissajous curves, and spin at the same time.
Suitable for regular pendulums, swings, spinning tire swings, spinning tops, bobbleheads, rocking boats, etc.
Script has a menu of demo motions

// Spinning pendulum script 1.3 by Tapple Gao
// http://wiki.secondlife.com/wiki/User:Tapple_Gao/3D_Spinning_Pendulum_Motion

// speed up or slow down all animations:
float speed = 0.5; // Speed Multiplier. 2.0 for double speed, 0.5 for half speed

// Change these to configure the "custom" item in the menu
vector spinAxis = <0,0,1>;      // axis to spin on
vector majorSwingAxis = <0,1,0>; // axis to swing about
float length = 8.0;  // how long is the animation, in seconds
float frameRate = 5.0; // frames per second. Must be between 0.0 and 9.0
integer majorSwingDegrees = 40;  // How far the swing moves, forward to back
integer minorSwingDegrees = 20;  // How far from the vertical the swing moves, side to side
integer majorSwingCount = -2; // How many times I swing forward to back
integer minorSwingCount = -2; // How many times I swing side to side. Usually set to the same as majorSwingCount
integer phaseDegrees = 90; // timing difference between major and minor swinging motion. Set to 90 for eliptical motion
integer spinCount = 1; // How many times the tire swing spins per animation

// End of user configuration

/////////////////////////////////////////////////////////
///////////// Spinning Pendulum Library /////////////////
/////////////////////////////////////////////////////////

integer isSwinging = FALSE;
rotation savedRot;

rotation NormRot(rotation Q) {
    float MagQ = llSqrt(Q.x*Q.x + Q.y*Q.y +Q.z*Q.z + Q.s*Q.s);
    return <Q.x/MagQ, Q.y/MagQ, Q.z/MagQ, Q.s/MagQ>;
}

stopPendulum() {
    llSetKeyframedMotion([], [KFM_COMMAND, KFM_CMD_STOP]);
    if (!isSwinging) return;

    llSetLinkPrimitiveParamsFast(!!llGetLinkNumber(), [PRIM_ROT_LOCAL, savedRot]);
    isSwinging = FALSE;
}

// Causes a prim to swing like a pendulum or spin like a top, depending on parameters. Implements arbitrary pendulum motion in 3d
// spinAxis: The axis to spin about, in prim-local coordinates. must be nonzero
// majorSwingAxis: The primary axis to swing about, in prim-local coordinates. must be nonzero
// length: The length of the animation, in seconds. Must be positive
// frameRate: The smoothness of the animation, in frames per second. Must be between 0.0 and 9.0
// majorSwingAngle: The angle that the prim swings around the majorSwingAxis, in radians. can be positive, negative, or zero
// minorSwingAngle: The angle that the prim swings around the minor swing axis, in radians. minor swing axis is perpendicular to majorSwingAxis. can be positive, negative, or zero
// majorSwingCount: Number of times to swing about the major axis during the animation. Can be positive, negative, or zero. Negative numbers will cause the prim to swing in the oposite direction
// minorSwingCount: Number of times to swing about the minor axis during the animation. Can be positive, negative, or zero. Negative numbers will cause the prim to swing in the oposite direction. Set this to the same number as majorSwingCount for eliptical swinging motion. Set it to other values for unrealistic, but interesting motion
// phase: timing difference between major and minor swinging, in radians. Set this to PI_BY_TWO for eliptical motion
// spinCount: Number of times to spin during the animation. Can be positive, negative, or zero. If zero, the prim will swing only, without spinning. if negative, spin the other way
spinningPendulum(vector spinAxis, vector majorSwingAxis, float length, float frameRate, float majorSwingAngle, float minorSwingAngle, integer majorSwingCount, integer minorSwingCount, float phase, integer spinCount) {
    stopPendulum();
    savedRot = llGetRootRotation();
    isSwinging=TRUE;

    vector minorSwingAxis = spinAxis % majorSwingAxis;
    majorSwingAxis = minorSwingAxis % spinAxis;
    rotation currentRot;

    length /= speed;
    integer frameCount = (integer)(length * frameRate);
    float frameTime = length / frameCount;
    list frames = [];
    integer frame;
    for (frame = 0; frame <= frameCount; frame++) {
        rotation nextRot = llAxisAngle2Rot(spinAxis, frame * spinCount * TWO_PI / frameCount) *
            llAxisAngle2Rot(majorSwingAxis, llCos(frame * TWO_PI / frameCount * majorSwingCount) * majorSwingAngle) *
            llAxisAngle2Rot(minorSwingAxis, llCos(frame * TWO_PI / frameCount * minorSwingCount - phase) * minorSwingAngle);
        //llSetRot(nextRot * savedRot); llSleep(0.1);
        if (frame == 0) llSetLinkPrimitiveParamsFast(!!llGetLinkNumber(),
            [PRIM_ROT_LOCAL, nextRot*savedRot]);
        else frames += [NormRot(nextRot / currentRot), frameTime];
        currentRot = nextRot;
    }
    //llOwnerSay(llList2CSV(frames)); 
    llSetKeyframedMotion(frames, [KFM_MODE, KFM_LOOP, KFM_DATA, KFM_ROTATION]);
}

/////////////////////////////////////////////////////////
//////////////////////// SCRIPT /////////////////////////
/////////////////////////////////////////////////////////

integer menuHandle;

doMenu(key id) {
    llListenRemove(menuHandle);
    integer menuChannel = -1 - (integer)llFrand(329847);
    menuHandle = llListen(menuChannel, "", id, "");
    llDialog(id, "Choose a demo",
        ["swing", "tire swing", "top", "bobblehead", "rocking boat", "custom", "stop"],
        menuChannel);
}

default {
    touch_start(integer num) {
        doMenu(llDetectedKey(0));
    }

    listen(integer channel, string name, key id, string msg) {
        doMenu(id);
        if (msg == "swing") {
            spinningPendulum(
                <0,0,1>, // spinAxis
                <0,1,0>, // majorSwingAxis
                4.0, // animation length, in seconds
                5.0, // frameRate
                50*DEG_TO_RAD, // majorSwingAngle
                0*DEG_TO_RAD, // minorSwingAngle
                1, // majorSwingCount
                0, // minorSwingCount
                90*DEG_TO_RAD, // phase
                0); // spinCount
        } else if (msg == "tire swing") {
            spinningPendulum(
                <0,0,1>, // spinAxis
                <0,1,0>, // majorSwingAxis
                8.0, // animation length, in seconds
                5.0, // frameRate
                40*DEG_TO_RAD, // majorSwingAngle
                20*DEG_TO_RAD, // minorSwingAngle
                -2, // majorSwingCount
                -2, // minorSwingCount
                90*DEG_TO_RAD, // phase
                1); // spinCount
        } else if (msg == "top") {
            spinningPendulum(
                <0,0,1>, // spinAxis
                <0,1,0>, // majorSwingAxis
                3.0, // animation length, in seconds
                5.0, // frameRate
                20*DEG_TO_RAD, // majorSwingAngle
                20*DEG_TO_RAD, // minorSwingAngle
                1, // majorSwingCount
                1, // minorSwingCount
                90*DEG_TO_RAD, // phase
                3); // spinCount
        } else if (msg == "bobblehead") {
            spinningPendulum(
                <0,0,1>, // spinAxis
                <0,1,0>, // majorSwingAxis
                15.0, // animation length, in seconds
                3.0, // frameRate
                50*DEG_TO_RAD, // majorSwingAngle
                20*DEG_TO_RAD, // minorSwingAngle
                4, // majorSwingCount
                3, // minorSwingCount
                30*DEG_TO_RAD, // phase
                0); // spinCount
        } else if (msg == "rocking boat") {
            spinningPendulum(
                <0,0,1>, // spinAxis
                <0,1,0>, // majorSwingAxis
                60.0, // animation length, in seconds
                2.5, // frameRate
                5*DEG_TO_RAD, // majorSwingAngle
                10*DEG_TO_RAD, // minorSwingAngle
                4, // majorSwingCount
                3, // minorSwingCount
                30*DEG_TO_RAD, // phase
                0); // spinCount
        } else if (msg == "custom") {
            spinningPendulum(
                spinAxis, // spinAxis
                majorSwingAxis, // majorSwingAxis
                length, // animation length, in seconds
                frameRate, // frameRate
                majorSwingDegrees*DEG_TO_RAD, // majorSwingAngle
                minorSwingDegrees*DEG_TO_RAD, // minorSwingAngle
                majorSwingCount, // majorSwingCount
                minorSwingCount, // minorSwingCount
                phaseDegrees*DEG_TO_RAD, // phase
                spinCount); // spinCount
        } else if (msg == "stop") {
            stopPendulum();
        }
    }
}

If you get the error "Only linksets which uses the new prim equivalency system may be animated.", set the object to physics type convex hull, or add the followin in the script:

    state_entry()
    {
        llSetLinkPrimitiveParamsFast(LINK_ROOT,
                [PRIM_PHYSICS_SHAPE_TYPE, PRIM_PHYSICS_SHAPE_CONVEX,
            PRIM_LINK_TARGET, LINK_ALL_CHILDREN,
                PRIM_PHYSICS_SHAPE_TYPE, PRIM_PHYSICS_SHAPE_NONE]);
    }

I did not include that in the script since it would break mesh builds with custom physics shapes