# Unit Vectors for Rotation?

This topic is 4909 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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

##### Share on other sites
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.
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

##### Share on other sites
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

##### Share on other sites
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.

##### Share on other sites
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

##### Share on other sites
You do know that the magnitude of the cross product of two unit vectors is the sine of the angle between them, right?

##### Share on other sites
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]

##### Share on other sites
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:
    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]

##### Share on other sites
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

##### Share on other sites
Well, I tried it and still not working correctly. Thank you for the help, but I'll just have to keep messing with it until I get it right. I think the easiest way might be to write something that converts a unit vector into actual rotational values.

-Chris

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 9
• 11
• 9
• 9
• ### Forum Statistics

• Total Topics
634135
• Total Posts
3015752
×