#### Archived

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

# Simple rotation prolem

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

## Recommended Posts

Hi all, I have a problem rotating an object. I am using quaternions to do it. For the most part it works well but when the angle goes from 359 to 1 or from 1 to 359 the rotation goes the long way around. Does anyone know how to prevent this? I''ve supplied the code below, note although I am working in degree converting to radians shouldn''t have any effect since principally they are the same. Cheers DRb2k2
VOID CMediumObjectDetails::CalculateMeshRotation(D3DXVECTOR3 CameraPosition)
{
CameraPosition.z = CameraPosition.z - this->m_vCurrentPosition.z;
CameraPosition.x = CameraPosition.x - this->m_vCurrentPosition.x;
D3DXVECTOR3 TempMeshPos = this->m_vCurrentPosition;
TempMeshPos.z = 0.0f;
TempMeshPos.x = 0.0f; // = 0

// get the vector of a position on the far side of mesh

D3DXVECTOR3 Line;
D3DXVec3Subtract(&Line, &TempMeshPos, &CameraPosition);
Line.y = 0;
D3DXVec3Normalize(&Line,&Line);
D3DXVECTOR3 LookingAt(0,0,-1);
D3DXVec3Normalize(&LookingAt, &LookingAt);

//check to see which direction to rotate in

D3DXVECTOR3 Cross;
float dotproduct;
dotproduct = D3DXVec3Dot(&Line, &LookingAt);
D3DXVec3Cross(&Cross,&Line,&LookingAt);

//calculate angle in degrees

dotproduct = (float)acos(dotproduct);
dotproduct = D3DXToDegree(dotproduct);
//convert to 360 degrees

if(Cross.y >=0.0f)
this->m_vRequiredRotation.y = 360.0f-dotproduct;
else
this->m_vRequiredRotation.y =  dotproduct;

// now see if any rotation needs to be done

if(this->m_vRequiredRotation != this->m_vCurrentRotation)
{
// now calculate the quaternion rotation

D3DXQUATERNION Current,Required;
Current.x = this->m_vCurrentRotation.x;
Current.y = this->m_vCurrentRotation.y;
Current.z = this->m_vCurrentRotation.z;
Required.x = -this->m_vRequiredRotation.x;
Required.y = -this->m_vRequiredRotation.y;
Required.z = -this->m_vRequiredRotation.z;

D3DXQUATERNION Result;
D3DXQuaternionSlerp(&Result, &Current, &Required, 0.5f);
this->m_vCurrentRotation.x = Result.x;
this->m_vCurrentRotation.y = Result.y;
this->m_vCurrentRotation.z = Result.z;

}

}


• ### Forum Statistics

• Total Topics
628714
• Total Posts
2984357

• 23
• 11
• 10
• 13
• 14