From: Anthalia Nemeth
So I have a box that rezzes an arrow and a target that moves around. The arrow needs to rotate to face the target and then move toward the target.
I have the move working but not the rotation part. I've tried using llEuler2Rot using the target's vector position as the parameter and it still doesn't work. The arrow rotates but doesn't point toward the target. Any help would be appreciated. Thanks in advance.
The targets vector rotation has nothing to do with the direction the arrow should face. All you want is the vector position of the arrow and the vector position of the target.
This requires some trig to calculate it. Basically you need to calculate the angle between the arrow and the target. I recently worked this out for something else.
//this routine returns the angle of between the object and the target
float getangle(vector target)
{
vector pos = llGetPos();
vector diff = pos - target;
//get the arctangent of the x,y here.
float angle = llAtan2(diff.x, diff.y);
angle = angle*RAD_TO_DEG;
return angle;
}
If you want to keep it in radians, just comment out the second to last line.
You may have to add a few bits to offset the result, otherwise the left-right result will be switched.
angle = 180-angle + 180;
Edit: just realized you might not know where to go with this.
In the rez:
when you calculate the rotation vector to apply to the arrow, you'll want to use llCos and llSin with the angle value.
vector rot = <0,0,0>;
rot.x = llCos(angle*DEG_TO_RAD);
rot.y = llSin(angle*DEG_TO_RAD);
The getangle function I wrote here only works on a 2d basis. If you need it to work on a 3d basis and have the arrow angle up or down, you'll need to modify the routine to return a vector and do two calls to llAtan2.
I haven't tested this routine though:
//this routine returns a vector full of radian angles for x, y, and z.
vector get3dangle(vector target)
{
vector angle;
vector pos = llGetPos();
vector diff = pos - target;
//get the arctangent of the x,y here.
angle.x = llAtan2(diff.x, diff.y);
angle.x = PI-angle.x+PI; //PI = 180 in radians. Doing a mirror flip here.
angle.y = angle.x; //uses the same angle as x
//calculate the angle for z
angle.z = llAtan2(diff.x, diff.z);
//this may or may not be necessary. never tested it.
angle.z = PI-angle.z+PI; //PI = 180 in radians. Doing a mirror flip here.
return angle;
}
and then in the rez section when you set the rot:
vector rot = <0,0,0>;
rot.x = llCos(angle.x);
rot.y = llSin(angle.y);
rot.y = llSin(angle.z);