Change camera position backward/forward to view all my scene

Hello :smile: ,

Suppose:
I have the bounding box of my scene:

BoundingBox boundingBox = (BoundingBox) mParentRootNode.getWorldBound();

My camera, mParentCamera, is not centered on this bounding box.

Now, I would like to move my camera backward (or forward) to view all my scene on the screen (or the bounding box). Could you tell me how to do that, please?

Note: I already implemented the function to move backward and forward (it’s working :stuck_out_tongue_winking_eye: ).

I do something similar here:
https://code.google.com/p/simsilica-tools/source/browse/trunk/SimArboreal-Editor/src/main/java/com/simsilica/arboreal/AtlasGeneratorState.java#441

It moves the camera to try to get the whole tree in view. The view matrix m11 tells sort of the ratio needed as explained in the comments. It’s hard to explain quickly better than the code does.

Thank you but your solution was not working for me :persevere:

After almost two days of debugging to understand what’s was wrong, it’s working :scream_cat:

Here is how I proceed:

  • get the bounding box of my world

  • generate the 8 points of this bounding box
    For each point:

  • compute the distances of the point between the camera plane, the x-plane (dir and up camera vectors) and the y-plane (dir and left camera vectors)

  • I also compute the distance between the point and the center of the bounding box.

  • After that I compute the distance which is needed to view x and y distance and I choose the maximum taking in to account the distance between the point and the center of the bounding box.

    candidate_distx=camdistx/(tan(FOVx/2)
    candidate_disty=camdisty/(tan(FOVy/2)

The position of the camera is equals to the center of the bounding box minus the normalized dir vector of the camera multiply by my computed distance… :smiley:

Also if the field of view about y (FOVy) is know, the FOVx is not equal to:

Aspect * FOVy. 

It’s equal to:

Aspect = screenWidth/screenHeight
FOVy_deg = 45f
FOVy_rad = (FOVy/2.0) * (PI/180)

FOVx_rad = atan[ Aspect * tan[FOVy_rad] ]

Long life to JMonkey :blush: