# Best way to find nearest face of a cube

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

## 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 on other sites

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.

1. 1
2. 2
3. 3
4. 4
5. 5
Rutin
17

• 10
• 12
• 9
• 12
• 37
• ### Forum Statistics

• Total Topics
631420
• Total Posts
2999987
×