Preventing the camera from turning upside down

This post is old, but maybe I can contribute, as I think makno’s solution doesn’t work and someone can be interested in a working solution.

Actually (I copy-pasted it in my own code and ran my sample app), makno’s solution prevents to see upper than the horizon and lower than down instead of preventing to see “upper” than the up ({0,1,0}) and “lower” than down (this second constraint works in makno’s solution).

So, a simpler and working solution is to use the camera’s up vector, instead of its direction vector.
If you were the camera, you could imagine this vector as pointing out of the top of your head, and moving at the same time when you look around.
So the difference between this vector and the Y axis :

  • equals zero when you look straight to the horizon
  • equals PI/2 when you look your feet or when you look up to the zenith.

So the code to write at the end of the “rotateCamera(float value, Vector3f axis)” method is very simple:

[java]
// Prevents the camera to be able to see “upper” than the up vector and “lower” than the down vector
// (in order to prevent to see the world upside-down).

    if (up.angleBetween(Vector3f.UNIT_Y) <= FastMath.HALF_PI) {
        cam.setAxes(q);
    }

[/java]

I use a standard FlyByCamera with no axis constraint, I just overriden the jMonkey class (and several others, didn’t have the choice…) to be able to override the “rotateCamera(float value, Vector3f axis)” method.

This:
up.angleBetween(Vector3f.UNIT_Y) <= FastMath.HALF_PI
Should be the same as:
up.y >= 0

…but without the expensive trig.

One point for you, pspeed :slight_smile:

I’ve created issue #626 to track this suggestion.