Difference between revisions of "User:Strife Onizuka/UpdateLinkSitTarget"

From Second Life Wiki
Jump to navigation Jump to search
m
m
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{LSL Header}}
{{LSL Header}}
<lsl>//Sets / Updates the sit target moving the avatar on it if necessary.
<source lang="lsl2">//Sets / Updates the sit target moving the avatar on it if necessary.
UpdateLinkSitTarget(integer link, vector pos, rotation rot)
UpdateLinkSitTarget(integer link, vector pos, rotation rot)
{//Using this while the object is moving may give unpredictable results.
{//Using this while the object is moving may give unpredictable results.
Line 11: Line 11:
         {
         {
             integer linkNum = llGetNumberOfPrims();
             integer linkNum = llGetNumberOfPrims();
             do{
             do
            {
                 if(user == llGetLinkKey( linkNum ))//just checking to make sure the index is valid.
                 if(user == llGetLinkKey( linkNum ))//just checking to make sure the index is valid.
                 {
                 {
Line 18: Line 19:
                     if(llGetLinkKey(link) != llGetLinkKey(1))//only need the local rot if it's not the root.
                     if(llGetLinkKey(link) != llGetLinkKey(1))//only need the local rot if it's not the root.
                         local = llGetLinkPrimitiveParams(link, [PRIM_POS_LOCAL, PRIM_ROT_LOCAL]);
                         local = llGetLinkPrimitiveParams(link, [PRIM_POS_LOCAL, PRIM_ROT_LOCAL]);
                     pos.z += 0.4;
                     float fAdjust = ((((0.008906 * size.z) + -0.049831) * size.z) + 0.088967) * size.z;
                     llSetLinkPrimitiveParamsFast(linkNum, [
                     llSetLinkPrimitiveParamsFast(linkNum, [
                             PRIM_POS_LOCAL, ((pos - (llRot2Up(rot) * size.z * 0.02638)) * llList2Rot(local, 1)) + llList2Vector(local, 0),
                             PRIM_POS_LOCAL, ((pos + <0.0,0.0,0.4> - (llRot2Up(rot) * fAdjust)) * llList2Rot(local, 1)) + llList2Vector(local, 0),
                             PRIM_ROT_LOCAL, rot * llList2Rot(local, 1)
                             PRIM_ROT_LOCAL, rot * llList2Rot(local, 1)
                         ]);
                         ]);
Line 33: Line 34:
     }
     }
     @end;
     @end;
}//Written by Strife Onizuka, size adjustment provided by Escort DeFarge</lsl>
}//Written by Strife Onizuka, size adjustment provided by Talarus Luan</source>
<lsl>list GetSitTarget(integer prim, key av)
<source lang="lsl2">list GetSitTarget(integer prim, key av)
{//WARNING: llGetObjectDetails can introduce an error that goes as far as the 5th decimal place!
{//WARNING: The accuracy of the position vector is very good if av is seated upon the object
  //This is highly unlikely to be ever noticed unless compounded over time.
  //but greatly reduced otherwise.
//Do not use while moving (like in a moving vehicle)!!!
     vector tp = llGetAgentSize(av);
     vector tp = llGetAgentSize(av);
     if(tp)
     if(tp)
     {
     {
         list details = [OBJECT_POS, OBJECT_ROT];
         list details;
         integer linkNum = llGetNumberOfPrims();
         integer linkNum = llGetNumberOfPrims();
         do {
         do {//if it's part of the link set accuracy is higher
             if(av == llGetLinkKey(linkNum)) {
             if(av == llGetLinkKey(linkNum)) {
                 details = llGetLinkPrimitiveParams(link, [PRIM_POS_LOCAL, PRIM_ROT_LOCAL]);
                 list read = [PRIM_POS_LOCAL, PRIM_ROT_LOCAL];
                 if(llGetLinkKey(prim) != llGetLinkKey(1))
                details = llGetLinkPrimitiveParams(linkNum, read);
                     details += llGetLinkPrimitiveParams(prim, [PRIM_POS_LOCAL, PRIM_ROT_LOCAL]);
                 if(llGetLinkKey(prim) != llGetLinkKey(1))//no root prim! Easiest way to check imho
                     details += llGetLinkPrimitiveParams(prim, read);
                 jump next;
                 jump next;
             }
             }
         } while( --linkNum );
         } while( --linkNum );
        details = [OBJECT_POS, OBJECT_ROT]
         details = llGetObjectDetails(av, details) + llGetObjectDetails(llGetLinkKey(prim), details);
         details = llGetObjectDetails(av, details) + llGetObjectDetails(llGetLinkKey(prim), details);
         @next;
         @next;
         rotation f = llList2Rot(details, 1);
         rotation f = llList2Rot(details, 1);
         rotation r = llList2Rot(details, 3) / f;
         rotation r = llList2Rot(details, 3) / f;
         return [((llList2Vector(details, 2) - llList2Vector(details, 0)) / f) + (llRot2Up(r) * tp.z * 0.02638) - <0.0, 0.0, 0.4>, r];
        float fAdjust = ((((0.008906 * tp.z) + -0.049831) * tp.z) + 0.088967) * tp.z;
         return [((llList2Vector(details, 2) - llList2Vector(details, 0)) / f) + (llRot2Up(r) * fAdjust) - <0.0, 0.0, 0.4>, r];
     }
     }
     return [];
     return [];
}//Written by Strife Onizuka</lsl>
}//Written by Strife Onizuka, size adjustment provided by Talarus Luan</source>

Latest revision as of 16:48, 18 June 2015

//Sets / Updates the sit target moving the avatar on it if necessary.
UpdateLinkSitTarget(integer link, vector pos, rotation rot)
{//Using this while the object is moving may give unpredictable results.
    llLinkSitTarget(link, pos, rot);//Set the sit target
    key user = llAvatarOnLinkSitTarget(link);
    if(user)//true if there is a user seated on the sittarget, if so update their position
    {
        vector size = llGetAgentSize(user);
        if(size)//This tests to make sure the user really exists.
        {
            integer linkNum = llGetNumberOfPrims();
            do
            {
                if(user == llGetLinkKey( linkNum ))//just checking to make sure the index is valid.
                {
                    //We need to make the position and rotation local to the current prim
                    list local;
                    if(llGetLinkKey(link) != llGetLinkKey(1))//only need the local rot if it's not the root.
                        local = llGetLinkPrimitiveParams(link, [PRIM_POS_LOCAL, PRIM_ROT_LOCAL]);
                    float fAdjust = ((((0.008906 * size.z) + -0.049831) * size.z) + 0.088967) * size.z;
                    llSetLinkPrimitiveParamsFast(linkNum, [
                            PRIM_POS_LOCAL, ((pos + <0.0,0.0,0.4> - (llRot2Up(rot) * fAdjust)) * llList2Rot(local, 1)) + llList2Vector(local, 0),
                            PRIM_ROT_LOCAL, rot * llList2Rot(local, 1)
                        ]);
                    jump end;//cheaper but a tad slower then return
                }
            }while( --linkNum );
        }
        else
        {//It is rare that the sit target will bork but it does happen, this can help to fix it.
            llUnSit(user);
        }
    }
    @end;
}//Written by Strife Onizuka, size adjustment provided by Talarus Luan
list GetSitTarget(integer prim, key av)
{//WARNING: The accuracy of the position vector is very good if av is seated upon the object
 //but greatly reduced otherwise.
    vector tp = llGetAgentSize(av);
    if(tp)
    {
        list details;
        integer linkNum = llGetNumberOfPrims();
        do {//if it's part of the link set accuracy is higher
            if(av == llGetLinkKey(linkNum)) {
                list read = [PRIM_POS_LOCAL, PRIM_ROT_LOCAL];
                details = llGetLinkPrimitiveParams(linkNum, read);
                if(llGetLinkKey(prim) != llGetLinkKey(1))//no root prim! Easiest way to check imho
                    details += llGetLinkPrimitiveParams(prim, read);
                jump next;
            }
        } while( --linkNum );
        details = [OBJECT_POS, OBJECT_ROT]
        details = llGetObjectDetails(av, details) + llGetObjectDetails(llGetLinkKey(prim), details);
        @next;
        rotation f = llList2Rot(details, 1);
        rotation r = llList2Rot(details, 3) / f;
        float fAdjust = ((((0.008906 * tp.z) + -0.049831) * tp.z) + 0.088967) * tp.z;
        return [((llList2Vector(details, 2) - llList2Vector(details, 0)) / f) + (llRot2Up(r) * fAdjust) - <0.0, 0.0, 0.4>, r];
    }
    return [];
}//Written by Strife Onizuka, size adjustment provided by Talarus Luan