Nishant Anindya
Nishant Anindya

Reputation: 539

Rotation of Orthographic Camera

I am able to rotate camera with this code

camera.zoom = 3//in constructor
if(camera.zoom>1)
    {
    camera.zoom-=0.01f;
    camera.rotate(15);
    }

this is done in render, Now zooming effect works properly but when zooming completes my screen is stay rotated with current angle. like below. enter image description here

I want that my screen stops after zooming at 0 degree.

Upvotes: 4

Views: 1954

Answers (4)

Neha Agarwal
Neha Agarwal

Reputation: 622

In your code snippet

**camera.zoom=3;**

and in each iteration you are zooming camera by 0.01 till camera.zoom > 1 so you have total 20 iteration for zooming

Then rotate with 18 degree angle after iteration it will rotate in 360 degree.

Upvotes: 1

Shinni
Shinni

Reputation: 241

attention, the computer can't correctly represent most real numbers!

in binary 0.01 is a periodic number, so it will be truncated/rounded.

substrating/adding float numbers a few hundred times will add the rounding error and thus give you horribly wrong results.

(e.g. after 200 substractions, your camera.zoom value will be ~ 1.0000019 - NOT 1.0!)

that's why your loop is repeated 201 times, giving you a zoom value of 0.9900019 and a rotation of 361.7996 ~ 361.8 (when using 1.8 as in alex's answer).

you could use libGDX Interpolation functions:

time += Gdx.graphics.getDeltaTime(); //the rounding error is futile here,
//because it'll increase the animation time by max. 1 frame
camera.zoom = Interpolation.linear.apply(3, 1, Math.min(time, 1));
camera.rotate = Interpolation.linear.apply(0, 360, Math.min(time, 1));

this code would create an one second long animation of zooming from 3 to 1 and rotating from 0 to 360 (simply one whole roation)

Upvotes: 0

Aliaaa
Aliaaa

Reputation: 1668

I wrote this method to calculate current angle of camera:

public float getCameraCurrentXYAngle(Camera cam)
{
    return (float)Math.atan2(cam.up.x, cam.up.y)*MathUtils.radiansToDegrees;
}

Then I call rotate method like this:

camera.rotate(rotationAngle - getCameraCurrentXYAngle(camera));

This code works, but it will rotate immediately in one call. to rotate it by a speed, you need to calculate appropriate 'rotationAngle' for every frame.

Upvotes: 1

alex
alex

Reputation: 6409

Have you tried rotating a multiple of 1.8 degrees with each iteration? Then you're image should have completed a number of full rotations once the 200 iterations have passed.

Upvotes: 0

Related Questions