Jump to content

Determine cube face facing camera after rotation


ramsestom
 Share

Recommended Posts

I am trying to determine the face of a cube facing toward the user after a random number of quarter rotations of a cube (I rotate the cube, not the camera that is always along the z axis). I assigned each face of the cube to a submesh and tried to check which one had a normal z value equals to -1 but the normals do not seem to be affected by the rotation (I always have the same face with a normal vector equals to (0, 0, -1) at its vertices whatever the rotation I apply and this is the face facing the user before the rotation). 

So is there a simple way to check the orientation of each face of a cube after a rotation to determine which one is perpendicular to the z axis and facing the user? 

Link to comment
Share on other sites

Thanks a lot. It is working perfectly. Could you just explain in a few sentences the math behind it. I am new to 3D and babylonjs and I still don't know what information exactly is stored in the worldmatrix (what are the values in wm.m[0], wm.m[1]....).

As I understood from your example, transformations affecting a mesh (like rotation) are not directly applied to this mesh vertices but stored in this worldmatrix (which might explain why normals values weren't directly affected by the rotation) but how is this information stored in the worldmatrix? Also could I directly apply the worldmatrix to my original normals to get the resulting ones after the rotation?

 

Link to comment
Share on other sites

The dot product of two normalized vectors will tell you what direction they are facing relative to one another.  

1 = pointing in the same direction

0 = perpendicular to each other

-1 = pointing in the exact opposite direction

 

The 0, 1, 2 index of the worldMatrix is a normal vector that points down the positive local x axis of your mesh.

4, 5, 6 is a normal vector that points to the positive y axis.

8, 9, 10 is a normal vector that points to the positive z axis.

Each one of these vectors corresponds to half of the faces of your cube.  The other half is the vectors in the opposite direction.

 

To find out which side is facing the camera, you want to know which of the vectors is closest to being aligned with the normalized vector between the cube and the camera.  I used the dot product for that and returned the side when the result was above a certain threshold.

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...