1

I've been thinking of ways to handle placing my GUI interface in a 3D perspective projection, so that I can have 3D rotation effects on my "2D" interface elements. The problem I've been having is that it's been difficult positioning elements and making certain they all fit in view at once.

The idea I've had so far is to define a 2D quad in space to represent a "screen" surface, and layout the GUI on top of the quad in the same way I'd layout the gui in 2D. Then I'd move the camera to the center of the quad, point the camera at it and project the camera's position a certain distance along the quad's surface normal until the entire quad fits into the viewing frustum. The problem with that is I'm not sure how to find the minimum distance to project the camera position.

Anyone know an equation/algorithm for finding this?

Ian Nafiri
  • 139
  • 1
  • 12

1 Answers1

1

You could simply make a loop and increase the distance and measure until it fits... but I think with some basic trig you could find the exact distance required to make the image appear a certain size.

You want it to fill a square of a certain size. If you imagine a square of that size, with the camera sitting at some distance (d) away and centered within the image, then you would take half the width of the image you want (w), and half the viewing angle of the camera (a), and use the sine to find the distance - you know the angle, and the size of the opposite side.

So the formula is basically this: d = sin(a) * w

Jasmine
  • 4,003
  • 2
  • 29
  • 39
  • Awesome, thanks. I thought about the brute force method you mentioned, but hadn't figured out the "right" way to do it (my trig is rather spotty). I'll give this a go, but it sounds like it'll work. – Ian Nafiri Feb 02 '11 at 20:41
  • With a little help from this answer: http://stackoverflow.com/questions/2866350/move-camera-to-fit-3d-scene I got everything working like a charm. Thanks :) – Ian Nafiri Feb 02 '11 at 21:35
  • I see they also used trigonometric functions, but slightly differently. It's always better to do it that way. If your angle never changes, you can increase performance by predefining the ratio. – Jasmine Feb 03 '11 at 18:48