views:

118

answers:

2

I want to calculate which face on a cube has been clicked. I've got the mouse to 3D down, and I can draw things, in 3D, at the mouse's position.

All I need to do now is calculate WHAT face of a cube is being touched.

EG. I have a function, when I pass the size, position and mouse position to it, it returns the face. Top, bottom, left, right, front, back.

Sounds simple, but I don't have the foggiest.

Thanks all, SilentC

+1  A: 

not sure what API are you using but OpenGL has a selection API, which does this for you

oykuo
+1  A: 

You just have to calculate a line plane intersection for every face of the cube with the line defined by the camera location and the mouse position on the view plane. Throw out all intersection that are in the plane but not on the face of the cube and pick the closest one.

You can also use line triangle intersections if you represent the cube as twelve trinagles. But in both case you have to provide much more information for a more specific answer.

Daniel Brückner
So the line that is defined would be from the camera to the mouse, right?
Also, I'm assuming that I'd have compensate for rotation, right?
The line from the camera **through** the mouse to infinity intersects one or more faces (usually two if there is only a single cube). Ignore degenerate cases like edges, vertexes, and any face that is parallel to the line. It might be worthwhile to cull out back-facing faces. The user almost certainly wants the closest face, but you might want to provide an affordance to choose among the faces so that the user can pick a concealed object if they want to.
RBerteig

related questions