# Problem with matrix lookat method

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

## Recommended Posts

I made my own matrix lookat method that functions just like D3DXMatrixLookAtLH. Yet when I use it in place of that directx method I end up seeing nothing but black in the game viewport. Is there an obvious flaw in this code?

I am doing this for a left handed, row major matrix. In the method, the matrix dest is intended to be initialized as a lookat transform matrix. Thanks for any help!

 INLINE void MatrixLookAtLH (Matrix& dest, Vector3D& eye, Vector3D& at, Vector3D up) { Vector3D tmp; Vector3D xAxis; Vector3D yAxis; Vector3D zAxis; tmp = at - eye; tmp.Normalize (); zAxis = tmp; tmp = up; tmp.Cross (zAxis, tmp); tmp.Normalize (); xAxis = tmp; tmp = zAxis; tmp.Cross (xAxis, tmp); yAxis = tmp; dest.Init (xAxis.x, yAxis.x, zAxis.x, 0, xAxis.y, yAxis.y, zAxis.y, 0, xAxis.z, yAxis.z, zAxis.z, 0, -xAxis.Dot (eye), -yAxis.Dot (eye), -zAxis.Dot (eye), 1); } 

##### Share on other sites
A couple of suggestions:

- You might try swapping the arguments to one or more of the calls to Cross(). Generally speaking, the relationship between the basis vectors is x = yXz, y = zXx, and z = xXy. The cross product is non-commutative, so if you get things backwards the overall results may be incorrect.

- Make sure you understand the difference between row/column major and row/column vectors, and make sure that the conventions you're using are compatible with Direct3D (in other words, that the matrix is laid out in memory in the order that Direct3D expects).

- This probably isn't the problem, but the cross product should be a binary function that doesn't modify its arguments rather than a member function that writes the result to the object on which it's called. (Arguably, at least.) Implementing the cross product in this way will make your code cleaner and easier to follow.

1. 1
2. 2
JoeJ
20
3. 3
4. 4
frob
11
5. 5

• 13
• 17
• 13
• 20
• 13
• ### Forum Statistics

• Total Topics
632191
• Total Posts
3004661

×