Sign in to follow this  
XiotexStudios

Keeping two objects in the camera view

Recommended Posts

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 this post


Link to post
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 this post


Link to post
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...

[edit]
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 this post


Link to post
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)...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this