Jump to content
  • Advertisement
Sign in to follow this  

Problem with matrix lookat method

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

If you intended to correct an error in the post then please contact us.

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 this post

Link to post
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.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!