Jump to content
  • Advertisement


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


Simple rotation prolem

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

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

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

	//check to see which direction to rotate in

	float dotproduct;
	dotproduct = D3DXVec3Dot(&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;
		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;

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



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.

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!