Camera.setRotation() slows down the game

Hello everyone

Whenever I use the camera.setRotation() method in the update loop, the game slows down drastically(0.5-1 fps)

Am I doing something wrong ?

Thanks for your help

What was your FPS before? 1 FPS doesn’t sound very drastic but I don’t know what it’s 1 FPS less than. :slight_smile: What does your scene have in it?



For a normal scene, changing the camera view also causes new data to be sent to the GPU… or at least re-evaluated.

In fact, i meaned that the game goes all the way down from 120 fps to 1.

As soon as i uncomment the line…

Yeah, that’s not right. What are you passing for rotation?

A quaternion, its value is changed every loop, but removing the line does not change anything

Can you try creating a simple test case based on the default blue cube BasicGame template?



If adding camera rotation to the simpleUpdate() method in that simple test case causes a similar slow down then there is something to look at. And if it doesn’t, then that means something else weird is going on that is more specific to your setup.

I did some more test, apparently, it does not come from setRotation but from getRotation

In fact, im searching for a way to shift the camera a bit from the default view, I want to make the character’s camera nod according to the breath

Is there a way to do that ?

getRotation() should be instant. All it does is return a value.



Can you show us what you are doing exactly?

well, basically, i’m doing this

[java]quat = quat.fromAngles((float) Math.sin(timer.getTime()/2000)/200, 0f, 0f);

quat.add(cam.getRotation());

cam.setRotation(quat);[/java]

every update

Other than the fact that it’s odd to pass the result of sin to fromAngles… I see nothing wrong with this. The code should run in nanoseconds.

well, the sin is just here to calculate the nod angle.

But, isn’t there a way to apply a shift angle to the camera ?

Yes, you get it’s rotation and change it… like you are doing.



And that definitely won’t take 120 FPS down to 1 FPS. Something else is wrong.

The sin looked weird to me just because it doesn’t return angles… it returns… the sin. So it will be between -1 and 1 for the different radians that are passed in. And then to divide -1 to 1 by 200 just looked strange because this will hardly move the camera at all. It it’s largest change it only shifts the camera around the X axis by 0.28 degrees. But maybe that is the effect you are going for.



It’s not wrong… it just looked strange.



Also, to combine two rotations you multiply them. Adding Quaternions will produce strange results as they will no longer represent a rotation.

well, in fact, the code worked, but at 1 fps ><

and if don’t divide it by 200 or 2000, the camera teleports from full up to full down over and over again

Oh, and I can’t move the view with the mouse when i’m using this

Yes, the equation looks like you just tried random stuff until it worked.



I think what you really want is to pick and angle of deflection and then multiply those radians by the sin() of time.



But you definitely shouldn’t be adding your quaternions together. Multiply them together or you will get really screwy results. This may also be messing up mouse look since your orientation quaternion will be all kinds of bad when it tries to add the mouse rotation.



If you want to prove that this is fast code, simply get System.nanoTime() before and after and find out how long that section is taking. If it takes more than a millisecond then I will be shocked.