Rotation following the mouse

I’m using LibGDX (And this might actually be the problem, considering the top left of the screen is 0, 0 intead of the bottom right, however I have tried subtracting the MouseY from the Height of the application, this didn’t work.

What I’m trying to do: I have a sprite on the screen that I want to rotate towards my mouse at all times.

Here’s the thread I used to try to solve my problem http://www.java-gaming.org/topics/solved-rotation-following-mouse/24763/view

Here’s my code.

package com.chris.spaceshooter.entities;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;

public class Player extends Entity
{
	// The player Sprite
	private Sprite playerSprite;
	
	// The players movement speed
	private float playerSpeed = 2.0f;
	
	public Player(Texture texture)
	{
		this.playerSprite = new Sprite(texture);
		this.currSpeed = playerSpeed;
	}
	
	public void render(SpriteBatch batch)
	{
		playerSprite.draw(batch);
	}
	
	public void update()
	{
		if(keyPressed(Keys.W)) { moveUp(); }
		if(keyPressed(Keys.A)) { moveLeft(); }
		if(keyPressed(Keys.S)) { moveDown(); }
		if(keyPressed(Keys.D)) { moveRight(); } 
		
		float radiansToMouse = (float) Math.atan2(spriteCenter(playerSprite).y - Gdx.input.getY(), spriteCenter(playerSprite).x - Gdx.input.getX());
		float degreesToMouse = 57.2957795f*radiansToMouse;
		
		//Update the sprite position to the Entity position
		playerSprite.setX(getX());
		playerSprite.setY(getY());
		playerSprite.setRotation(degreesToMouse);
	}

	// Boolean to check if a key is pressed
	private boolean keyPressed(int keycode) { return Gdx.input.isKeyPressed(keycode); }
	
	private Vector2 spriteCenter(Sprite sprite) {
		float height = sprite.getHeight();
		float width = sprite.getWidth();
		return new Vector2(width / 2, height / 2);
	}
	
}

This just doesn’t rotate properly and I can’t figure it out! It’s making my balls snarl. Literally.

I have attatched the jar file with my single sprite in a zip file below, so if you can figure anything out, let me know. If any further information is required, please, ask and you shall recieve.

FileSize: 5.1Mb (I guess this is because of LibGDX)
Download: http://up.ht/Jc6XKx

Make one vector originating from origin upward at 0 degrees. Get vector from origin -> mouse. Calculate.

You know, $5 says you laid that out in simplest terms for me, but even after examining the link provided, I had no idea what I was looking at.

I’m going to go ahead and try to translate what you said.

First: Create a vector2 of the Current Position of the Player
Second: Create a vector2 containing the mouse position
Calculate the difference between the two.

^If this is correct, isn’t that what I’m doing?

[s]I dunno, google is your friend.

I’ve never done vector math. I would need to know more about how vectors are defined to be able to provide you an example. There’s most definitely an easier way to find out, I just don’t know it. I guess you could make a right triangle with your mouse coordinate and the center of the screen, and then do some trig. I haven’t taken math in quite awhile. Sorry, best I can do is give you the formula, I can’t teach something I don’t understand myself.[/s]

EDIT: Apparently there’s an easier way. Using this formula: Math.toDegrees( Math.atan2(fromLeft - originfromLeft, originFromTop - fromTop) ) + 360.0) % 360.0
You may need to change this a bit, I dunno. I will keep looking around, I might explore how it works later as well, I haven’t need angles yet. I’ll try setting up an example for myself to see if it works tomorrow if you can’t get it to work. I’d do it now, but I need sleep.

Updated with more information, added my files, and tried a few more things.

Still no luck :frowning:

I’ll try it then. I’ll post up if i figure it out. In the mean time, if anyone around here already knows how to do this… cough cough. No seriously, just post it, that’d be cool.

Yeah, I’d really enjoy having this code. Math isn’t really my strongest subject and I’m tired of screwing with it. Honestly. I’ve done plenty of google searches and the only thing I can think of is that LibGdx changing the 0,0 point on the screen is what’s causing the issue.

Alright here it is:
setRotation(360 - (float) ((Math.toDegrees( Math.atan2(mouse.x - origin.x, origin.y - mouse.y) ) + 360.0) % 360.0) + 90);

Origin is the middle of your camera. My camera is at 0,0 and it’s 800 x 800, so my origin is 400,400. Mouse and origin are both vector2 objects.

This works if the sprite is in the center of the screen, however if it’s in another location the rotation is still relevant to the middle of the screen, instead of the center of the sprite.
I set the origin.x/y to the center of the sprite, however that didn’t work either.

Well your sprite center is wrong, which might be causing it. You’re just giving the sprites dimensions, not it’s position. You missed a step in there. You need to return Vector2(playerSprite.x + width / 2, playerSprite.y + height / 2), otherwise you’re just returning a vector with coordinates of half your width and height of your sprite, likely 32x32.

both your code used for rotation and mine work for me, assuming the player is in the center of the screen. I’ll try it for other positions.

Alright, the issue is the mouse position is relative to the position inside of the window, not based on the sprite location. You have to make the mouse give you it’s position relative to the sprite location. I’ll try to figure out how to go about this.

float xOffset = (Gdx.graphics.getWidth() / 2) - (Player.getX() + (Player.getWidth() / 2));
		float yOffset =  (Gdx.graphics.getHeight() / 2)  - (Player.getY() + (Player.getHeight() / 2));
		float mouseX = Gdx.input.getX() + xOffset;
		float mouseY = (Gdx.graphics.getHeight() - Gdx.input.getY()) + yOffset;
		
		float radiansToMouse = (float) Math.atan2(mouseX -  (Gdx.graphics.getWidth() / 2) ,  (Gdx.graphics.getHeight() / 2)  - mouseY);
	      float degreesToMouse = 57.2957795f*radiansToMouse;
	      Player.setRotation(degreesToMouse - 90);

This works for wherever the player is.

Right now, I love you.

I honestly would have never figured this out.

As I said before I hate math, lol.

Thanks!!!

Meh, I just did it on paper and was like oh dur. You’re welcome, good luck with your program.