Jump to content
  • Advertisement
Sign in to follow this  
CHTHONIC

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

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


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

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by CHTHONIC
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.


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 this post


Link to post
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 this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!