# [C++/DX9.0c] How to rotate to face a point in 3D space

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

## Recommended Posts

First off I have read and tried to use all off the suggestions already logged on this subject and have managed to get none of them to work. For now all I want to do is to obtain the rotational values around each axis (x,y and z) so that one point is made to 'face' another. So for example if there is a point at (1,2,3) i want it to face another point that is at (4,5,6). Sorry if this appears obvious, the penny just did not drop when i was reading the other relating posts on this forum. If someone could provide a step-through using the example above I would be much obliged. Alex.

##### Share on other sites
I'm not very sure of myself here (fairly new to 3D), but I think this is the right track -

You have current directional vector. A
Calculate a new vector from point a to point b (b-a). B
Normalize the vectors
Take the cross product of A and B to get the axis of rotation.
Take the dot product of A and B to get the cos(theta) - which you can get the angle of rotation from.
Rotate around the arbitrary axis by theta.

Like I said, I'm not very sure of this, but I hope it helps somehow :P

##### Share on other sites
Thanks for the reply, I have tried what you have suggested already and had no luck, Below is the DX code:

D3DXVec3Subtract(&difference,&target,&currentPosition);D3DXVec3Normalize(&difference,&difference);D3DXVec3Normalize(¤tPosition,&currentPosition);D3DXVECTOR3 crossProduct;D3DXVec3Cross(&crossProduct,&currentPosition,&difference);float dotProduct = D3DXVec3Dot(&currentPosition,&difference);float angle = acos(dotProduct);D3DXVec3Scale(&crossProduct,&crossProduct,angle);SetRotation(crossProduct);

Where "target" is the point to face, "currentPosition" is where the object currently is, "difference" is the difference between the two aforementioned.

##### Share on other sites
Just use D3DXMATRIXLookAtLH or D3DXMatrixLookAtRH depending on your view.

D3DXMATRIX * D3DXMATRIXLookAtLH(
D3DXMATRIX * pOut,
CONST D3DXVECTOR3 * pEye,
CONST D3DXVECTOR3 * pAt,
CONST D3DXVECTOR3 * pUp
);

This functions takes your eye position and the at and up vectors and computes your matrix. In your example:
D3DXVECTOR3 vEye(1,2,3);
D3DXVECTOR3 vAt(4,5,6);
D3DXVECTOR3 vUp(0,1,0); //depending on your up direction
D3DXMATRIX vMat;
D3DXMATRIXLookAtLH(&vMat, &vEye, &vAt, &vUp);

vMat will hold your transform. It also works for billboards (eye in the object and at in the camera), for shadows (sometimes you want some polygons facing the light) and basically any kind of look at transformation.

Luck!
Guimo

##### Share on other sites
I have tried this as well and it still is not right. I actually need the angle around each axis.

D3DXVECTOR3 target = D3DXVECTOR3(x_OfNewPosition,y_OfNewPosition,z_OfNewPosition);		D3DXMATRIX lookAtMat;		D3DXMatrixLookAtLH(	&lookAtMat,							&m_translation,							&target,							&(D3DXVECTOR3(0,1,0))	);		D3DXVECTOR3 scale;		D3DXQUATERNION rotation;		D3DXVECTOR3 translation;		D3DXMatrixDecompose(	&scale,								&rotation,								&translation,								&lookAtMat	);		D3DXVECTOR3 axisRotationVec;		float axisRotationFloat;		D3DXQuaternionNormalize(&rotation,&rotation);		D3DXQuaternionToAxisAngle(&rotation,&axisRotationVec,&axisRotationFloat);		D3DXVec3Scale(&axisRotationVec,&axisRotationVec,axisRotationFloat);		SetRotation(axisRotationVec);

##### Share on other sites
Are you trying to rotate the camera? An object? Not sure what you mean by rotating a point.

##### Share on other sites
Sorry for not being clear. For example, an object at translation (1,2,3) is going to move to a point at (4,5,6) over a period of time, I want that object to turn and face that point before it begins moving.

Alex.

