Computing camera vectors

Started by
3 comments, last by void* 19 years, 7 months ago
I've been working on my camera class for a few days now and was wondering if my math is correct. I'm trying to calculate the Up from the Right, Position, and LookAt. And I'm trying to calculate the Right from the Up, Position, and LookAt (No, I'm not trying to do these one right after another):

// For Right vector
D3DXVECTOR3 Position(0.0f, 0.0f, -5.0f);
D3DXVECTOR3 LookAt(0.0f, 0.0f, 0.0f);
D3DXVECTOR3 Up(0.0f, 1.0f, 0.0f);
D3DXVECTOR3 Right;
D3DXVec3Cross(&Right, &Up, &(LookAt - Position));
D3DXVec3Normalize(&Right, &Right);

// For Up vector
D3DXVECTOR3 Position(0.0f, 0.0f, -5.0f);
D3DXVECTOR3 LookAt(0.0f, 0.0f, 0.0f);
D3DXVECTOR3 Right(1.0f, 0.0f, 0.0f);
D3DXVECTOR3 Up;
D3DXVec3Cross(&Up, &(LookAt - Position), &Right);
D3DXVec3Normalize(&Up, &Up);

If I output this code, the first on produces Right[1.0f, 0.0f, 0.0f] and the second produces Up[0.0f, 1.0f, 0.0f] which are correct values, but I don't have any way to tell if the values output is correct of I change any of the input vectors (still working on my renderer). I've only been learning vectors for a few days now, so any help would be appreciated! Thanks!
Advertisement
Two simple problems:

1. D3D uses a left handed coordinate system, so you will want a Left vector, not a Right vector.

2. Left = At cross Up and Up = Left cross At
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
Well, the first isn't really a problem since D3D has support for both, but I do use the left-handed CCS, so a left vector would make more sense, huh?

Thanks JohnBolton!
Ok, the algo
Left - LookAt x Up
works great unless I'm looking directly at the origin (which I understand since multiplication by 0), but how do I fix this (or will I ever have to worry about it)?

Any help would be great, I'll continue working/searching.

Thanks
suggestion:
If looking directly at the origin, just translate everything by -Position and you'll have no zero vectors (except of course your position vector, which is fine because it doesn't get crossed by anything)
Greenspun's Tenth Rule of Programming: "Any sufficiently complicated C or Fortran program contains an ad-hoc, informally-specified bug-ridden slow implementation of half of Common Lisp."

This topic is closed to new replies.

Advertisement