Jump to content

left, right, behind, forward


77Diablo34
 Share

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

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

Recommended Posts

I have been playing with this code for a few days and I'm having some trouble. I just can't get it to pick up left, right, behind, forward at all. maybe I some fresh eyes on it would help me find the error in my ways. Trying to do away with a sensor on it as well. I have others plans on how it will gain the Key I'm looking to check. At this point its just to get left, right, behind, forward working.

 

integer Find(vector baseline, vector target)
{
    //Returns whether the target vector is to the left or right of the baseline vector
    //returns 0
    //straight ahead = 0 (baseline == target)
    //left  = 1
    //right = -1
    //directly behind = 2
    
    rotation rot_between = llRotBetween(baseline,target);
    vector euler_rot = llRot2Euler(rot_between);
    float z_rot = euler_rot.z;
    
    if (llVecNorm(baseline) == llVecNorm(target))
    {
        return 0;
    }
    if (llVecNorm(baseline) == llVecNorm(target) * -1)
    {
        return 2;
    }
    if (z_rot > 0)
    {
        return 1;
    }
    else
    {
        return -1;
    }
}

Test()
{
    //list A = llGetObjectDetails(llGetOwner(), [OBJECT_POS, OBJECT_ROT]);
    
    
    list data = llGetObjectDetails(llGetOwner(), [OBJECT_POS, OBJECT_ROT]);
    vector target_pos = llList2Vector(data, 0);
    rotation target_rot = llList2Rot(data, 1);
    
    // Arrow from the object to my avatar's position.
    //vector direction = llGetPos() - target_pos;
    
    
    vector direction = (llGetPos() - target_pos) / target_rot;
    
    //integer Place = Find(llRot2Fwd(llList2Rot(A,1)),llList2Vector(A,0) - llGetPos());
    integer Place = Find(llRot2Fwd(llGetRot()),direction);
    llOwnerSay((string)Place); 
}


default
{
    state_entry()
    {
        Test();
        //llOwnerSay("Left or Right example");
    }
    
    touch_start(integer total_number)
    {
        //llOwnerSay("Detecting Avatar");
        //llSensor("","",AGENT,60,PI);
    }

    //sensor(integer num)
    //{
        //integer target_L_or_R = left_or_right(llRot2Fwd(llGetRot()),llDetectedPos(0) - llGetPos());     
        //if (target_L_or_R == 1)
        //{
            //llOwnerSay("Avatar is to the left of me");
        //}
        //else
        //{
            //llOwnerSay("Avatar is to my right");
        //}
        //integer moving_L_or_R = left_or_right(llRot2Fwd(llGetRot()),llRot2Fwd(llDetectedRot(0)));  
        //if (moving_L_or_R == 1)
        //{
            //llOwnerSay("Avatar is facing to my left");
        //}

        //else
        //{
            //llOwnerSay("Avatar is facing to my right");
        //}      
    //}
}

Link to comment
Share on other sites

I see that your baseline vector is defined but no rotation is given - is it intended that the baseline never has a rotation?

I can see that causing issues down the line if the object is placed on an angle and needs to determine the position of the avatar relative to itself.

(I'll look at your script in depth a little later, need to finish some other things first!)

Link to comment
Share on other sites

Okay, I've made some modifications to get that working:

TLDR; you were overcomplicating things with a lot of functions which weren't really needed - I've stripped most of them out so you can see how everything is being calculated.

 integer Find(vector baseline, vector target, rotation rot)
{
    //Returns whether the target vector is to the left or right of the baseline vector
    //returns 0
    //straight ahead = 0 (baseline == target)
    //left  = 1
    //right = -1
    //directly behind = 2
    
    integer deadzone = 10; //How many degrees we consider as a deadzone (the 2 vectors provided are close enough to be considered directly in front or behind)
    
    rotation r2 = llRotBetween(llRot2Fwd(rot),llVecNorm(target-baseline));
    vector test = llRot2Euler(r2)*RAD_TO_DEG;
    float ang = test.z; //Angle in degrees target is relative to base
    
    if (llFabs(ang) < deadzone) //object is within the deadzone.
    {
        return 0;
    }
    else if (llFabs(ang) > (180-deadzone)) //object within deadzone, behind
    {
        return 2;
    }
    else if (ang > 0)
    {
        return 1;
    }
    else
    {
        return -1;
    }
}

Test()
{
    list data = llGetObjectDetails(llGetOwner(), [OBJECT_POS]);
    vector target_pos = llList2Vector(data, 0);
    
    integer Place = Find(llGetPos(),target_pos,llGetRot());
    llOwnerSay((string)Place); 
}


default
{
    state_entry()
    {
        //Test();
        llOwnerSay("Left or Right example");
    }
    
    touch_start(integer total_number)
    {
        llWhisper(0,"You touched me!");
        Test();
    }
} 

I made some modifications to the Find function to make it a little easier to use (namely, be able to supply the rotation of the baseline object in the event it's not the object containing the script)

Also: llRotBetween calculates direction relative to <0,0,0> - so don't feed it 2 position vectors, feed it 2 normalized vectors and you're all good.

Edited by Jenna Huntsman
Link to comment
Share on other sites

I'm kinda skimming the code examples, but assuming you have a person at pos1, with a rotation Rot, and you want to know if pos2 is to the left or right:

vector cross = llRot2Fwd(Rot) % (pos2-pos1);
integer leftRight = (cross.z>0) - (cross.z<0);
// left and right are confusing, 1 is one of them, -1 the other, 0 is straight ahead or behind.

 

Edited by Quistess Alpha
made variable names in verbal description match code.
  • Like 1
Link to comment
Share on other sites

Indeed's Quistess trying to use it in a hud for a game I'm working on. The idea is to know what side the person is on of you for offsets on my other commands I'm using. 

Some commands will use a 360 arch while others will only work if there in a cone or half sphere. But its all based off, if there to the sides of the person front or back.

Link to comment
Share on other sites

You are about to reply to a thread that has been inactive for 865 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...