Jump to content
  • Advertisement
Sign in to follow this  
enigmatix

Left-Handed LookAt

This topic is 3973 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

In XNA, there is no LookATLH function. By default, XNA is right handed. I've been trying to code my own left handed function but I haven't been getting the proper results. Lets just say the camera is at the origin (0,0,0) and its looking down the positive x-axis (1,0,0) That means the camera has not been rotated. All angles are 0. Basically, i want the +Z-axis to be the left and the +Y-axis to be up. I've been getting either to be flipped in in positive or negative, so +Y is down and Z+ is right, which is incorrect. Does anyone know how to setup a LookATLH Matrix manually? Is there a way i can do this using quaternions? Thanks.

Share this post


Link to post
Share on other sites
Advertisement
Wasn't this somewhere in the DirectX SDK docs?

Anyway, this replacement seemed to work fine for me:


LookAtLH( const vector3& position,
const vector3& target,
const vector3& upVector )
{
// geprüft
vector3 zaxis = target - position;
zaxis.normalize();

vector3 xaxis = upVector.cross( zaxis );
xaxis.normalize();

vector3 yaxis = zaxis.cross( xaxis );

m[0][0] = xaxis.x;
m[0][1] = yaxis.x;
m[0][2] = zaxis.x;
m[0][3] = 0;

m[1][0] = xaxis.y;
m[1][1] = yaxis.y;
m[1][2] = zaxis.y;
m[1][3] = 0;

m[2][0] = xaxis.z;
m[2][1] = yaxis.z;
m[2][2] = zaxis.z;
m[2][3] = 0;

m[3][0] = -xaxis.dot( position );
m[3][1] = -yaxis.dot( position );
m[3][2] = -zaxis.dot( position );
m[3][3] = 1.0f;
}

Share this post


Link to post
Share on other sites
Instead of making the look direction (1,0,0), make it (0,0,1).

Thus, positive Z is forward, positive Y is up, and positive X is to the right. To my knowledge, this is standard. It's even where the term "Z buffer" comes from. Meaning the depth buffer represents how far forward from you in the Z direction a pixel is.

I haven't found I've had to change my thinking or do any hacks like this since switching from direct3d to xna. If anything, it always annoyed me greatly that direct3d uses left handed matrices, and it's nice to be using right handed ones.

Share this post


Link to post
Share on other sites
gharen2: Oddly enough, when I use Matrix.CreateLookAt(), if I set the look to (0,0,1), positive X still goes to the left. Essentially what I'm getting is horizontally a flipped image.

Share this post


Link to post
Share on other sites
Is there a way to flip it? I know you can flip the signs of any column of a matrix to have the objects flip, but how can you accomplish this with the camera position as well?

Share this post


Link to post
Share on other sites
The calculation of a look-at matrix is not specific to a left-handed or right-handed coordinate system.

Share this post


Link to post
Share on other sites
Well, i figured it out. I also had to calculate the perspective matrix in LH as well. In any case i found the formula here:

http://msdn2.microsoft.com/en-us/library/bb281727.aspx

Endurion also posted the correct formula for the view matrix above.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!