306

2
+2  Q:

## Global angle of 3d vectors

I have 3 Vectors, Up Right and Front that represent a direction.

I need to convert these into an XYZ angle (3 floats) so i can use with glRotatef()

[EDIT]

Its not rendering properly. can you see if its anything blatant here: pastebin.com/f6683492d

A:
``````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 normalises a 3 component vector and vec3 constructs one as defined.

This will give you the angle between the vectr you have and a default coordinate basis.

Edit: Of course, as pointed out above, you most likely already have a basis matrix which you can apply. Very easy to Orthonormalise as well. Realistically I can't think of a time where you'd need to do what I've done above .. but .. hey ... its what you asked for ;)

Make sure that your up, dir, and right vectors are unit length if using this method.
A VERY good point :)
this rotates half way then back on itself
+5  A:

I assume you mean "up, right and forward" because down is the opposite of up and doesn't relly contribute any new information.
Your question isn't very clear but I think you mean that you want to create a transformation to the new coordinate base which is defined by the vectors you describe. If these vectors are orthogonal (have 90 degrees between them) then you don't need to go through all the bother of calculating angles and using glRotate(). Instead you can use the vectors of the new base directly as the transformation.

Say the vectors you have are A(a1,a2,a3) - up, B(b1,b2,b3) - right and C(c1,c2,c3) - forward. First, If the are not completely orthogonal you need to make sure that they become orthogonal, possibly with a few 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 the rotation matrix which will bring you from the unit base to the base defined by A,B,C With this matrix all you need to do is to use glMultMatrix() and you're done. If the first try doesn't work, transposing the matrix would probably fix it.

EDIT After checking again, the right order of the matrix should be like so: 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 the transpose of the above answer. Also, I recommend that you first try to see if it works without translation. And then you can add translation by simply adding it to the matrix like so:

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