Problems with camera.unproject

I want to make my player sprite to rotate towards mouse position, but the world y-axis starts from bottom and the mouse postiitons y-axis starts from the top. I could flip the y-axis from the camera with camera.setToOrtho(false); I’ve heard that you can also use camera.unproject when calculating the rotation.

Here is my player rotation method

public void rotateToMousePosition(Vector2 playerPos,Vector2 targetPos, OrthographicCamera camera){
		camera.unproject(new Vector3(targetPos, 0));
		float deltaX = playerPos.x - targetPos.x;
		float deltaY = playerPos.y - targetPos.y;
		float degrees = MathUtils.atan2(deltaX, deltaY) * MathUtils.radiansToDegrees; //calculating rotation
		
		this.setRotation(degrees);
	}

However this does not solve the y-axis problem. mouse.getY() y-axis still starts from the top. I’m doing somethong wrong?

You have to use the vector returned from unproject, the method itself does not do anything to the camera.


public void rotateToMousePosition(Vector2 playerPos,Vector2 targetPos, OrthographicCamera camera){
      Vector3 tmp = camera.unproject(new Vector3(targetPos, 0)); // transform from screenspace to worldspace
      Vector2 transformedTargetPos = new Vector2(tmp.x, tmp.y);
      float deltaX = playerPos.x - transformedTargetPos.x;
      float deltaY = playerPos.y - transformedTargetPos.y;
      float degrees = MathUtils.atan2(deltaX, deltaY) * MathUtils.radiansToDegrees; //calculating rotation
      
      this.setRotation(degrees);
}

EDIT: also atan2 accepts parameters as (y, x), not (x, y).

Got it worked finally. Thanks for the fast reply