Rotate player in relation to mouse position

Hey guys, today my question is… How can I rotate the player in relation to mouse position?

I tried to make it, but the player was rotated always without stop :S

Your question is not very descriptive. Are you talking about a first person shooter? If so your problem could be that you don’t set the mouse position back to the centre of the screen after it moves.
If this is not your problem, or even your question, consider being a little more helpful to people trying to help you. Maybe you could even post some code.

Is this in 3D or 2D? I guess it is 2D. Then I think you have a top down view and the player can rotate by the imaginary z axis coming out of the screen.

In this case it must be fairly simple. Just compute the distance between the player position and the mouse position and store it for example as a Vector2. You can then normalize it and calculate the dot product on the norm vector representing rotation of 0 degrees. You then just have to see if you are above or under the y position of the player to get the angle right.

If my assumptions are right, tell me if you need more explanation. Or maybe there is an even easier way.

Oh sorry for don’t write a bigger description of my problem.

Okay I’ve a little 2D game. Where the player can shot. So, I need rotate the “bow” in function of the mouse position. Really my problem isn’t rotate the player, because I made it more or less. My problem is I can’t stop the rotation. My problem is, when I move the mouse, the player rotate yes, but it stops never. So I want that when the player has rotated then stop. I don’t know how to make that.

Here’s my code

public void rotate(double x, double y, double cx, double cy, double n) {
		double dx = x - cx;
		double dy = y - cy;
		double r = Math.sqrt(dx*dx + dy*dy);
		double a = Math.atan2(dy, dx);
		a -= n / 180 * Math.PI;
		this.x = cx + r * Math.cos(a);
		this.y = cy + r * Math.sin(a);
	}
	
	public void render(Graphics g) {
		//image.draw((float)x, (float)y);
		g.setColor(Color.red);
		g.drawLine((float)x, (float)y, (float)x2, (float)y2);

	}
	
	public void update(GameContainer container) {
		Input input = container.getInput();
		rotation = Math.atan2( input.getMouseY() - 600/2, input.getMouseX() - 800/2 );
		
		rotate(x, y, x2, y2, rotation);
	}

I will try to comment your code to see where the problems may be.

public void rotate(double x, double y, double cx, double cy, double n) {
		double dx = x - cx; // calculate current angle of drawn line (the bow?)
		double dy = y - cy;
		double r = Math.sqrt(dx*dx + dy*dy);
		double a = Math.atan2(dy, dx); // angle is known now in rad

		a -= n / 180 * Math.PI; // rotation in rad is converted to rad... uhm, yeah. And then subtracted from the computed angle of the line... am I getting this wrong?
		this.x = cx + r * Math.cos(a);
		this.y = cy + r * Math.sin(a);
	}
	
	public void render(Graphics g) {
		//image.draw((float)x, (float)y);
		g.setColor(Color.red);
		g.drawLine((float)x, (float)y, (float)x2, (float)y2);

	}
	
	public void update(GameContainer container) {
		Input input = container.getInput();
		rotation = Math.atan2( input.getMouseY() - 600/2, input.getMouseX() - 800/2 ); // mouse coordinates relative to center of the screen used for getting the angle between normal position without rotation and rotation to current mouse position
		
		rotate(x, y, x2, y2, rotation); // method for rotation. Uses line start and end point and rotation angle as inputs...
	}

I do something similar in my game but only have 8 directions that the player can be facing, would that be of any use to you?

Watch on of the videos here. Sorry if this is not what you want.

http://www.java-gaming.org/topics/iconified/29239/view.html

Angles hate you. Hate them back: http://www.java-gaming.org/topics/calculating-theta-of-what/29174/msg/267308/view.html#msg267308

Guys, I don’t know if you didn’t understand me or I don’t understand you. My problem isn’t to rotate the line, because I can rotate it perfectly, my problem is stop the rotation once it has the correct angle.

Because of not having any proper information about what “doesn’t stop rotating” really means, we can’t really tell what you want to hear apart from where the problems in the code may lie.

There is no screenshot, no captured video, no sketch, no precise information about it. Anything of this could help. All that I see in your code for now is, that you seem to be messing with angles in degrees and radians. But maybe I got this wrong. In the end, you should know best and tell us, if this could actually be the problem or not. So, when you look at the comments I made on your code, is this correct?

It rotates endlessly even though the angle didn’t change?
Did you try calculating a delta value between the last angle and the newest before changing the rotation of the player?

Okay I’ve recorded a little video
http://screencast-o-matic.com/watch/cIfoDeV3gM

So I hope this is helpful

P.S. Sorry for the video poor quality, That’s a bad webpage but I didn’t want to upload the problem to youtube. And on my PC, the rotation is faster…

Thank you for the video, this helps. It doesn’t really look like the problem is just that it won’t stop rotating.

So there still is this line, that seems wrong:

      a -= n / 180 * Math.PI;

What are you doing there and why? Is the value n supposed to be in degrees? Is it actually in degrees?

I don’t know what is its function, I just was testing :). And Now I’ve a problem with that line, I deleted it and now The line doesn’t rotate…

If you have a process that starts but does not stop, I can think of two general approaches. Either (1) the method stops itself, or (2) something external to the method tells it to stop.

(1) Create a variable that iterates with each increment of the rotation. Test against a maximum value or another condition that lets you know it is time to stop. For example, perhaps you “know” it takes 20 rotating steps to reach the final position–just have a counter test for 20. Or you know the final angle should be N radians. Again, test for N radians with every iteration.

(2) Create a “state” boolean, such as ‘isRotating’. Consult this boolean each increment before deciding whether to rotate or not. An external method communicates with the rotating method by setting isRotating to true or false.

The second idea makes sense if you are using something like mousePressed() to start the rotation. If so, perhaps using mouseReleased() is the desired way to stop it? This is possible if mouseReleased() changes the isRotating boolean to false and the rotating method is consulting this boolean every iteration.

BTW, if you do this second method, it helps to make isRotating volatile.