Maths - lookAt function - discussion

By: Yosi Hakel (t0xin) - 2008-02-28 19:29
I would really appreciate it if you could help me out regarding the lookAt() function here 
My situation is very simple 
My camera is located at point (0,0,0) 
I want to point it towards point (a,b,c) (i.e. orient the camera such that it looks at it) 
My only means of pointing/orienting the camera are EULER ANGLES (this is dictated to me by third party libraries which I cannot change) 
so my approach is very simple 
1) Build a lookAt matrix using the code from said link: 
lookAt(double a, double b, double c, matrix& m) 

vector3 up(0, 1, 0); 
vector3 z(a, b, c);  
vector3 x( up * z ); // x = up cross z  
vector3 y( z * x ); // y = z cross x  
m.set_components(x,y,z );  

2) Convert the lookAt matrix to Euler angles using the code from
rotate(matrix m)  

if (m.m10 > 0.998) { // singularity at north pole 
heading = Math.atan2(m.m02,m.m22); 
attitude = Math.PI/2; 
bank = 0; 

if (m.m10 < -0.998) { // singularity at south pole 
heading = Math.atan2(m.m02,m.m22); 
attitude = -Math.PI/2; 
bank = 0; 

heading = Math.atan2(-m.m20,m.m00); 
bank = Math.atan2(-m.m12,m.m11); 
attitude = Math.asin(m.m10); 

But the Euler angles I get don't make sense to me 
For example, using this code for the vector (0,0,1) (i.e. standing at (0,0,0) trying to look at (0, 0, 1)) produces:  
heading = attitude = bank = 0  
Whereas I'd expect : 
heading = +/-180 degs 
attitude = 0  
bank = don't care 
since we're basically looking in the direction of the Z axis 
Another example, trying to look at the vector (1,1,1) (again standing at (0,0,0)) produces 
heading = attitude = 0 
bank= ~35 degs 
Again I don't understand it, I would expect something like 
heading = 135 degs 
attitude= -45 degs 
bank= don't care 
Since we're looking at a vector symmetrical to all axes (thus 45 and 90+45 degs) 
I'm probably missing something and/or doing something wrong, so again, I would appreciate your help 
Thanks a lot in advance, 
P.S. I am aware that there's a problem when the up vector (0, 1, 0) is parallel to the vector I want to look at, but I'll cross that bridge when I get to it (or at least try to...)

metadata block
see also:


Correspondence about this page

Book Shop - Further reading.

Where I can, I have put links to Amazon for books that are relevant to the subject, click on the appropriate country flag to get more details of the book or to buy it from them.

cover If you are interested in 3D games, this looks like a good book to have on the shelf. If, like me, you want to have know the theory and how it is derived then there is a lot for you here. Including - Graphics pipeline, scenegraph, picking, collision detection, bezier curves, surfaces, key frame animation, level of detail, terrain, quadtrees & octtrees, special effects, numerical methods. Includes CDROM with code.

Other Math Books

Terminology and Notation

Specific to this page here:


This site may have errors. Don't use for critical systems.

Copyright (c) 1998-2017 Martin John Baker - All rights reserved - privacy policy.