Camera controls rotation angles are wrong after lerping camera position

After lerping the camera to some position, then the controls are broken:

Lerping breaks camera rotation control | Babylon.js Playground (babylonjs.com)

After the initial animation completes, drag left/right to try to rotate left right, but it no longer works.

How do we get the camera out of this state and back to normal?

Note, if targetRotation.z is 0, then the example works fine,

Working sample with targetRotation.z == 0 | Babylon.js Playground (babylonjs.com)

but in my more complicated case, targetRotation.z might not be exactly zero, and I can’t control it because it is being animated by something else. For sake of example, suppose I cannot change targetRotation.z to 0 myself.

So, I tried to fix it by doing this:

  • after animation completes, set the camera’s rotation.z back to 0
  • then call attachControls()

but still no luck:

Camera lerping attempted fix, no luck. | Babylon.js Playground (babylonjs.com)

I can’t attach a video here.

Aha! The trick is to set the lerped camera’s rotation.z to 0 on every frame. Example:

Camera lerping fixed! | Babylon.js Playground (babylonjs.com)

Why exactly is this though?

Indeed, it doesn’t seem right that the camera behavior at the end of the animation is not the same if you set rotation.z = 0 after each step as if you set rotation.z = 0 at the end of the animation. Or perhaps it’s because the target is recalculated internally at each step, and because rotation.z != 0, a little roll is added to the mix?

cc @PolygonalSun in case he has any clues.

So, I looked into this and here’s what I’ve found. The reason why you’re seeing the difference in the rotation angles is because the camera’s upVector is being changed. From what I’m seeing, you have a value of 0.000001 set for z in your rotation vector. In the code, when a change to the z-rotation is detected, the upVector will be updated. While this wouldn’t be a big change for one frame, I imagine that the changes are compounded a bit as the animation plays. This behavior could probably be avoided by either setting the value for z in your targetRotation to 0 (if you don’t need the value in there) or by just setting the camera’s upVector back to 0,1,0 when you’re done with the animation.