angle_to_mouse(X,Y); function
Posted: Fri Aug 22, 2008 3:29 am
This is I function I conceived for a small experimentation project I was making. I thought it could be useful for some people so I decided to post it...
It determines the angle(in radians) from the actor's coordinates (which called it) to the mouse's coordinates. You can add an offset from the actors coordinates if you do not want the angle given to be relative its center. If you do want the angle from the actor's center to the mouse, just leave it as angle_to_mouse(0,0);.
Here's the code, put it in a Global Script, its very simple.
It determines the angle(in radians) from the actor's coordinates (which called it) to the mouse's coordinates. You can add an offset from the actors coordinates if you do not want the angle given to be relative its center. If you do want the angle from the actor's center to the mouse, just leave it as angle_to_mouse(0,0);.
Here's the code, put it in a Global Script, its very simple.
- Code: Select all
double angle_to_mouse(double X_offset, double Y_offset)
{
double distX = xmouse + view.x - x - X_offset;
double distY = ymouse + view.y - y - Y_offset;
double ret;
if(distX>0)
{
ret = atan(distY/distX);
}
else if(distX<0)
{
ret = PI + atan(distY/distX);
}
else
{
if(distY>=0)
{
ret = PI/2;
}
else
{
ret = 3*PI/2;
}
}
if(ret != abs(ret))
{
ret += 2*PI;
}
return ret;
}