Jump to content

How to rotate Camera with LSL


Carina103
 Share

Recommended Posts

hello everyone, i am trying to make a tank in second life, so my question is how can i rotate the camera when i press left or right the? the camera should always be behind the user showing user body. the code is at such

 

key specificAvatar = NULL_KEY; // Variable to store the UUID of the specific avatar
integer specificPrim = 2;      // Link number of the specific prim to rotate
integer noPrim = 999;

back_view(float degrees)
{
    rotation sitRot = llAxisAngle2Rot(<0, 0, 1>, degrees * DEG_TO_RAD);

    llSitTarget(<0, 0, 0.1>, sitRot);

    llSetCameraEyeOffset(<-2, 0, 1> * sitRot);
    llSetCameraAtOffset(<2, 0, 1> * sitRot);
}

updateCamera(rotation newRot)
{
    vector eyeOffset = <-2, 0, 1> * newRot;
    vector atOffset = <2, 0, 1> * newRot;

    llOwnerSay("Updating camera...");
    llOwnerSay("Eye Offset: " + (string)eyeOffset);
    llOwnerSay("At Offset: " + (string)atOffset);

    llSetCameraEyeOffset(eyeOffset);
    llSetCameraAtOffset(atOffset);
}

default
{
    state_entry()
    {
        llSitTarget(<0.0, 0.0, 0.1>, ZERO_ROTATION);
    }

    changed(integer change)
    {
        if (change & CHANGED_LINK)
        {
            key avatar = llAvatarOnLinkSitTarget(specificPrim);
            if (avatar != NULL_KEY)
            {
                noPrim = llGetNumberOfPrims();
                specificAvatar = avatar; // Store the UUID of the avatar sitting on the specific prim
                llRequestPermissions(avatar, PERMISSION_TAKE_CONTROLS | PERMISSION_CONTROL_CAMERA);

                back_view(0);
            }
            else
            {
                specificAvatar = NULL_KEY; // Reset the stored UUID when the avatar stands up
                llReleaseControls();
                noPrim = 999;
            }
        }
    }

    run_time_permissions(integer perm)
    {
        if (perm & PERMISSION_TAKE_CONTROLS)
        {
            llTakeControls(CONTROL_FWD |
                           CONTROL_BACK |
                           CONTROL_LEFT |
                           CONTROL_RIGHT |
                           CONTROL_ROT_LEFT |
                           CONTROL_ROT_RIGHT |
                           CONTROL_UP |
                           CONTROL_DOWN |
                           CONTROL_LBUTTON |
                           CONTROL_ML_LBUTTON,
                           TRUE, FALSE);
        }
    }

    control(key id, integer level, integer edge)
    {
        string controlKey = "";
        rotation currentRot;
        rotation newRot;

        // Determine which control key is pressed
        if (level & CONTROL_FWD)
            controlKey = "Forward";
        else if (level & CONTROL_BACK)
            controlKey = "Backward";
        else if (level & CONTROL_LEFT || level & CONTROL_ROT_LEFT)
        {
            controlKey = "Left";
            // Rotate the specific prim and the sitter left by 15 degrees based on user's direction
            currentRot = llList2Rot(llGetLinkPrimitiveParams(specificPrim, [PRIM_ROT_LOCAL]), 0);
            newRot = currentRot * llEuler2Rot(<0, 0, 15> * DEG_TO_RAD);
            llSetLinkPrimitiveParamsFast(specificPrim, [PRIM_ROT_LOCAL, newRot]);
            llSetLinkPrimitiveParamsFast(noPrim, [PRIM_ROT_LOCAL, newRot]);
            updateCamera(newRot);
        }
        else if (level & CONTROL_RIGHT || level & CONTROL_ROT_RIGHT)
        {
            controlKey = "Right";
            // Rotate the specific prim and the sitter right by 15 degrees based on user's direction
            currentRot = llList2Rot(llGetLinkPrimitiveParams(specificPrim, [PRIM_ROT_LOCAL]), 0);
            newRot = currentRot * llEuler2Rot(<0, 0, -15> * DEG_TO_RAD);
            llSetLinkPrimitiveParamsFast(specificPrim, [PRIM_ROT_LOCAL, newRot]);
            llSetLinkPrimitiveParamsFast(noPrim, [PRIM_ROT_LOCAL, newRot]);
            updateCamera(newRot);
        }
        else if (level & CONTROL_UP)
            controlKey = "Up";
        else if (level & CONTROL_DOWN)
            controlKey = "Down";
        else if (level & CONTROL_LBUTTON)
            controlKey = "Left Mouse Button";
        else if (level & CONTROL_ML_LBUTTON)
            controlKey = "Middle Mouse Button";

        // Output the pressed control key to the owner's chat
        if (controlKey != "")
            llOwnerSay("Control Key Pressed: " + controlKey);
    }
}

Link to comment
Share on other sites

The best you can do is use llSetCameraParams to change the 'focus offset' slightly left or right. See:

the most relevant portion is:

vector turn = 
  <0,-1,0>*!(level&CONTROL_ROT_LEFT)+
  <0, 1,0>*!(level&CONTROL_ROT_RIGHT);

llSetCameraParams(
[   CAMERA_ACTIVE,TRUE,
    CAMERA_FOCUS_OFFSET, (0.75*turn)+<5,0,0.50>
]);

 

Link to comment
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share

×
×
  • Create New...