0

SO!

I was wondering if you could help me to solve one problem:

I've got a cube, which can be rotated in three axis. I can get information about cube's rotation which is an array of three angles from 0 to 2PI.

The question is: how can I identify, which side of cube is in the bottom from those three euler angles?

I think the perfect function would be something like that:

function getSideFromAngles(x,y,z) {
   // magic goes here
   // for example getSideFromAngles(Math.PI/2, 0, 0)
   // if x===PI/2 and y===0 and z===0 then return "front"
   // which means front side of cube "looks" down now.
}

Just in case - Three.js also allows me to get quaternions of the cube.

Thanks in advance for you help

Lisunov Ilia
  • 141
  • 1
  • 4
  • I don't have an answer ready for you, only considerations which can further complicate things :) ... Is your camera fixed? (down is no longer down) ... Are you allowing rotation across all 3 axis? (more complicated) ... Is the object possibly going to be a child of another object? (multiple rotations) ... Is it always a cube, or are other shapes possible? (different assumptions and logic required). And btw, `x, y, z` would be a vector, not a rotation as such. – Leeft Aug 05 '14 at 13:22
  • thanks for reply :), 1. my camera is fixed, 2. rotation is allowed across all 3 axis 3. it is always cube, 4. not sure what do you mean, but i can be wrong, i get this data from three.js - for example object.rotation gives me x,y,z which are angles, I thought. – Lisunov Ilia Aug 05 '14 at 13:23
  • http://threejs.org/docs/#Reference/Math/Euler - here is what i get, i think they use x, y, z instead of α, β, γ, but they are still angles.. – Lisunov Ilia Aug 05 '14 at 13:30
  • They need a rotation order too ... without that order, you can't determine any kind of rotation. Easier to just accept a `THREE.Euler` object (or a quaternion). – Leeft Aug 05 '14 at 13:44
  • thank you, i can accept THREE.Euler as an object, even though in my example the order is always xyz. The thing is I don't understand - how can I get side which is on bottom with specified euler angles. I can make an array of all 64 variants and just use that array, but I am sure that there should be the formula that can give an answer depending on object euler rotation values. – Lisunov Ilia Aug 05 '14 at 13:49

1 Answers1

0

First of all, in your faces you should have the normals, that gives you the base direction of the face.

Also, in your mesh you should have the matrixWorld, that should give the global result of the rotations (all of them).

Now, if you multiply that matrix by the normals, you will have the normals in world space.

Now, create a vector pointing down (0, 0, -1), and calculate the dot product between this and the normals in world space. The face having the highest value is the one that points more "downward"

vals
  • 61,425
  • 11
  • 89
  • 138