Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Determining direction from one vector to another (waypoint)

Phate Shepherd
Addicted to code
Join date: 14 Feb 2008
Posts: 96
06-14-2008 14:38
I'm cooking up a dash for use in vehicles, and one thing I would like is a waypoint indicator... basically an indicator to point back to your start, or last selected waypoint. Problem is, I have no clue how to figure out the angle (around the z axis) between two given points. Here is a simplified example....

Can anyone point me in the right direction for figuring out the angle?
-----------------------
vector waypoint ;
vector current ;
float reverseheading ;

default
{

state_entry()
{
waypoint = llGetRegionCorner() + llGetPos() ;
llSetTimerEvent(.5) ;
}

touch_start (interger total_number)
{
waypoint = llGetRegionCorner() + llGetPos() ;
}

timer()
{
current = llGetRegionCorner() + llGetPos() ;

// Now here is where I'm baffled. How do I determine the angle
// (around the z axis) FROM current to waypoint?
// Conversely, how would I determine the angle FROM the waypoint to current?
// Just subtract PI, correct?

reverseheading = ????

llSay (0, (string) reverseheading) ;
}
}
Dora Gustafson
Registered User
Join date: 13 Mar 2007
Posts: 779
06-14-2008 16:16
It can be done:
If vector A = current - waypoint then you can find the angle between A and EAST.
The angle the other way is the angle between B and EAST where: vector B = waypoint - current.
Assuming east is 0 and you operate in one plane in normal XY coordinates.
However angles are not of much use in LSL, Unless you want to display a heading there are other much better means to handle directions and rotations.
See http://www.lslwiki.net/lslwiki/wakka.php?wakka=rotation
The angle between two vectors in LSL terms could be:
CODE

float angle( vector U, vector V )
{ // return angle in radians
return llAcos( llVecNorm( U )*llVecNorm( V ));
}
_____________________
From Studio Dora
Hewee Zetkin
Registered User
Join date: 20 Jul 2006
Posts: 2,702
06-14-2008 16:34
CODE

float dir = waypoint-current;
float theta = llAtan2(dir.y, dir.x);
Phate Shepherd
Addicted to code
Join date: 14 Feb 2008
Posts: 96
06-15-2008 11:50
Thank you both!

Hewee's solution was quick and dirty. The only other change was to subtract out the planes rotation so that my arrow always pointed home relative to the planes orientation.

Now I have a pretty glass HUD on my blimp with an arrow that always points home ;-)

Won't ever get lost touring the skies of Azure Islands.

Since I'm learning, and I need comments to build my skills, here is the final waypoint system.

// Waypoint Script for vehicles. Attach to cylinder and set the top texture (face 0)
// to an arrow pointing east.
// Phate Shepherd - Free to use, credit appreciated.

vector globalwaypoint ;
vector localwaypoint ;
string msg ;
vector dir ;
float curtheta ;
float lasttheta ;
vector curRot ;

default
{

state_entry()
{
localwaypoint = llGetPos() ;
globalwaypoint = llGetRegionCorner() + localwaypoint ;
msg = "Current waypoint:\n" + llGetRegionName() +" " + (string)((integer)localwaypoint.x) + "," + (string)((integer)localwaypoint.y) + "\nTouch to set new waypoint\n\n " ;
llSetText( msg, <1, 0, 0>, 1.0) ;
lasttheta = 99 ;
llSetTimerEvent(.25) ;
}

touch_start (integer total_number)
{
localwaypoint = llGetPos() ;
globalwaypoint = llGetRegionCorner() + localwaypoint ;
msg = "Current waypoint:\n" + llGetRegionName() +" " + (string)((integer)localwaypoint.x) + "," + (string)((integer)localwaypoint.y) + "\nTouch to set new waypoint\n\n " ;
llSetText( msg, <1, 0, 0>, 1.0) ;
lasttheta = 99 ;
}

timer()
{
dir = globalwaypoint - (llGetRegionCorner() + llGetPos()) ;
curRot = llRot2Euler( llGetRot() );
curtheta = llAtan2(dir.y, dir.x) - curRot.z ;
// Avoid excess updates
if (llFabs(lasttheta - curtheta) > 0.0174)
{
llRotateTexture(curtheta,0) ;
lasttheta = curtheta ;
}
}
}