#### Archived

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

# Rotation from one vector to another -- repost

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

## Recommended Posts

I lost my original post but no one replied to it anyway. Anyway, I have an effect that gets created along the z axis and I want to find the rotation to the camera's direction. Here's what I have right now. m_vDir is the camera's direction.

// Rotate the cylinder off the z axis to face the direction of the ray

// Find the rotation from vCurrDir to m_vDir and store it for render time

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

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

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

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

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


I use the resulting m_rotMat in my render function to rotate the effect. My code seems correct but my effect is positioned all messed up. Any ideas? I have been trying to fix this for a few weeks now and still no luck! grrr [edited by - beoch on September 30, 2002 6:35:09 PM]

##### Share on other sites
Maybe it''s your solution: this is a function to find angle between 2 vector...
float GetVectorAngle(D3DVECTOR Vec1,D3DVECTOR Vec2)
{
return (float) acos(DotProduct(Vec1,Vec2)/(Magnitude(Vec1)*(Magnitude(Vec2))));
}

If vec1 and vec2 are normalized, you can omit the "/(Magnitude(Vec1)*(Magnitude(Vec2)"

Franck

##### Share on other sites
It''s still not quite right, but it''s a lot closer with that change. Thanks

• 40
• 12
• 10
• 10
• 9
• ### Forum Statistics

• Total Topics
631371
• Total Posts
2999606
×