Working on the connection of a vehicle in Second Life and a robot ( a simple Lego NXT) to steer in Real Life, the absolute angle had to be found in Second Life.
The problem of rotations in general is that is goes from 0 – 360 and then in one jump back to 0. So adding two of these can give strange results! For instance 180 + 185 = 5! Adding is not a problem, but finding an average might be: the average of 180 and 185 is 2.5, haha!
*(180 + 185) / 2 = 182.5 in our world, but because of the jump in the degree world this becomes 2.5!
Same problem here: the atan returns angles between 0 and 180 or 0 and -180, so the backwards angles between 180 and 360 had to be found considering the configuration of the objects. Not too difficult but something extra to consider.
The
Some experimenting resulted in the nice behavior of this function: it returns the full angle, from 0 – 360 degrees. This makes life easy!!
Two objects linked, one, sending its position to the root every half sec:
Script:
default
{
state_entry()
{
llSetTimerEvent(.5);
}
timer()
{
llMessageLinked(LINK_ROOT, 0, (string) llGetPos() , NULL_KEY);
}
}
and the root receiving the message and making an angle of this position and the position of his own:
default
{
state_entry()
{ }
link_message(integer sender_num, integer num, string str, key id)
{
vector myPos = llGetPos();
vector childPos = (vector) str;
vector diff = myPos - childPos;
float x = diff.x;
float y = diff.y;
angle = PI - llAtan2( x,y ) ;
llSay(0,(string)( angle/PI*180));
}
}
Ok, the PI in front of the function is extra, whatever you need on angle, which way etcetera, you have to decide for yourself….