##### Share on other sites
After doing yet more reading (I am not a maths person!) i realised i should be dividing the angle scale factor not multiplying, but this still does not work:

D3DXVECTOR3 target = D3DXVECTOR3(x_OfNewPosition,y_OfNewPosition,z_OfNewPosition);D3DXMATRIX lookAtMat;D3DXMatrixLookAtLH(&lookAtMat,&m_translation,&target,&(D3DXVECTOR3(0,1,0)));D3DXVECTOR3 scale;D3DXQUATERNION rotation;D3DXVECTOR3 translation;D3DXMatrixDecompose(&scale,&rotation,&translation,&lookAtMat);if(rotation.w > 1.0f)   D3DXQuaternionNormalize(&rotation,&rotation);float angle = 2 * acos(rotation.w);float s = sqrt(1 - rotation.w * rotation.w);D3DXVECTOR3 rotVec;if(s < 0.001){	rotVec.x = rotation.x;	rotVec.y = rotation.y;	rotVec.z = rotation.z;}else{	rotVec.x = rotation.x / s;	rotVec.y = rotation.y / s;	rotVec.z = rotation.z / s;}rotVec *= angle;SetRotation(rotVec);

##### Share on other sites
Quote:
 Original post by CHTHONICSorry for not being clear. For example, an object at translation (1,2,3) is going to move to a point at (4,5,6) over a period of time, I want that object to turn and face that point before it begins moving.Alex.

In that case, you should be able to use Guimo's post -

D3DXMATRIX * D3DXMATRIXLookAtLH(
D3DXMATRIX * pOut, <-- transform
CONST D3DXVECTOR3 * pEye, <-- objects position (1,2,3)
CONST D3DXVECTOR3 * pAt, <-- the point the object wants to turn to (4,5,6)
CONST D3DXVECTOR3 * pUp <-- the objects up vector (0,1,0)
)

##### Share on other sites
I actually need to retrieve the angle of rotation around each axis though (x,y z), how do i retrieve that from the matrix?

Alex.

##### Share on other sites
What you are going to need to do before you start coding all of this, is you need to visualize what is going on. I think you want your camera to face some point? This is what I have gathered. Well you will be dealing with two things here, your camera's view vector, and the point to where you are looking at.

First try and draw in 3D space, on paper or something, two points; one representing where your camera is, (1,2,3) and one where the other object is (4,5,6).

To make things simple, lets assume you are on the X-Z Plane, that means that the View Vector's values of X and Z will be changed in order to change your view to look at your object.

Without getting to technical and into too much math, (You said you weren't a math guy), Let's just say that the angles are represented as, + and -, so +X, -X, +Z, -Z.

Now here is a structure of your Camera,

Let's say that vView is your View Vector and vPos is the Camera's Position.

vPos.x, vPos.y, vPos.z
vView.x, vView.y, vView.z
0, 1, 0 );

Now we want to change these values so we need to get the Current View Vector, and set it to let's say, vVector.

vVector = vView - vPos; (This is subtracting Vectors remember)

Now here is the math that you don't like, so we have taken our + or - angle and set it to fSpeed. That just means the bigger the value for fSpeed, the more you will rotate your view.

vView.z = (float)(vPos.z + sin(fSpeed) * vVector.x + cos(fSpeed) * vVector.z);
vView.x = (float)(vPos.x + cos(fSpeed) * vVector.x - sin(fSpeed) * vVector.z);

you can see that having a positive angle in the X direction, you will move your view to the right.

Now you can be creative and adjust the fSpeed values and try and understand what is happening when you change the values, and from there I think you will be able to point your camera at an object.

##### Share on other sites
Thanks guys.

nerditup: No its not the cameras view i want to change. I want to change one object in 3d space to face a particular point in 3d space. I want to know how to work out the rotations around each axis (the Yaw, Pitch and Roll) required to do this.

Alex.

##### Share on other sites
nerditup,

I would be much obliged if you could give me a worked example, possibly using (1,2,3) looking at (4,5,6).

Alex.