# Keeping two objects in the camera view

This topic is 4484 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Anybody know a good technique of calculating the camera view in order to keep two objects in view even if they are moving apart from each other?

##### Share on other sites
Use Trigonometry, it works really great for stuff like this.

##### Share on other sites
One easy way would be to keep bounding spheres for each object, and a combined bounding sphere that always contains both objects. (The per-object spheres can make it easy to update the combined sphere.) Then, per frame just make sure the camera can see the entire combined bounding sphere.

Now, you could get a bit fancy, and use some other smarts to position the camera in a meaningful way. For example, if you want one of the two to always be centered, then in reality you'd have to use a larger combined sphere or some other math (trig, yeah), to figure out the requisite view frustum.

##### Share on other sites
Trigonometry... what strange beast is that? :)

I had thought about the bounding sphere idea but wondered if there were some other methods out there - I like to embrace all ideas rather than assume that my own are the best..

##### Share on other sites
Generally, your view frustum displays stuff with camera-local coordinates with znear < z < zfar. Anything else gets clipped or doesn't fit in the XY anyway, and is not displayed. You should be able to hardcode these values yourself directly in the projection matrix.

The width of the space volume that is projected on the edge z = znear, is given by the (1,1) member of your projection matrix, and its height is given by the (2,2) member. One is usually expressed through the other, via some viewport aspect constant.

By simple analogy, the respective width and height at the far edge of the frustum, z = zfar, is given by (zfar/znear)*m(1,1) and (zfar/znear)*m(2,2), or at any z, znear < z < zfar, from:
(z/znear)*m(1,1) and (z/znear)*m(2,2)

If you want to make sure that an object will be visible in the frustum, you can express its coordinates in camera local space (multiply as coordinate (w==1) by the view matrix, don't forget to normalize with respect to its new w) and see whether its new x,y, will be visible in the view frustum given its z.
Or calculate the camera FOV angle and use its new X,Y to determine whether they lie in that range. If not you can move the camera, or tweak the projection matrix...

Similarly, you could transformn the coordinate by the product (projection matrix * view matrix) and see whether -1 < x < 1, -1 < y < 1. It's the same thing...
[/edit]

It depends on the effect you want to get.

I cannot suggest anything else, since you weren't specific. E.g. did you want the camera to preserve a certain orientation to them both, lie on a specific plane? I think that the above -though- should be enough to start with...

##### Share on other sites
Nice!

Ideally I thought that the camera (or FOV) position would adjust to take into account the seperate object positions. I might also play around with orientation to get a nice smooth feel to the change.

##### Share on other sites
Sure. You can calculate their "center", as in (1/n)Σxi for n objects, where xi are their positions, and construct a new view matrix. Then you can find their projections on the znear plane, and make sure that you set a FOV angle that will "include" the farthest one (allowing a margin of some degrees)...

1. 1
2. 2
3. 3
4. 4
Rutin
16
5. 5

• 12
• 9
• 12
• 37
• 12
• ### Forum Statistics

• Total Topics
631419
• Total Posts
2999981
×