#### Archived

This topic is now archived and is closed to further replies.

# LookAt cam help

This topic is 5519 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey everyone, i''m trying to work out a LookAt routine. But since i don''t have heavy math background it''s not-so-easy to figure out what''s wrong. The code i''m using is from the D3D docs and i''ve found it to work fairly ok on some geometry while acting really screwed on others. The code i''m using is zaxis = normal(At - Eye) xaxis = normal(cross(Up, zaxis)) yaxis = cross(zaxis, xaxis) xaxis.x yaxis.x zaxis.x 0 xaxis.y yaxis.y zaxis.y 0 xaxis.z yaxis.z zaxis.z 0 -dot(xaxis, eye) -dot(yaxis, eye) -dot(zaxis, eye) 1 my coordinate system is left handed and the matrix order is row major ( or is it called column major, anyway, the bottom row is translation ). It could be that i''m not using it correctly, i''m converting the mouse movement to the direction vector through spherical coordinates where the x movement becomes theta and y becomes phi. And the eye vector is just eye += at*speed.

##### Share on other sites
1) I assume you''re passing that to D3D as a view matrix. Just in case your matrix structure is transposed compared to D3Ds, try transposing the result. (For a look at matrix the transpose is in the inverse so plugging a wrongly transposed matrix in gives the reverse operation).

2)
a. The cross product (aka vector product) gives a vector perpendicular to the two input vectors, but ONLY when the two input vectors aren''t the same or exact opposites (i.e. on the same line).

b. In your case xaxis = normal(cross(Up, zaxis)) is fine *UNLESS* zaxis=Up or zaxis=-Up in which case everything will screw up.

c. The vector you call Up should be tested to make sure it isn''t the same (or exact opposite to) as the direction, if it is, then you should pick another up vector which is definately not the same as the direction (e.g. if (zaxis==up || zaxis==-up) up=[0,0,1]):

d.
if (fabsf(vec3DotProduct(zaxis,up)) > 0.9f){  up = vec3(0.0f, 0.0f, 1.0f);}... rest of code goes here

e. Depending on how whacky your camera is, and if the above "flips" at some points, use the sign of the dot product to change the sign of the z component of the "new" up vector above.

--
Simon O''Connor
Creative Asylum Ltd
www.creative-asylum.com

##### Share on other sites
Thx, figured it out though. Oh and i''m not using D3D for this. Anyway, you said that matrix is transposed compared to mine. To me the d3d matrices look like they''re row major. Becuase translation is in the last row. This is the same order i use, so the part about it being transposed got me wondering. Are you sure it''s transposed?

##### Share on other sites
I was thinking more of the organisation in memory than row major versus column major notation.

mtx[x][y] versus mtx[y][x]

And the mapping of the "_21" type variables the D3D source uses to your matrix data type (D3D deliberately reverses the elements on those variables, so in some peoples matrix types, that member would really be _12).

--
Simon O''Connor
Creative Asylum Ltd
www.creative-asylum.com