Unit Vectors for Rotation?
Are there any functions in DirectX that allow you to apply the rotation to an object using a unit vector instead of the usual:
float Angle = 0.0f;
D3DXMatrixRotationY(&yRot, Angle);
Device->SetTransform(D3DTS_WORLD, &yRot);
For example, if I wanted an object to face straight down the Z coordinate I would apply a unit vector of (0.0f, 0.0f, -1.0f).
-Chris
I'm not sure if this is what you meant, but if so you could have read the SDK documentation. This function allows you to rotate about an arbitrary axis.
Note, btw, that this is (as is your D3DX-call) not native DirectX but the D3DX extension framework.
Greetz,
Illco
D3DXMatrixRotationAxis( D3DXMATRIX *pOut, CONST D3DXVECTOR3 *pV, FLOAT Angle );
Note, btw, that this is (as is your D3DX-call) not native DirectX but the D3DX extension framework.
Greetz,
Illco
I was hoping that there was a function that could take in a unit vector and return a matrix with the rotations to aim an object in the direction on the unit vector. I've looked through MSDN and I have'nt found anything yet, but maybe there is a function that can do it.
-Chris
-Chris
Just take the cross product of your object's facing vector and the direction you wish to face. That will give you the vector of rotation necessary.
Right, but I was wondering how to actually apply a unit vector to an object. All I have is the position of the object and the vector to aim the object, what I need to do now is to actually rotate the object using the unit vector I have. The problem is that I do not know how to rotate an object with a unit vector, I only know how to rotate an object using:
float Angle = 0.0f;
D3DXMatrixRotationY(&yRot, Angle);
Device->SetTransform(D3DTS_WORLD, &yRot);
However, I cannot rotate the object in this way because I do not know the actually number of degrees it needs to turn, I only have a unit vector. Is there any way to actually use the unit vector to rotate the object?
-Chris
float Angle = 0.0f;
D3DXMatrixRotationY(&yRot, Angle);
Device->SetTransform(D3DTS_WORLD, &yRot);
However, I cannot rotate the object in this way because I do not know the actually number of degrees it needs to turn, I only have a unit vector. Is there any way to actually use the unit vector to rotate the object?
-Chris
You do know that the magnitude of the cross product of two unit vectors is the sine of the angle between them, right?
Basically what I have done so far is I have 2 objects sitting on a plane, one object you can move around using the arrow keys and the other object is supposed to follow the object you can move. So far the following object can follow the movable object around fine using this formula:
D3DXVECTOR3 Move;
D3DXVec3Normalize(&Move, &(vecPosition1 - vecPosition));
vecPosition.x += Move.x * timeDelta * 2.0f;
vecPosition.z += Move.z * timeDelta * 2.0f;
Where vecPosition is the position of the following object and vecPosition1 is the position of the object you can move with keys. What I need now is to rotate the following object so that it actually faces the other object that it is following. This is what I came up with, but it is not really working at all.
static float Angle = 0.0f;
D3DXVECTOR3 Facing;
//Find an arbitrary point the following object is currently facing and subtract the objects current position to get the current facing unit vector.
D3DXVec3Normalize(&Facing, &((D3DXVECTOR3( cosf(Angle) * -5.0f, 0.0f, sinf(Angle) * -5.0f )) - vecPosition));
Angle = D3DXVec3Dot(&Move, &Facing);
D3DXVec3Cross( &CrossProduct, &Facing, &Move);
D3DXVec3Normalize(&CrossProduct, &CrossProduct);
if(CrossProduct.y > 0)
Angle = -Angle;
D3DXMatrixRotationY(&yRot, Angle);
A major problem with this is that when I do the Dot product to get the angle, the return is actually the cosine of the angle between the vectors, and I'm not sure how to get the real angle from the cosine of an angle, but I will continue looking through the internet to find it.
[Edited by - bengaltgrs on August 14, 2005 9:22:03 PM]
D3DXVECTOR3 Move;
D3DXVec3Normalize(&Move, &(vecPosition1 - vecPosition));
vecPosition.x += Move.x * timeDelta * 2.0f;
vecPosition.z += Move.z * timeDelta * 2.0f;
Where vecPosition is the position of the following object and vecPosition1 is the position of the object you can move with keys. What I need now is to rotate the following object so that it actually faces the other object that it is following. This is what I came up with, but it is not really working at all.
static float Angle = 0.0f;
D3DXVECTOR3 Facing;
//Find an arbitrary point the following object is currently facing and subtract the objects current position to get the current facing unit vector.
D3DXVec3Normalize(&Facing, &((D3DXVECTOR3( cosf(Angle) * -5.0f, 0.0f, sinf(Angle) * -5.0f )) - vecPosition));
Angle = D3DXVec3Dot(&Move, &Facing);
D3DXVec3Cross( &CrossProduct, &Facing, &Move);
D3DXVec3Normalize(&CrossProduct, &CrossProduct);
if(CrossProduct.y > 0)
Angle = -Angle;
D3DXMatrixRotationY(&yRot, Angle);
A major problem with this is that when I do the Dot product to get the angle, the return is actually the cosine of the angle between the vectors, and I'm not sure how to get the real angle from the cosine of an angle, but I will continue looking through the internet to find it.
[Edited by - bengaltgrs on August 14, 2005 9:22:03 PM]
The get the angle from a cosine, you call the arccosine function acos() (cos-1 in math notation). You must make sure that the parameter is in the range [-1,1] because sometimes the dot product may go outside of that range, due to precision errors. Your code might look like this:
[Edited by - JohnBolton on August 15, 2005 1:08:40 AM]
float c = D3DXVec3Dot(&Move, &Facing); if ( c < -1.0f ) c = -1.0f if ( c > 1.0f ) c = 1.0f; Angle = acosf( c );
[Edited by - JohnBolton on August 15, 2005 1:08:40 AM]
Its not clear what your code is doing but here is a simple way to rotate an object to face another. Assuming that vecPosition and vecPosition1 are the same as in your code:
D3DXVECTOR3 diff = vecPosition1 - vecPosition; // Get the vector from the observer to the observed float angle = atan2( diff.x, diff.z ); // Compute the rotation angle (from the Z axis) D3DXMatrixRotationY( &yRot, angle ); // Get the transformation
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement