# How do you get a rotation around the center of the screen from (mouse) X-Y coordinates?

My player is going to have a rotation control based on the mouse position. The player is always in the center of the screen. What I need to do is get the rotation around the center of the screen(the player) . From there I will pipe that to a function which sets the player’s heading.

I checked “Hello Picking”, but /i still come across the problem of getting the rotation from the coordinates.

The direction is the heading… you never really need to bring angles into it.

Make a vector from the player to the screen coordinate and normalize it. That’s the direction vector.

From there you can use quaternion lookAt or whatever.

``````    Vector2f mp = inputManager.getCursorPosition();
Quaternion q = new Quaternion();
q.lookAt(new Vector3f(mp.x, 0, mp.y), Vector3f.UNIT_Y);
Vector3f newRot = new Vector3f();
q.toAngleAxis(newRot);
System.out.println(newRot.x);
``````

(I realize this is horrible)
Did you mean something like this?

No. Why do you do 900 extra steps after you already got the answer in the third line?

Edit: and to be clear:

is total nonsense. a) it won’t get you what you want, b) you are ignoring half the result.

You don’t need the angle. Something else you are doing is totally messed up if you need the angle.

Because of my rotation function, which isn’t that great either.
it’s effectively equal to `gl.player.newRot = (int) (newRot.x * FastMath.RAD_TO_DEG)`

Here is my rotation function, which is why I nee the angle.

``````public void update(GameLogic logic) {
this.node.setLocalTranslation(position);
this.node.setLocalRotation(rotation);
if (newRot != rot) {
int jump = 5;
float pos_rot = (360 + newRot - rot) % 360;
float neg_rot = 180;
if (pos_rot < neg_rot) rot += jump;
else rot -= jump;
}
this.moveFwd();
this.node.setLocalTranslation(position);
this.node.setLocalRotation(rotation);
}``````

So many people make this mistake… direction to quaternion → angles → quaternion… angles are never needed. Total waste of time, huge source of bugs.

You already have the quaternion… if you want to slerp to it then slerp to it. No reason to bring angles into things.

I wasn’t using the slerp functon anymore because the desired angle will be updated every frame.

If you want to do things the hard way and use angles then atan2() is what you want.

``````Math.atan2(y, x);
``````

How do I use this? Literally 0 experience with this. Sorry if it’s a dumb question.

Edit: by the way, you know in your first post that the cursor position is based on the lower left corner and not the center of the screen?

Forgot about the mouse positon thing.
And yes, I did read it. as I said I have 0 experience with that…

I mean… you have rectangular coordinates and you want an angle. How much clearer does the javadoc need to be:
“Returns the angle theta from the conversion of rectangular coordinates (x, y) to polar coordinates (r, theta).”

Pass in your rectangular coordinates… get an angle. I can’t be any clearer without writing the code for you… but my contracting rates are quite steep.

So:

``````    Vector2f mp = inputManager.getCursorPosition();
mp.add(new Vector2f(settings.getWidth() / 2, settings.getHeight() / 2));
gl.player.rot = (int) (Math.atan2(mp.y, mp.x) * FastMath.RAD_TO_DEG);
``````

-because (though I havn’t double checked by testing it) I’m pretty sure

this line isn’t doing anything.

Also have a look here to get the rotation you desire: http://wikicode.wikidot.com/get-angle-of-line-between-two-points

1 Like

I think you need to draw on a piece of paper. Draw a screen shaped box. Put a big dot in the middle. Write down what the rough x,y of that is.

Put some other random dots on the rectangle and work out roughly what their CENTER RELATIVE COORDINATES should look like. Make sure to do this in at least all four quadrants. Note, for example that the lower left quadrant would be negative numbers and not giant positive ones.

(Hint: your math is exactly the opposite of what it should be.)