D3DXVECTOR3 vLook, vUp, vRight, vTemp = D3DXVECTOR3(0,1,0);
//get the normalised facing direction vector
D3DXVec3Normalize(&vLook, lpDirection);
//get right vector perpendicular to "facing" and another vector
D3DXVec3Cross(&vRight, &vLook, &vTemp);
D3DXVec3Normalize(&vRight, &vRight);
//get *real* up vector from other real vectors
D3DXVec3Cross(&vUp, &vLook, &vRight);
D3DXVec3Normalize(&vUp, &vUp);
//make final matrix
D3DXMatrixIdentity(lpRetMatrix);
lpRetMatrix->m00 = vRight.x;
lpRetMatrix->m01 = vRight.y;
lpRetMatrix->m02 = vRight.z;
lpRetMatrix->m10 = vUp.x ;
lpRetMatrix->m11 = vUp.y;
lpRetMatrix->m12 = vUp.z;
lpRetMatrix->m20 = vLook.x;
lpRetMatrix->m21 = vLook.y;
lpRetMatrix->m22 = vLook.z ;
//put the translation into the matrix
lpRetMatrix->m30 = lpPosition->x;
lpRetMatrix->m31 = lpPosition->y;
lpRetMatrix->m32 = lpPosition->z;
Vector based rotation question
I need to rotate an object based on its direction, so I looked into how rotation matrixes work. Here is some code I found (S1CA may find it farmilliar ) but it seems to be made for right-handed coords.
EDIT: Changed code to working version for anyone who is interested
I invert the X axis of the look matrix, which works in most cases. Sometimes it doesn't though, how do I properly make a rotation matrix for left-handed systems?
[edited by - Cybertron on May 7, 2002 5:55:10 PM]
hi
to rotate an object around an arbitrary axis, you may use the D3DMatrixRotationAxis function, specifying the local Z (direction) axis of your object.
tibo
to rotate an object around an arbitrary axis, you may use the D3DMatrixRotationAxis function, specifying the local Z (direction) axis of your object.
tibo
1. Changing the order of the parameters for the cross product will get you the perpendicular in the opposite direction...
2. Negating Z is a more usual way to go between LHS and RHS.
3. There is one potential issue with that code (which I''m sure I mentioned at the time). If the lookAt direction is the same or exactly opposite to the temporary ''up'' vector (parallel to), then that code will fail. If you need to test for that (i.e. you''re ever looking straight up or straight down) you should test whether the vectors are parallel...
p = fabs( lookAt DOT tempUp )
if p=1 the tempUp is the same direction as the lookAt (or exactly opposite) so you you should choose a different tempUp - i.e. 0,0,1 or 1,0,0.
4. We use a slightly different form of the same kind of operation in our left handed stuff all the time (ours uses a dot product projection instead of the first crossproduct)
The code I posted was from memory though rather than tested code - try swapping one or both of the cross product orders.
--
Simon O''Connor
Creative Asylum Ltd
www.creative-asylum.com
2. Negating Z is a more usual way to go between LHS and RHS.
3. There is one potential issue with that code (which I''m sure I mentioned at the time). If the lookAt direction is the same or exactly opposite to the temporary ''up'' vector (parallel to), then that code will fail. If you need to test for that (i.e. you''re ever looking straight up or straight down) you should test whether the vectors are parallel...
p = fabs( lookAt DOT tempUp )
if p=1 the tempUp is the same direction as the lookAt (or exactly opposite) so you you should choose a different tempUp - i.e. 0,0,1 or 1,0,0.
4. We use a slightly different form of the same kind of operation in our left handed stuff all the time (ours uses a dot product projection instead of the first crossproduct)
The code I posted was from memory though rather than tested code - try swapping one or both of the cross product orders.
--
Simon O''Connor
Creative Asylum Ltd
www.creative-asylum.com
When I change the code that gets vRigh it works perfectly while panning horizontally, But when I pitch vertically it does not. Changing the code for vUp makes the object dissapear!
So reversing the order of the first Cross product fixes the horizontal differences of the LHS and RHS, but how do I fix the vertical differences?
So reversing the order of the first Cross product fixes the horizontal differences of the LHS and RHS, but how do I fix the vertical differences?
I fixed it, Here is the solution
lpRetMatrix->m00 = vRight.x;
lpRetMatrix->m01 = vRight.y;
lpRetMatrix->m02 = vRight.z;
lpRetMatrix->m10 = vUp.x ;
lpRetMatrix->m11 = vUp.y;
lpRetMatrix->m12 = vUp.z;
lpRetMatrix->m20 = vLook.x;
lpRetMatrix->m21 = vLook.y;
lpRetMatrix->m22 = vLook.z ;
It was just accessing the wrong item of the matrix, I noticed it while looking at some similar code that gets the vectors out of a matrix
lpRetMatrix->m00 = vRight.x;
lpRetMatrix->m01 = vRight.y;
lpRetMatrix->m02 = vRight.z;
lpRetMatrix->m10 = vUp.x ;
lpRetMatrix->m11 = vUp.y;
lpRetMatrix->m12 = vUp.z;
lpRetMatrix->m20 = vLook.x;
lpRetMatrix->m21 = vLook.y;
lpRetMatrix->m22 = vLook.z ;
It was just accessing the wrong item of the matrix, I noticed it while looking at some similar code that gets the vectors out of a matrix
Aha - the old row major versus column major matrix issue. It happens to me once in a while too - particularly when reading books which have matrices the other way round or trying to plug matrices I''m familiar with into APIs I''m not so familiar with .
The way I personally confirm which way the matrix elements go is to see where translations get set - to the right or to the bottom.
BTW: Another way of finding out whether the direction vector is the same or exact opposite (parallel) to the temporary up vector is to check the vRight vector after you''ve done the first cross product. If all of it''s elements are 0, the two vectors are parallel and therefore you need to choose a different temporary up vector.
--
Simon O''Connor
Creative Asylum Ltd
www.creative-asylum.com
The way I personally confirm which way the matrix elements go is to see where translations get set - to the right or to the bottom.
BTW: Another way of finding out whether the direction vector is the same or exact opposite (parallel) to the temporary up vector is to check the vRight vector after you''ve done the first cross product. If all of it''s elements are 0, the two vectors are parallel and therefore you need to choose a different temporary up vector.
--
Simon O''Connor
Creative Asylum Ltd
www.creative-asylum.com
The funny thing is using the wrong matrix positions is it still worked most of the time!
If the vectors are parallel and normalized, woudn''t the look vector have a Y value of 1 or -1? that would be faster than a dot or cross product
If the vectors are parallel and normalized, woudn''t the look vector have a Y value of 1 or -1? that would be faster than a dot or cross product
quote:Original post by Cybertron
The funny thing is using the wrong matrix positions is it still worked most of the time!
If the vectors are parallel and normalized, woudn''t the look vector have a Y value of 1 or -1? that would be faster than a dot or cross product
Using the matrix the other way round is simply doing a "transpose", which for a square rotation matrix such as the one you''re trying to create is also it''s inverse.
Checking the Y for 1 or -1 would also work (though you''ll have to check the range with epsilon - it''d be bad to simply do an "if (in.y == 1.0f)" since IEEE floats can''t always represent certain values exactly - particularly after maths operations between numbers of greatly different magnitude)
--
Simon O''Connor
Creative Asylum Ltd
www.creative-asylum.com
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement