# Thread: Compute the angle to rotate for 2 vectors

1. ## Compute the angle to rotate for 2 vectors

I understand i need to get the Dotproduct of the 2 vectors and then set the rotationz for the bottom teapot so it looks up at the other above it ,do you have to convert the DotProduct to radians as its in degrea's?

vectors are

D3DXVECTOR3 TeaPotPos(0.0f,-3.0f,-10.0f);
D3DXVECTOR3 TeaPotPos2(0.0f,3.0f,-10.0f);

pic included

http://img709.imageshack.us/img709/1994/helpk.jpg

thanks for looking

2. Does directX not have a "rotate" function?

3. yes it does but i need to find the exact value which is a float to rotate

4. What exactly are you looking for? If you're simply looking for converting the angle from degrees to radian, then you probably already know that 180 degrees is equal to pi radians.

5. Well there are a few ways I know of to do this.

First, the hard way.
1. Create a vector from the bottom teapot to the top teapot and normalize it.
2. Cross the look vector of the bottom teapot with the vector you just created. This is the axis of rotation.
3. Calculate the dot product of the vector from step 1 with the look vector of the bottom teapot. This is the cosine of the angle of rotation.
4. Rotate the bottom teapot on the axis found in step 2 by the arccosine of the dot product found in step 3. This can be done with a simple D3DXMatrixRotationAxis() and passing in the params you just calculated.

Now, the easy way:
1. Use D3DXMatrixLookAtLH() to create a matrix that will look at the top teapot from the bottom teapot
2. Extract the up, right, and look vectors from the resulting matrix
3. Ortho-normalize the 3 vectors
4. Set the bottom teapot's up, right, and look vectors to the normalized vectors.

For a simple top-down 2D rotation:
1. Create a vector from the bottom teapot to the top teapot and normalize it.
2. The Z rotation is calculated via atan2f(y,x). x and y here are the x and y components of the vector created in step 1. atan2f(y,x) computes the arctangent of x or the arctangent of y/x.

Note that this will cause the bottom teapot to 'snap' to the correct orientation. If you want a smooth transition you will need to lerp from it's current orientation to it's the orientation you just calculated.