Jump to content
trivis

Positioning myself left or right of a target avatar

Recommended Posts

Somehow I thought this would be easy, but I am still struggling with some rotation things (what's new lol).

I am building a positioning HUD. This HUD needs to position me an exact distance left or right of a target-avatar. My rotation is not important (as i understood that a HUD cant rotate me after positioning). The id of the target avatar is known by using llSensor.

The positioning of the target is, of course important.

My question is how can I determine if I am standing somewhere left or right of my target-avatar (I can be standing in front of the or behind it), and after that positioning myself that exact distance left or right of my target?

 

Thanks in advance for your help,

Trivis

Share this post


Link to post
Share on other sites

Thank you @Mollymews! 

This will help for efficiently positioning beside the target avatar!

 

  • Like 1

Share this post


Link to post
Share on other sites

Still curious how i can decide if I am standing somewhere left or right of an target avatar. Any idea? Is llVecNorm the solution?

Share this post


Link to post
Share on other sites
3 hours ago, trivis said:

Still curious how i can decide if I am standing somewhere left or right of an target avatar. Any idea? Is llVecNorm the solution?

Things you need:

  1. Your position.
  2. Your target's position.

From there, you can calculate a vector (XYZ direction) from you to your target. (Or the other way around, doesn't really matter.)

Then, if you can check one of the axes (X, Y, or Z) to see if it's negative or not. If it is, you're on one side. If it's not, you're on the other. (The specific one you need to use depends a bit on how you've calculated the vector.)

I understand this might seem vague, but the actual vector calculation can be done in two (or more) clean lines of code. Read up on llRotBetween and llRot2Euler.

I could just post an example script but since it involves a bit more thinking, it's important that you're able to actually understand how to do it. I suggest you just make a whole new script and try to just make it detect which side you're on, rather than try to edit your current script and fit it in there before it works. Showing debug messages for yourself with llOwnerSay or llSetText so you can see what the numbers are actually doing is also a very important skill you should learn if you haven't already.

Edited by Wulfie Reanimator
  • Like 1

Share this post


Link to post
Share on other sites

Thanks Wulfie!,

The new script isnt a problem, i am not testing and trying in an existing script :-)

I will try to calculate and understand what you wrote where the solution is! But... the rotation of the target avatar is also involved, isnt it? Looks to me that this rotation is just the issue :)

Thanks again,

Trivis

Edited by trivis

Share this post


Link to post
Share on other sites

Worked on the problem... it works fine when I use a "point object" which is left or right of a "avatar object". 

When the avatar object is a real avatar it doesnt work.

The script is:

default {
    touch_start(integer total_number) {
        key id; 
        id = llGetOwner(); // avatar
//        id = "e9f7952f-2ac4-0cb7-db9d-4f9a61509593"; // object
        // target avatar
        vector avPos = llList2Vector(llGetObjectDetails(id,[OBJECT_POS]),0);
        rotation avRot = llList2Rot(llGetObjectDetails(id,[OBJECT_ROT]),0);
        // the math
        vector A = avPos - <0,1,0>*avRot;
        vector B = avPos + <0,1,0>*avRot;
        vector C = llGetPos();
        vector AB = B - A;
        vector AC = C - A;
        float diff = (B.x-A.x)*(C.y-A.y)-(B.y-A.y)*(C.x-A.x);
        // left or right
        // diff > 0 left, diff < 0 right, diff == 0 front/back
        if (diff > 0 ) llOwnerSay("I am on the left");
        if (diff < 0 ) llOwnerSay("I am on the right");
    }
}

Is there anyone who is able to pull me out this deep well?

Share this post


Link to post
Share on other sites

Well you had quite a different (plain math) approach to it, so you lose me on the diff part. 😂

But you're right, there is one rotational thing you have to consider. Let me explain.

Here's an example object, it's actually two cylinders cut in half, left and right. You can also see the region coordinates (XYZ) on my avatar.
103776c82e.png

First, let's figure out which direction my avatar is from the object's center.

list data = llGetObjectDetails(target_avatar, [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;

5789c36355.png

You can see that X is negative, which is correct because my avatar is slightly behind the object in region coordinates. Y and Z are positive because I'm on the left and above the object. Wait, that's it? Y is positive when I'm on the left! Almost -- there's a catch. The catch is that everything is done in global coordinates right now. The calculation breaks (or rather, the result doesn't change) if the target object rotates.
5e27b75152.png

To fix this, I'll just take the target's rotation into account. (Actually, it's closer to negating the object's rotation.)

direction = direction / target_rot;

// Or as a single line instead:
vector direction = (llGetPos() - target_pos) / target_rot;

Why divide? What about multiply? Read the wiki for detailed explanations. (But in simple terms, the object is rotated 90 degrees to the right. I want to rotate the calculated direction 90 degrees back to the left so we're back in global coordinates.)
http://wiki.secondlife.com/wiki/Rotation#Combining_Rotations

And now I could add some code to check if Y is positive or negative, and do stuff. It will work even if the object was standing up or at any arbitrary angle.

 

Edited by Wulfie Reanimator
  • Like 1
  • Thanks 1

Share this post


Link to post
Share on other sites

Wow that is amazing @Wulfie Reanimator!!!!!

You've put so much effort in answering my question. You're a hero!

Will program this later today, if you dont mind, have to do some work first :-)

T H A N K S

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...