# Rotating a compass object with avatar rotation

## Recommended Posts

I've got a compass object worn on my avatar and would like it to always be rotated towards a location in world no matter what direction the avatar who wears it is facing.

At the moment the object points in the correct direction but only when the avatar is facing North.

Facing North - https://i.gyazo.com/bbd35b98bd439b2971448a12f1aaeeac.png

In both images the arrow continues to point in the same way but the avatar is facing a different direction. I need the arrow to change to continue facing <0,0,0> when the avatar rotates.

Code

```vector vTarget = <0,0,0>;
vector vPos = llGetPos(); //object position
float fDistance=llVecDist(<vTarget.x,vTarget.y,0>,<vPos.x,vPos.y,0>); // XY Distance, disregarding height differences.
llSetRot(llRotBetween(<1,0,0>,llVecNorm(<fDistance,0,0>)) * llRotBetween(<1,0,0>,-llVecNorm(<vTarget.x - vPos.x,vTarget.y - vPos.y,0>)));```

SOLUTION

```vector vTarget = llList2Vector(waypoints, current_waypoint);
rotation vRot = llGetRot();
vector vAvatar = llGetPos();
vector direction = (vTarget - vAvatar) / vRot;
direction.z = 0;
direction = llVecNorm(direction);
rotation final = llRotBetween(<1,0,0>, -direction);

Edited by NotTooManyItems
##### Share on other sites

The most important thing is to negate the avatar's rotation, if you're trying to reference a position in world-coordinates. Otherwise what you have is a relative offset, which means that it will change with the avatar as it rotates.

This can be done by "dividing" the rotation by the avatar's rotation. (It's not really division, that's just the symbol we use for it.)

```vector target = <0,0,0>;
vector avatar = llGetPos();

vector direction = target - avatar;
direction.z = 0;
direction = llVecNorm(direction);

rotation final = llRotBetween(<1,0,0>, direction) / llGetRot();

Edited by Wulfie Reanimator
• 1
• 1
##### Share on other sites

Unless I'm confused, I think it may look better if the object is attached to http://wiki.secondlife.com/wiki/ATTACH_AVATAR_CENTER so it doesn't appear to rotate based on the avatar's animations.

##### Share on other sites
12 minutes ago, Qie Niangao said:

Unless I'm confused, I think it may look better if the object is attached to http://wiki.secondlife.com/wiki/ATTACH_AVATAR_CENTER so it doesn't appear to rotate based on the avatar's animations.

It will still rotate according to the avatar's general direction. When you turn left, Avatar Center turns as well.

##### Share on other sites

Yeah, wearing this on AV Center will encounter the +/- 30 degree turning buffer users have which is responsible for one person seeing you facing one direction and another seeing you facing slightly different than that.

• 1
##### Share on other sites
8 hours ago, Wulfie Reanimator said:

The most important thing is to negate the avatar's rotation, if you're trying to reference a position in world-coordinates. Otherwise what you have is a relative offset, which means that it will change with the avatar as it rotates.

This can be done by "dividing" the rotation by the avatar's rotation. (It's not really division, that's just the symbol we use for it.)

```
vector target = <0,0,0>;
vector avatar = llGetPos();

vector direction = target - avatar;
direction.z = 0;
direction = llVecNorm(direction);

rotation final = llRotBetween(<1,0,0>, direction) / llGetRot();

Thanks! Dividing by llGetRot() did it! And the llSetLinkPrimitiveParamsFast() function made the rotation smoother!

• 1
##### Share on other sites
On 2/24/2021 at 6:25 PM, Wulfie Reanimator said:

It will still rotate according to the avatar's general direction. When you turn left, Avatar Center turns as well.

On 2/24/2021 at 7:42 PM, Lucia Nightfire said:

Yeah, wearing this on AV Center will encounter the +/- 30 degree turning buffer users have which is responsible for one person seeing you facing one direction and another seeing you facing slightly different than that.

I wonder if I'm just not explaining what I mean. Certainly "when you turn left, Avatar Center turns as well" -- the distinction I was trying to make is that Avatar Center has the advantage of remaining oriented in the same direction as the agent, regardless of how animations move the avatar. Or at least that's been my experience. Here's a simple demonstration:

```integer animated;

default
{
state_entry()
{
llSetTimerEvent(1.0);
}
run_time_permissions(integer perms)
{
if (PERMISSION_TRIGGER_ANIMATION & perms)
if (animated = !animated)
llStartAnimation("turn_180");
else
llStopAnimation("turn_180");
}
touch_start(integer total_number)
{
llRequestPermissions(llGetOwner(), PERMISSION_TRIGGER_ANIMATION);
}
timer()
{
vector direction = (<0,0,0> - llGetPos()) / llGetRot();
direction = llVecNorm(<direction.x, direction.y, 0.0>);
}
}```

Drop that in some prim that shows the orientation of its X axis, then attach it to Pelvis and touch it. It will spin around according to the avatar's "turn_180" animation. Touch it again to stop the animation, then try it attached to Avatar Center instead; it should retain the agent's orientation (which will still turn left with the left arrow key, etc).

##### Share on other sites
Just now, Qie Niangao said:

I wonder if I'm just not explaining what I mean. Certainly "when you turn left, Avatar Center turns as well" -- the distinction I was trying to make is that Avatar Center has the advantage of remaining oriented in the same direction as the agent, regardless of how animations move the avatar.

You're totally correct, but I believe that's what @NotTooManyItems was already doing and that's the assumption I made with the script in my first post.

• 1
##### Share on other sites
On 2/24/2021 at 7:42 PM, Lucia Nightfire said:

Yeah, wearing this on AV Center will encounter the +/- 30 degree turning buffer users have which is responsible for one person seeing you facing one direction and another seeing you facing slightly different than that.

Just for my own curiosity, do the 'AvatarRotateThresholdSlow' and 'AvatarRotateThresholdFast' debug settings have any effect on your avatar's rotation as seen by other viewers?

Edited by Quistessa
##### Share on other sites
51 minutes ago, Quistessa said:

Just for my own curiosity, do the 'AvatarRotateThresholdSlow' and 'AvatarRotateThresholdFast' debug settings have any effect on your avatar's rotation as seen by other viewers?

i don't think so. Debug settings only apply to our viewer