CodeCamper
CodeCamper

Reputation: 6980

libGdx how to return the camera rotation

camera.rotate(1.5f);

I can rotate the camera with degrees like so, but how can I return the rotation of the current camera so for example I know how many degrees it takes so I can restore it back to the normal rotation?

Upvotes: 1

Views: 969

Answers (1)

Bernd
Bernd

Reputation: 789

I have dealt with the same problem and since the libgdx camera doesn't natively have a function for returning the rotation, solved it by creating a function returning the cameras rotation in degrees.

public float getCameraRotation()
{                       
     float camAngle = -(float)Math.atan2(camera.up.x, camera.up.y)*MathUtils.radiansToDegrees + 180;         
     return camAngle;
}

It works by getting the camera angle in radiant:

-(float)Math.atan2(camera.up.x, camera.up.y)

then converting it to degrees:

*MathUtils.radiansToDegrees 

and finally since the range of the angle is -180 to 180 converting it to 0-360

+ 180

Upvotes: 6

Related Questions