• Create Account

### #ActualGamer Pro

Posted 19 February 2013 - 05:13 PM

Alright final question. I have everything setup, I have 1 last thing to do. I want to maximise the box in my view (it's not as easy as it sounds). Insofar, I have my camera creation broken down into 3 steps (if I'm doing anything wrong or weird feel free to set me straight). Here's my pseudo code.

Step 1, I initialize the camera such that the entire box is in my viewing frustum.

// Calculate box's dimensions after rotatations.
vector dimensions = box dimensions
matrix rotation = elevation rotation * heading rotation // XYZ euler order.
dimensions = dimensions * rotation

// Figure out how to size box so all if it fits in view.
horizontalPercent = dimensions.x / screenX
verticalPercent = dimensions.y / screenY

// Calculate distance.
if horizontalPercent > verticalPercent then
opposite = dimensions.x / 2
distance = opposite / tan(horizontalFoV / 2)
otherwise
opposite = dimensions.y / 2
distance = opposite / tan(verticalFoV / 2)

// Distance lets me see entire box if side is at origin, box is centered on origin so add half its depth.
distance = distance + dimensions.z / 2

// And create camera....


Step 2, figure out where the rendered box's center is

// Because the entire box fits on screen, I can calculate each corner's Normalized Device Coordinates.
For each corner
point = projectPointToScreen(point)
// update the max and min x & y values.

centerX = (maxX + minX) / 2
centerY = (maxY + minY) / 2


Step 3, figure out where in world space the rendered box's center is and look at that.

camera.lookAt = projectPointToWorld(centerX, centerY)


Now this works great; the box is perfectly centered on screen and when I'm looking straight down (elevation = 90) the box is maximised. But if I'm looking at it at an angle (elevation != 90) there is space on the top and bottom of it. I would like to calculate the camera distance so that the box is maximised. I'm not sure how to do that that and it would have to be back in step 1; because if I move the camera closer (or further) then the gap at the bottom of the box is going to close quicker than the gap at the top (due to perspective) and the box is no longer centered in my view.

Any ideas?

### #2Gamer Pro

Posted 19 February 2013 - 05:12 PM

Alright final question. I have everything setup, I have 1 last thing to do. I want to maximise the box in my view (it's not as easy as it sounds). Insofar, I have my camera creation broken down into 3 steps (if I'm doing anything wrong or weird feel free to set me straight). Here's my pseudo code.

Step 1, I initialize the camera such that the entire box is in my viewing frustum.

// Calculate box's dimensions after rotatations.
vector dimensions = box dimensions
matrix rotation = elevation rotation * heading rotation // XYZ euler order.
dimensions = dimensions * rotation

// Figure out how to size box so all if it fits in view.
horizontalPercent = dimensions.x / screenX
verticalPercent = dimensions.y / screenY

// Calculate distance.
if horizontalPercent > verticalPercent then
opposite = dimensions.x / 2
distance = opposite / tan(horizontalFoV / 2)
otherwise
opposite = dimensions.y / 2
distance = opposite / tan(verticalFoV / 2)

// Distance lets me see entire box if side is at origin, box is centered on origin so add half its depth.
distance = distance + dimensions.z / 2

// And create camera....


Step 2, figure out where the rendered box's center is

// Because the entire box fits on screen, I can calculate each corner's Normalized Device Coordinates.
For each corner
point = projectPointToScreen(point)
// update the max and min x & y values.

centerX = (maxX + minX) / 2
centerY = (maxY + minY) / 2


Step 3, figure out where in world space the rendered box's center is and look at that.

camera.lookAt = projectPointToWorld(centerX, centerY)


Now this works great; the box is perfectly centered on screen and when I'm looking straight down (elevation = 90) the box is maximised. But if I'm looking at it at an angle (elevation != 90) there is space on the top and bottom of it. I would like to calculate the camera distance so that the box is maximised. I'm not sure how to do that that and it would have to be back in step 1; because if I move the camera closer (or further) then the gap at the bottom of the box is going to close quicker than the gap at the top (due to perspective) and the box is no longer centered in my view.

As I'm writing this I think I could probably calculate a new field of view. I can try it tomorrow (long day), but I would consider this a last resort as I would like my field of fiew to be constant except for when I'm zooming in/out (one can argue that's what I want).

Any ideas?

### #1Gamer Pro

Posted 19 February 2013 - 05:10 PM

Alright final question. I have everything setup, I have 1 last thing to do. I want to maximise the box in my view (it's not as easy as it sounds). Insofar, I have my camera creation broken down into 3 steps (if I'm doing anything wrong or weird feel free to set me straight). Here's my pseudo code.

Step 1, I initialize the camera such that the entire box is in my viewing frustum.

// Calculate box's dimensions after rotatations.
vector dimensions = box dimensions
matrix rotation = elevation rotation * heading rotation // XYZ euler order.
dimensions = dimensions * rotation

// Figure out how to size box so all if it fits in view.
horizontalPercent = dimensions.x / screenX
verticalPercent = dimensions.y / screenY

// Calculate distance.
if horizontalPercent > verticalPercent then
opposite = dimensions.x / 2
distance = opposite / tan(horizontalFoV / 2)
otherwise
opposite = dimensions.y / 2
distance = opposite / tan(verticalFoV / 2)

// Distance lets me see entire box if side is at origin, box is centered on origin so add half its depth.
distance = distance + dimensions.z / 2

// And create camera....


Step 2, figure out where the rendered box's center is


// Because the entire box fits on screen, I can calculate each corner's Normalized Device Coordinates.
For each corner
point = projectPointToScreen(point)
// update the max and min x & y values.

centerX = (maxX + minX) / 2
centerY = (maxY + minY) / 2


Step 3, figure out where in world space the rendered box's center is and look at that.

camera.lookAt = projectPointToWorld(centerX, centerY)


Now this works great; the box is perfectly centered on screen and when I'm looking straight down (elevation = 90) the box is maximised. But if I'm looking at it at an angle (elevation != 90) there is space on the top and bottom of it. I would like to calculate the camera distance so that the box is maximised. I'm not sure how to do that that and it would have to be back in step 1; because if I move the camera closer (or further) then the gap at the bottom of the box is going to close quicker than the gap at the top (due to perspective) and the box is no longer centered in my view.

As I'm writing this I think I could probably calculate a new field of view. I can try it tomorrow (long day), but I would consider this a last resort as I would like my field of fiew to be constant except for when I'm zooming in/out (one can argue that's what I want).

Any ideas?

PARTNERS