How to fit a box in the camera's view frustum

Started by
20 comments, last by Gamer Pro 11 years, 1 month ago
Ok. You have to do the trig calculation a little differently to allow for perspective.

Find the normals of the top and bottom culling planes. Find the size of the box along those normal directions, using the formula I gave previously. If the sizes are s1 and s2, then the distance is

D = (s1 + s2) / (2 sin(half FOV))

Do the same for left and right. Also, you don't want to be looking at the box centre or part of the box will be off screen. You'll need to pan the camera to make it fit. The distance to pan is

P = (s1 - s2) / (2 sin(half FOV))

I'll leave the derivation as an exercise for the reader ;)
Advertisement

I want to make sure I'm on the same page. Math and I aren't exactly on the best of terms.

Find the normals of the top and bottom culling planes.

I'm reading this as find the normals of the far and near planes of the camera. In which case there's only one and it's the inverse of the direction the camera is looking at.

Find the size of the box along those normal directions, using the formula I gave previously.

This is the box's depth, so: Box size X * abs(dot(Box X, Camera Z)) + Box size Y * abs(dot(Box Y, Camera Z)) + Box size Z * abs(dot(Box Z, Camera Z))

If the sizes are s1 and s2...

I don't understand how their can be 2 sizes. Do you mean the 'starting' and 'ending' point of the box & therefore using those points to determine the box size? Wouldn't the above forumla do that for me so D = halfDepth * sin(half FOV) (I'm assuming I'm multiplying, you missed the operator in your post.)

Do the same for left and right.

This is so I can determine if I should be sizing vertical or horizontally, right?

You'll need to pan the camera to make it fit.

I'm currently doing this by determining the center of box when it's rendered on screen and finding that point in the world. I like your way better since the code is much smaller (the most efficent code is the one you don't write, afterall). But I need clarification on the sizes thing.

There are six culling planes: top, bottom, left, right, front and back. Top and bottom have different normals and so do left and right.

I missed some brackets, see above.

Yes, horizontal and vertical will give different distances.

Due to perspective the centre of the box won't be centred on screen if the box touches the edges on both sides. That's the reason for the offset.

Okay, I've calculated the distance. It works for the 90° cases and for the middle cases it looks right (I won't know until I complete the panning). But I do have a couple of questions regarding the panning.

For the panning distance, I'm going: size calculated from the top normal - size calculated from the bottom normal & size calculated from the left normal - size calculated from the right normal. Is that correct or should they be the other way around?

Finally, I have no idea how to calculate the vector I need to pan along so that box fits and is centered.

And thank you for all the help; I've been trying to figure this out for a few months now and all the progress I've made in the last month has been directly because of you.

No problem, I just hope I'm getting it right.

The formula gives you horizontal and vertical panning distances (camera x and camera y). You have it the right way round and combining the two components gives the vector to move along.

For the one that fits exactly you should pan the full distance in the respective direction. For the other direction I'd guess you should scale it down by the ratio of the distances, but I haven't worked that through.

Also, note that the panning distance is signed and this tells you which way to go along each axis.

Oooh. I thought the distance was a radius thing. 'You have the pan the camera this much in some direction' kind of thing.

However, it looks like distance calculate is coming up short.

Before panning:

[attachment=13886:before pan.png]

After panning:

[attachment=13887:after pan.png]

Now ignoring that the panning is wrong, the box should fit the screen, or at worse, spill past either the top or bottom of the screen due to bad/no panning. The distance calculation seems fine.


bottom size = half map x * absolute(x-axis • bottom normal) +
                      half map y * absolute(y-axis • bottom normal) +
                      half map z * absolute(z-axis • bottom normal)
left size = half map x * absolute(x-axis • left normal) +
                half map y * absolute(y-axis • left normal) +
                half map z * absolute(z-axis • left normal)
and etc..
 
vertical distance = (top size + bottom size) / (2 * sin(vertical FoV / 2))
horizontal distance = (leftSize + rightSize) / (2 * sin(horizontal FoV / 2))
 
distance = max(vertical distance, horizontal distance)
 
camera position = normalized camera direction * distance
No idea what happened in the before and after shots. The camera should move in the plane of the screen keeping the same rotation. In this case it should have moved mostly downwards to centre the box.

I'm not too worry about the panning yet, I probably did something wrong. I'm just trying to figure out the distance calculation.

I'm not sure the distance is wrong, looking at that. Without the panning it's hard to say.

Okay, lets deal with the panning first.

So I've calculated a horizontal panning distance of 147 and a vertical panning distance of -448. So this means I need to pan along the camera's x-axis 147 units to the right and along the camera's y-axis 448 units down (first flag, it looks like I should be panning to the left, not the right). I calculate the normalized vectors that represent the camera's x and y-axis and multiply those vectors with the corresponding magnitudes and now I have the world coordinates.

Question, when you say pan do you mean move the camera? That's what I think when I hear pan. The problem with this is that the camera is positioned such that it is viewing the box at the given heading and elevation, if I move the camera this is now no longer the case. IE: The camera is something like 545 units up in the air as part of the 45° elevation, if I move the camera down the world version of 448 units, the camera is know, like, 15° elevated. The above picture is the camera LOOKING AT the calculated world coordinates of (147, -448).

This topic is closed to new replies.

Advertisement