# Java – Global angle of 3d vectors

anglejavamathopenglvector

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

#### Best Solution

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
``````