Boundaries for orbit camera

Hello!
I have a question about the orbit camera.
How to limit the movement of the camera within the object on which it is focused? Or if it doesn’t work out like that, then how to limit the camera movements within the boundaries that I can set myself? My main goal is that the camera cannot move away from the object so much that it ceases to be visible in its viewport.

Hi @Oleksii_Dubrovskyi,

One way to approach this is to create a bounding box in code. check the camera new position on each axis (x,y,z) per frame, if it’s included in the bounding (valid position), if not, revert to the current position along that axis.

Below is the code , I have minimised the amount of distanceSensitivity when its out of bounds

if(this.orbitCamera.distance <= minDistance ){
        if(event.wheel > 0){
            this.distanceSensitivity =  0.05;
        }else{
            this.distanceSensitivity =  0.2;
        }
    }else if( this.orbitCamera.distance >= MaxDistance){
        if(event.wheel < 0){
            this.distanceSensitivity =  0.05;
        }else{
            this.distanceSensitivity =  0.2;
        }
    }

and below code for orbit camera

    if(this._distance >= minDistance && this._distance <= maxDistance){
        position.scale(-this._distance);
    }else{
        if(this._distance < minDistance){
            this._distance = minDistance;
        }
        else if(this._distance > maxDistance){
            this._distance = maxDistance;
        }
        position.scale(-this._distance);
    }
1 Like

Hi, thanks for the reply!
But the camera position values ​​will be completely different if I, for example, zoom in or zoom out. Or if I rotate the camera on its own axis. Or what parameters need to be monitored to achieve the result?