Sign in to follow this  
jamesl22

Best way to find nearest face of a cube

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
[quote name='James Lovejoy' timestamp='1307644383' post='4821423']
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++?[/quote]
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 [i]p[/i] 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 [i]p[/i] 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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this