Moving something on an angle.

I think the real problem is that you’re not keeping a consistent definition of an angle. The usual definition is that the +x axis is 0 degrees and the +y axis is 90 degrees. Math.atan2(y,x) will give you an angle according to this definition, so you need to make sure all of your code is consistent with this.

You have the y input in atan2 negated, it should be:


float radiansToMouse = (float) Math.atan2(mouseY - (Gdx.graphics.getHeight() / 2), mouseX -  (Gdx.graphics.getWidth() / 2));

I’m guessing the player sprite image is orientated upwards and you’re using the axes for 2D where the top left corner is (0,0)? in this case if the sprite is drawn without rotation it will face in the -90 direction so there needs to be an offset of +90 added to the rotation when it is actually drawn.

I’m not sure exactly what setRotation and playerSprite.setRotation do, or why you are calling one inside the other, but try (with the above modification as well):


playerSprite.setRotation(setRotation(degreesToMouse) + 90);

(as long as that setRotation only changes the actual sprite’s rotation and not the rotation used for the bullet calculations, otherwise remove the +90 from here and move it to where the sprite is drawn)

My Player Extends the Entity Class, setRotation is an float that sets the rotation value of the Entity, and returns the value set, allowing me to set both in a single line.

protected float setRotation(float rot) { this.rotation = rot; return this.rotation; }

The code you provided is also not rotating correctly, however the original one that had the x,y switched was working.

Finally got this working, but the offset was 270 instead of 90.

playerSprite.setRotation(setRotation(degreesToMouse + 270));