ramsestom Posted June 16, 2016 Share Posted June 16, 2016 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? Quote Link to comment Share on other sites More sharing options...
adam Posted June 16, 2016 Share Posted June 16, 2016 I'm sure there is an easier way, but I would do something like this: http://www.babylonjs-playground.com/#235QZM#5 jerome and ramsestom 2 Quote Link to comment Share on other sites More sharing options...
ramsestom Posted June 16, 2016 Author Share Posted June 16, 2016 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? Quote Link to comment Share on other sites More sharing options...
adam Posted June 17, 2016 Share Posted June 17, 2016 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. jerome and ramsestom 2 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.