2
\$\begingroup\$

I'm trying to draw a 2D plane that has minimum dimensions to contain a 3D model (a sphere). How do I calculate the minimum size and position of the rectangle to completely cover a 3D model on screen?

\$\endgroup\$
2
  • \$\begingroup\$ can you elaborate why do you want to calculate the dimensions in 2D plane (rectangle), rather than in 3D (bounding box)? maybe you are asking the wrong question? \$\endgroup\$
    – concept3d
    Nov 8, 2013 at 19:59
  • \$\begingroup\$ So basically you want to cover a bounding box on 2 specific axis? You should be able to calculate each length on each axis by the boundingbox struct. Then simply create your plane with these dimensions. \$\endgroup\$
    – Sidar
    Nov 8, 2013 at 20:09

1 Answer 1

5
\$\begingroup\$

Loose fitting, fast

Generate a bounding AABB, which you likely already have (and is super cheap to compute for a sphere). Project the AABB's corners to the screen. Take the maximum and minimum X and Y values of the projected coordinates to form bounds of screen-space rectangle.

This will be at least as large as the object. Depending on camera orientation, the AABB may extend past the visible edges of the object, so the rectangle will be larger than the object (but no smaller).

Close fitting, slow, generalized

The above algorithm is mostly the same except we no longer want an AABB. Rather we want a bounding box aligned to the camera. One way to do this is to take the camera's up and right vectors and to find the points on the mesh/object farthest point in those directions and their inverses.

This is quite easy, albeit slow. For a plain mesh or triangulated object take each vertex and project it along the input axis (+X in camera space, -X in camera space, etc., all unit vectors). In this projection you'll compute a dot product between the point (treated as a vector) and the unit direction vector; simply keep the vertex with the largest dot product which is the point furthest along that direction vector. You can avoid needing to iterate over all vertices for both the positive and negative versions of a direction vector by taking both the largest and smallest dot product vertices. Once you have these vertices you project those to the screen to get their screen-space coordinaes and use those as bounds for your screen-space rectangle.

For more mathematical shapes like a perfect sphere, you need to find the function that computes this equivalence. For a sphere it's super easy: the vertex "position" of the farther point on the sphere in any direction is the center point of the sphere plus the radius scaling the target direction. More complex shapes can have very intricate functions, but thankfully you generally always use vertices/meshes in games.

Since the sphere is so special, if that's all you care about you can simplify all of the above for just that case. Center point of sphere plus the camera up/right vectors scaled by the radius.

\$\endgroup\$
2
  • \$\begingroup\$ Sory but could you be more explicit , i'm totaly new to xna 3D all that i've done until now is to draw some models i don't know what are you talking about, or some code cold be very helpfull \$\endgroup\$ Nov 9, 2013 at 9:38
  • \$\begingroup\$ @PinteLaurentiu: I'm unsure which part to expound upon. If you don't know what projecting a vector means or how to do it or don't know what an AABB is or what the standard 3D camera basis is, you maybe need to step back and start at the foundation. There should be info right here on GDSE on those if you search bit, or Google it. Or if you have specific questions not answered here already, post a new question. \$\endgroup\$ Nov 10, 2013 at 6:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .