UniversalCamera flipping after its position.z goes over specific value

Hello guys,

Currently working on a common project along with @splash27, we are struggling with a strange bug where a UniversalCamera will flip, when position.z reaches a specific value.

Here is a repro :

In this repro the camera.position.y is 20, and the flip will occur at 64.0 (see the console)
But it depends on the position.y : if position.y == 10, the flip will happen at 32.0, etc, etc… :thinking:

To narrow it down a bit - it looks like a regression introduced in 8.10.1 (which contains some camera-related breaking changes)

PG with 8.10.0 doesnt manifest the problem:

2 Likes

Ah, yep, 8.10.1 is when we introduced RH camera fixes cc @bghgary

I’m investigating…

Just dropping a couple of observations:

  • This scene is left-handed
  • FreeCamera also repros
1 Like

There are some math issues caused by the fact that the camera is vertical. I haven’t found anything that indicates a regression yet, but the math in the code for the look at matrix is not stable in this situation. I will discuss this with @deltakosh and make a fix, but in the meantime, try the following.

When a camera is looking straight up or down, set the up vector to indicate the intended orientation as the default up vector is colinear with the eye direction of the camera. I would suggest setting the upVector to 0,0,-1 to match what you see in the first PG: https://playground.babylonjs.com/#JI8DW5#3.

2 Likes

After discussing with @deltakosh, we think there is nothing to do. We can add an epsilon to the check for colinear eye direction and up vector, but there is no epsilon value that makes sense since we don’t know the scale of the scene. We can maybe make this configurable, but that seems overkill.

The right solution is to update the up vector of the camera if the camera is ever going to get close to vertical.

So do I understand correctly that this needs to be done only once (camera.upVector.set(0, 0, -1)) ? For example, in an FPS style game, set this once for the first person (moving) cam and that should prevent this issue?

If the camera eye direction never becomes colinear with the default up vector (0, 1, 0), there is nothing to do. If the camera is setting its target in a way that moves across the default up vector, the up vector should be updated in addition to setting the target. If the camera is always looking straight down like it is in the OP, then the up vector only needs to be set once.

I would guess in an FPS, the camera would be limited from looking straight up or straight down with a small buffer. In this case, there is nothing to do. If it’s really necessary to look straight up or down, then the up vector can be updated each time the user changes the camera’s view.

Ok, thanks for clarifying!

1 Like