How to make sure Object is not out of camera view?

Target:
I want to make sure that object does not leave camera view while moving the camera left-right-up-down. I can also rotate camera around the object and perform movement of camera.

How to create line trace like the blue lines or yellow lines so that when i move camera, if object hit trace, i could stop moving of camera so that object stays in camera view.

Any other idea as well? thanks!

Depending on how accurate you want this you could also calculate this using the camera’s field of view and the objects location.

An example of a method to check if the object is within the horizontal field of view of your camera:

Here I have an object “MyOject”. I take the vector between my Camera and MyObject, and project that onto the plane with the up vector of my camera as its normal.

I use this vector and my camera’s forward vector to calculate a signed rotation (negative or positive) between these two vectors on the plane that has my camera’s up vector as its normal.

I do that using this formula:

With the value I get out of that I check if that value is bigger than half my field of view.
If so, the object is out of range on the right side of my camera.

Else I check if the value is less than negative half my field of view.
If so, the object is out of range on the left side of my camera.

Else it’s inside my field of view.

You could then add the same functionality, with the right vector as the plane, and the vertical field of view to see if it’s out of view on the top or on the bottom.

This method will use the location of your object, so if you have a large object it probably won’t be accurate enough.
To make this method more accurate you could loop over the locations of your objects bounding box extents instead.

1 Like
  1. Find the vector from the camera origin to the object’s center.
  2. Determine the horizontal and vertical angles of that vector from the camera’s forward vector.
  3. Compare those angles to the horizontal and vertical FOV of the camera. If the angles to the object are greater than half the FOV, the center of the object is off the screen.

If you want to make sure the object is fully on screen, do the comparisons described above for the points on bounding box of the object instead of the center.

I did with compound bound and it works as how i wanted. Thanks! :slight_smile:

Thanks! Same as you mentioned.