Sign in to follow this  
ajm113

Mouse Pos & Rotation Where the Mouse Is?

Recommended Posts

Ok, I am creating this 2D shooter that has a guy that walks around and rotates where you have the mouse at in a top view. What I am wondering is what is the calculation on rotating a character/model too where the mouse is on the screen. So the model is pointing to the position of the cursor's pos. Thanks, I know this must be a rookie question, but I am not too skilled at math at the moment and want too touch all the basics before I progress in full 3D math.

Share this post


Link to post
Share on other sites
Well, I'm not too skilled at math either, but if you are representing the player's facing direction with a normalized (magnitude of one) vector, there's an easy solution. Just take the vector that goes from the player's screen position to the mouse, normalize it, and set the player's facing vector to that.

Hope this helps!

-- ProgrammerZ

Share this post


Link to post
Share on other sites
Uh 'normalize it'? See thats where I get lost...

I am only dealing with a float that is handling the rotation of the model, so I don't know why I need to create a vector.

Share this post


Link to post
Share on other sites
I think that a vector should probably provide a good (and fairly simple, I think) means of getting your desired angle.

The basic idea, if I'm not much mistaken, would be to find the vector between the mouse position and the player character, and then use atan2 (which should essentially be atan, with values returned between -pi and pi, instead of atan's usual -p1/2 to pi/2 (if I have that correct)).

If you want any elaboration, just ask. ^_^

[edit] Just for clarity, I don't think that normalisation is called for in this particular method (which is not, I imagine, what the previous respondent was suggesting).

[Edited by - Thaumaturge on November 13, 2008 1:28:30 AM]

Share this post


Link to post
Share on other sites
Yeah I been trying to play with that, but I keep getting the same results of the player rotating in a 1/4 quarter rotation, the player is in the middle of the screen always, so I am wondering if their has too be some math with the projection matrix. I'm using OpenGL, so is there a difference between math done out in 3d to 2d scale with OpenGL?

Code:


result = (180 * atan2((float)mousePosition.y, (float)mousePosition.x) / Math::PI);




[Edited by - ajm113 on November 15, 2008 7:33:21 PM]

Share this post


Link to post
Share on other sites
First of all, my apologies for the delay in response. ^^;

As to your problem, it occurs to me to ask: have you checked the values that you get as the mouse position? Specifically, where is the origin (i.e. 0,0)? If it's in the middle of the screen, then atan2 should work, I believe. However, if, as it occurs to me may very well be the case, you find that the origin is in one corner, you might find that transforming the mouse coordinates so that the origin becomes the centre of the screen will help.

In order to do this, simply subtract (or add, if you have negative values on any axis) half of the screen's size in that dimension from the relevant component of the mouse position - that is, mouse.x - screen_width/2 and mouse.y - screen_height/2.

You shouldn't, I don't think, have any playing to do with projection matrices or the like - you're simply taking a set of screen coordinates (I would imagine), and deriving an angle relative to a reference point, also in screen-space, from it.

Good luck. ^_^

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