• Advertisement
Sign in to follow this  

Best way to find nearest face of a cube

This topic is 2451 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi,

When I get a collision with a bounding box, given the point of collision, size and position of the cube, what is the best way to find the colliding face and then return an array of vectors (the points of the face) in C++?

Thanks,

James

Share this post


Link to post
Share on other sites
Advertisement

When I get a collision with a bounding box, given the point of collision, size and position of the cube, what is the best way to find the colliding face and then return an array of vectors (the points of the face) in C++?

The language is secondary (the concepts will be the same regardless of language).

As for the problem you mention, here's one way to proceed:

Given a point p known to lie on the surface of the cube, start by transforming this point into the local space of the cube. Then, find the element of p for which the magnitude is greatest. (Or, more precisely, an element for which no other element has greater magnitude, which will ensure that a (possibly arbitrary) face is always returned even if the point lies on an edge or vertex.) This will tell you which of the pairs of opposing faces the point corresponds to. The sign of the element will then tell you which of these two faces the point corresponds to.

To get the corners of the face, you can generate them on the fly, or precompute and store the 8 cube corners and, for each of the 6 faces, a set of 4 indices indexing into the corner array.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement