Finding the rotation from one vector to another
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]
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...
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:
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
or,
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:
Hope that helps.
Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.
[? ? 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
or,
[? ? rx] [0]Effect_z_axis_rotated = |? ? ry| * |0| [? ? rz] [1]which yields: [rx]Effect_z_axis_rotated = |ry| [rz]
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.
Yes, of course, quaternions.
Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.
Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.
How does this look?
D3DXVECTOR3 vDir = g_pCamera->GetDir(); // Vector to rotate to D3DXVECTOR3 vCurrDir = D3DXVECTOR3(0.0f, 0.0f, 1.0f); // Current direction D3DXVECTOR3 vRotAxis; 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
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement