Best way to find nearest face of a cube

Started by
0 comments, last by Zakwayda 12 years, 10 months ago
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
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.

This topic is closed to new replies.

Advertisement