angle_to_mouse(X,Y); function

Talk about making games.

angle_to_mouse(X,Y); function

Postby pyrometal » 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.

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;
}
SPRITE WARRIOR:
Free, open-source & crossplatform pixel art editor (currently under construction).
User avatar
pyrometal
 
Posts: 706
Joined: Wed Nov 28, 2007 4:07 am
Location: Kingston, ON, Canada
Score: 86 Give a positive score

Return to Game Development

Who is online

Users browsing this forum: No registered users and 1 guest