Skip to main content
gamedev.net
🔒 Locked

Problem with matrix lookat method

Started by daedalic Apr 15, 2011 at 10:06 PM 0 replies 1.4k views
Original Post
daedalic
daedalic
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);
}
Zakwayda
Zakwayda
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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.