0
\$\begingroup\$

I have the following problem. I have a camera that is initially looking at a cube. The cube is with center at (0, 0, 0). The camera that is initially on (0, 0, 60). The camera can rotate and translate freely around the world while the cube stays fixed. I want for a given position of the camera, to determine which side of cube is oriented to "up", "left", "right", "down", "front" and "back". For example on the given picture we see thaht we are looking at left side of cube and on right side is the front. enter image description here

I know how to solve the problem if the camera is fixed, but the cube rotates(apply the inverse of the transformation matrix, of the cube, to the normal of face, in which we are interested in(for example (-1, 0, 0) for left) than find face with normal with smallest angle to this vector). But I cant design an algorithm for the camera moving case.

Also I should probably add that the camera stays looking at (0, 0, 0) the whole time. I think that might be relevant...

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Take the normal vector of the face you care about, and multiply it by the cube's transformation matrix (not its inverse, unless you mean the inverse-transpose which you'd use to transform normals in the presence of non-uniform scale) to get it into world space.

Next, multiply it by the camera's view matrix (the inverse of the camera's world transformation matrix) to get it into view space.

Now it's in a space where +1 x means it's facing "right" from the camera's point of view, +1 y means it's facing "up", +1 z means it's facing "toward" the camera, and -1 z means it's facing "away". (If you're using an OpenGL right-handed coordinate system - adjust as needed for your coordinate conventions)

So, you can find which component of your vector is farthest from zero (greatest in absolute value), and label the face as facing that corresponding direction from the camera's point of view.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .