Need help calculating angle between objects
|
|
Bones Outlander
Registered User
Join date: 16 Jan 2008
Posts: 30
|
02-03-2008 04:52
Hi, I am trying to write a HUD script and I want it to tell me the angle between me and another avatar I'm tracking relative to the direction I'm facing, rather than relative to true North. I can calculate the distance between me and the other avatar, but I can't work out how to determine the angle between us. So assuming I have the vectors MyPos and AviPos, is there a way of calculating the angle between these two positions relative to the direction I'm facing. So when somebody is to my left they will always be WEST of me regardless of where true North is. I hope this makes sense  Thanks
|
|
Trep Cosmo
Registered User
Join date: 3 Mar 2005
Posts: 101
|
02-03-2008 06:11
Pretty sure you're looking for llRotBetween(). Too tired to come up with the exact solution for you. But if you pick an imaginary point in front of you, and use that offset and the offset between you and the other avatar then plug those into llRotBetween() I think it should give you the rotation between those two.
_____________________
"There is no 'I' in team, but there is a 'Me' if you scramble it." -- House
|
|
Jesse Barnett
500,000 scoville units
Join date: 21 May 2006
Posts: 4,160
|
02-03-2008 06:14
_____________________
I (who is a she not a he) reserve the right to exercise selective comprehension of the OP's question at anytime. From: someone I am still around, just no longer here. See you across the aisle. Hope LL burns in hell for archiving this forum
|
|
Ee Maculate
Owner of Fourmile Castle
Join date: 11 Jan 2007
Posts: 919
|
02-03-2008 07:06
This won't quite work because the vectors are in 3D, you should project the vectors onto the plane first before calculating the angle between them? I.e. you need to ignore the height. For example an avatar directly in front of you compass-wise but at an elevation of 45 degrees will return 45 degrees. If one of the vectors is foo then the easiest way to do this is set foo.z to 0 (foo.z is the z component of the vector). The only problem with this is if someone is directly above you you will get a zero vector from this so cannot use llRotBetween()... you would have to check for this first.
|
|
Lex Neva
wears dorky glasses
Join date: 27 Nov 2004
Posts: 1,361
|
02-03-2008 08:51
Try llAngleBetween(), passing in your avatar's rotation and the rotation of the target avatar. Since avatars that aren't sitting on something always have an X and Y axis rotation of 0 (I believe), you'll just get the differences in "headings". You'll need to experiment a little to figure out whether negative means left or right of you (I believe it should be right).
|
|
Hewee Zetkin
Registered User
Join date: 20 Jul 2006
Posts: 2,702
|
02-03-2008 12:36
This gives you the angle (in degrees or radians) between the forward direction of your avatar and the straight-line direction of the other avatar. vector pos = llGetPos(); rotation rot = llGetRot(); vector facing = llRot2Fwd(rot); vector relativePos = otherAvatarPos-pos; vector relativeDir = llVecNorm(relativePos); float angleInRad = llAcos(facing*relativeDir); float angleInDeg = angleInRad*RAD_TO_DEG;
If you don't care about vertical and just want the horizontal corollary, replace the last two lines with: vector horizFacing = llVecNorm(<facing.x, facing.y, 0.0>); vector relativeHorizDir = llVecNorm(<relativeDir.x, relativeDir.y, 0.0>); float angleInRad = llAcos(horizFacing*relativeHorizDir); float angleInDeg = angleInRad*RAD_TO_DEG;
(Just be careful with this one if either the avatar or relative direction is straight up/down. That case might merit some conditional tests so the llVecNorm() calls don't go crazy with division by zero.)
|
|
Bones Outlander
Registered User
Join date: 16 Jan 2008
Posts: 30
|
02-03-2008 13:12
Thanks to everybody for their replies,
I've tried Howee's example and it works, thanks Howee I really appreciate you help.
The only thing is I need to convert the angle to N,S,E,W etc so need the returned angle to go from 0 to 360 degrees.
Is there an easy way of doing that?
Many thanks.
|
|
Jesse Barnett
500,000 scoville units
Join date: 21 May 2006
Posts: 4,160
|
02-03-2008 13:58
Take a look at Talarus's script here: /54/79/207542/1.html#post1657700You could strip the cardinal points part out for your script.
_____________________
I (who is a she not a he) reserve the right to exercise selective comprehension of the OP's question at anytime. From: someone I am still around, just no longer here. See you across the aisle. Hope LL burns in hell for archiving this forum
|
|
Bones Outlander
Registered User
Join date: 16 Jan 2008
Posts: 30
|
02-04-2008 22:38
Thanks, but I'm really new to this and all this vector stuff is totally messign with my head! I just can't work out how to combine this script and Hewee's to get the result I need. I'm sure it's easy when you know how, but I've still got training wheels on 
|
|
Hewee Zetkin
Registered User
Join date: 20 Jul 2006
Posts: 2,702
|
02-04-2008 23:38
From: Bones Outlander I just can't work out how to combine this script and Hewee's to get the result I need. Well, one thing I didn't understand when I posted my solution is that you wanted a 2D bearing, not an absolute angle between two (2D or 3D) vectors. My code will return at most PI radians (180 degrees). Here is some code that will return the angle you really want from your description (again cases of straight up/down are left as a logic excercise for the reader): vector pos = llGetPos(); rotation rot = llGetRot(); vector localX = llRot2Fwd(rot); vector basisX = llVecNorm(<localX.x, localX.y, 0.0>); vector basisY = <-basisX.y, basisX.x, 0.0>; // z%x
vector posOffset = otherAvatarPos-pos; float x = posOffset*basisX; float y = posOffset*basisY;
float bearingRad = llAtan2(y, x); float bearingDeg = bearingRad*RAD_TO_DEG;
|
|
Bones Outlander
Registered User
Join date: 16 Jan 2008
Posts: 30
|
02-05-2008 12:45
Thanks Hewee,
This script returns me the following.
If the person is directly in front of me (or close to it) I get someting close to 0 If the person is to my LEFT I get 90 If the person is to my RIGHT I get -90 If the person is BEHIND me I get something close to either 180 or -180 depending if they are slightly left/right. Not sure what I get if they are exactly behind me....
Is this what you expected?
|