Sign in to follow this  
wild_pointer

Rotating an object to face cursor

Recommended Posts

Sorry to trouble you with something so simple. I'm making a little 2d topdown game and I want the player to rotate to face the cursor. I've explored a couple ways and gotten it to work well enough, the only problem is that the player sits at 0,0 and all my methods require special consideration when the cursor either passes the x or y axis. Is there a really easy way to do this without alot of special case work?

Share this post


Link to post
Share on other sites
Take a look at this thread

http://www.gamedev.net/community/forums/topic.asp?topic_id=263747

and see if maybe it gives you some ideas. If you've got more questions (or could be a little more specific) then ask away.

Share this post


Link to post
Share on other sites
Quote:
Original post by Bucket_Head in that other thread
So you can get the angle using a call to arcsine or arccosine (I believe it's asin and acos - those all work in radians, of course) but be a little careful of the domain and range on those suckers, you may need some ifs to determine what quadrants you're in. Once you've got the angle you could feed that to your graphics API and just rotate the whole quad.


That was the portion I was trying to avoid =) Thanks for your help.

[Edited by - wild_pointer on August 18, 2004 2:36:12 PM]

Share this post


Link to post
Share on other sites
This is what I use:

typedef struct
{
int x
int y;
}

float LookAt(vec2 a, vec2 b)
{
vec2 diff;
diff.x = a.x - b.x;
diff.y = a.y - b.y;
return (float)atan2(diff.y,diff.x);
}


The answer is returned in radians

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this