I have a Cube with a unique color for each face. I made a script where I can swipe and rotate the Cube in increments of 90 degrees in each direction. what's the best way to determine which face of a the Cube is facing the camera.

My scene


Iterating through the faces:

faceNormal[i].dot(-worldViewVector) > 0.8 //(should actually be >0.9999)

If you do an if/switch to apply a rotation to the cube(or camera):
Just update and store a value indicating the front face at the same time.

This actually works the same for non-snapping angles such as your scene view. In the orthogonal mode, the dots just happen to be convenient, namely 1,0,0,0,0,-1, so the front face is obvious. Also consider that if you happen to find the back face first, the front face is the one opposite one; a little shortcut.

If (-worldViewVector) is always (0,0,1) (toward the user)(camera fixed, cube rotates):
You can just apply the inverse of the cube's rotation matrix to (0,0,1) and should end up with a unit direction vector indicating the front face (relative to the initial front face). This modifies the iteration:

   thisCubesDirection = (0,0,1) * cube.RotationOnlyMatrix;
   if (thisCubesDirection.X > 0.999) ...
   else if ...
  • 1
    \$\begingroup\$ Cheers! Be sure to keep separate rotation-only matricies. \$\endgroup\$ – Jon Apr 6 '15 at 1:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.