Global angle 3d vectors

I have 3 vectors, Up and Right, which represent the direction.

I need to convert them to XYZ angle (3 floats) so I can use with glRotatef ()

Please, help

[EDIT]

Incorrect display. can you see what its blatant here: pastebin.com/f6683492d

+2


source to share


2 answers


I am assuming you mean up, right and forward, because down is the opposite of up and has nothing to do with any new information. Your question is not very clear, but I think you want to create a transformation to a new coordinate base that is defined by the vectors you describe. If these vectors are orthogonal (they have 90 degrees between them), you don't need to go through all the computational angles and use glRotate (). Instead, you can use the vectors of the new base directly as a transformation.

Let's say that you have A (a1, a2, a3) - up, B (b1, b2, b3) - right and C (c1, c2, c3) - forward. First, if they are not completely orthogonal, then you need to make sure they become orthogonal, possibly with multiple cross products. secondly, you need to make sure their length is 1. Now create the following matrix:

a1 b1 c1 0
a2 b2 c2 0
a3 b3 c3 0
0  0  0  1

      

This is a rotation matrix that takes you from the base block to the base defined by A, B, C With this matrix, all you have to do is use glMultMatrix () and you're done. If the first try doesn't work, moving the matrix will probably fix it.




EDIT After re-checking the correct order of the matrix should be like this: for vector A (ax, ay, az), B (bx, by, bz), C (cx, cy, cz)

ax ay az 0
bx by bz 0
cx cy cz 0
0  0  0  1

      

This is a transposition of the above answer. Also, I recommend trying first to see if it works without translation. And then you can add the translation by simply adding it to the matrix like this:

ax     ay     az     0
bx     by     bz     0
cx     cy     cz     0
pos.x  pos.y  pos.z  1

      

+6


source


x = acos( dp3( nrm( up ), new vec3( 0, 1, 0 ) ) );
y = acos( dp3( nrm( dir ), new vec3( 0, 0, 1 ) ) );
z = acos( dp3( nrm( right ), new vec3( 1, 0, 0 ) ) );

      

where dp3 is a 3-component dot product, nrm normalizes a 3-component vector, and vec3 plots one as defined.



This will give you the angle between your vectr and the default coordinate base.

Edit: Of course, as stated above, you most likely already have a base matrix that you can apply. It is very easy to Orthonormalize. I really can't think of a time when you need to do what I did above .. but .. hey ... that's what you asked;)

0


source







All Articles