1
\$\begingroup\$

I'm trying to rotate my OrthographicCamera in the render loop to a fixed angle, but I can only make the camera spin. This is the code that I have:

in create():

OrthographicCamera camera = new OrthographicCamera();
camera.setToOrtho(true, 100, 100);
camera.position.set(50, 50, 0);
camera.update();

in the render loop:

camera.rotate(30);
camera.update();

The camera keeps spinning each frame. But I'd like the camera to stay at 30 degrees.

I have already seen this answer:

https://stackoverflow.com/questions/16509244/set-camera-rotation-in-libgdx

But I don't like it because this code is too drastic. I wonder if there is a more "common" way to do this.

\$\endgroup\$
5
\$\begingroup\$

The method camera.rotate() (and all other transformation methods on camera) act on the current state of the camera. So if it's rotated 30 degrees, it will add more 30 degrees to the rotation.

If you want to keep at 30 degrees (but still applying the transform every step), you have to make it look back at whatever it was looking before. Assuming you're using an orthogonal camera for 2D rendering with y pointing down, then this should work:

camera.up.set(0, -1, 0);
camera.direction.set(0, 0, 1);
camera.rotate(30);

If you prefer y pointing up, try:

camera.up.set(0, 1, 0);
camera.direction.set(0, 0, -1);
camera.rotate(30);

Hope this helps you :)

\$\endgroup\$
  • \$\begingroup\$ Thanks for this. I was actually able to use this without the camera.directin.set(0, 0, -1); lines. \$\endgroup\$ – snapfractalpop Jan 16 '18 at 7:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.