Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


Finding the rotation from one vector to another

This topic is 5940 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

How do you do this? Basically, I have a ray facing some direction and an effect that gets generated along the z axis. I want to find the rotation so I can rotate the effect off the z axis and to face in the direction of the ray. [edited by - beoch on September 12, 2002 9:54:52 PM]

Share this post

Link to post
Share on other sites
Guest Anonymous Poster
Search for dot product

Share this post

Link to post
Share on other sites
I''m familiar with the dot product. I know I can get the angle with it but I was thinking more along the lines of a matrix transformation that I''ll need to do. It''s probably something simple so I think I will revisit it in the morning with a clear head...

Share this post

Link to post
Share on other sites
Lets look at this in pieces. You have a ray, say in direction (rx, ry, rz). In effect, you would like to rotate your z axis so that it points toward the ray. That is simple enough. Here''s a partial rotation matrix that will do this:

[? ? rx]
R = |? ? ry|
[? ? rz]

You can test that this does properly transform your z axis (or the effect''s axis) by applying R to the z axis vector:

Effect_axis = R * z_axis_vector


[? ? rx] [0]
Effect_z_axis_rotated = |? ? ry| * |0|
[? ? rz] [1]

which yields:

Effect_z_axis_rotated = |ry|

which is what you wanted. So the R matrix is correct as we''ve written it so far.

The problem is that you really need to transform the complete coordinate system of the effect, e.g., the effect''s x and y axes not just z axis. So you need to fill in the rest of that rotation matrix. I won''t try to advise you on how to determine the rotated x and y axes, except that you need to create some rules. You could for example find a rule that says the effect''s x axis always points toward some object. A constraint is that it is perpendicular to the effect''s z axis. Then the effect''s y axis is found by y = z cross x.

I will tell you that once you determine that the effect should act along (rx,ry,rz) and also that its x axis should be (exx, exy, exz) and y axis should be (eyx, eyy, eyz), the complete rotation matrix is:

[exx eyx rx]
R = |exy eyy ry|
[exz eyz rz]

Hope that helps.

Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.

Share this post

Link to post
Share on other sites
How does this look?

D3DXVECTOR3 vDir = g_pCamera->GetDir(); // Vector to rotate to

D3DXVECTOR3 vCurrDir = D3DXVECTOR3(0.0f, 0.0f, 1.0f); // Current direction

D3DXVec3Cross( &vRotAxis, &vCurrDir, &vDir ); // Find the rotation axis

float fAngle = D3DXVec3Dot( &vCurrDir, &vDir ); // Angle of rotation

D3DXQUATERNION quat = D3DXQUATERNION( vRotAxis.x, vRotAxis.y, vRotAxis.z, fAngle ); // Rotation quaternion

D3DXMatrixRotationQuaternion( &m_rotMat, &quat ); // Rotation matrix

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!