Skip to main content
gamedev.net
🔒 Locked

problem animation

Started by thuong 0 replies 300+ views
Original Post
thuong
thuong
i have aniamtion follow:

	CMyAnimationSet *AnimSet = m_AnimationSets;

	// Look for matching animation set name if used
	if(AnimationSetName) {

		// Find matching animation set name
		while(AnimSet != NULL) {

			// Break when match found
			if(!stricmp(AnimSet->m_Name, AnimationSetName))
				break;

			// Go to next animation set object
			AnimSet = AnimSet->m_Next;
		}
	}

	// Return no set found
	if(AnimSet == NULL)
		return;

	// Bounds time to animation length
	if(Time > AnimSet->m_Length)
		Time = (Loop==TRUE)?Time%(AnimSet->m_Length+1):AnimSet->m_Length;

	// Go through each animation
	CMyAnimation *Anim = AnimSet->m_Animations;
	while(Anim) {

		// Only process if it's attached to a bone
		if(Anim->m_Bone) {
 
			// Reset transformation
			
			// Reset transformation
			D3DXMATRIX matAnimation;
			D3DXMatrixIdentity(&matAnimation);
			//D3DXMatrixIdentity(&Anim->m_Bone->TransformationMatrix);

			// Apply various matrices to transformation
			
			
			
			// Scaling
			if(Anim->m_NumScaleKeys && Anim->m_ScaleKeys) {

				// Loop for matching scale key
				DWORD Key1 = 0, Key2 = 0;
				//DWORD Key1 = -1, Key2 = -1;
				for(DWORD i=0;i<Anim->m_NumScaleKeys;i++) {
					if(Time >= Anim->m_ScaleKeys.m_Time)
					Key1 = i;
				}

				//if(Key1 < 0)
				//{
				//	Key1 = 0;
				//	Key2 = 0;
				//}
				//else
				{
					// Get 2nd key number
					Key2 = (Key1>=(Anim->m_NumScaleKeys-1))?Key1:Key1+1;
				}

				// Get difference in keys' times
				DWORD TimeDiff = Anim->m_ScaleKeys[Key2].m_Time-
					             Anim->m_ScaleKeys[Key1].m_Time;
				//if(!TimeDiff)
				//	TimeDiff = 1;
				float Scalar;
				if(TimeDiff == 0)
				{
					Scalar = 0;
				}
				else
				{
					// Calculate a scalar value to use
					Scalar = (float)(Time - Anim->m_ScaleKeys[Key1].m_Time) / (float)TimeDiff;
				}

				// Calculate interpolated scale values
				//D3DXVECTOR3 vecScale = Anim->m_ScaleKeys[Key2].m_vecKey - 
				//	                   Anim->m_ScaleKeys[Key1].m_vecKey;
				//vecScale *= Scalar;
				//vecScale += Anim->m_ScaleKeys[Key1].m_vecKey;

				D3DXVECTOR3 vecScale;
				D3DXVec3Lerp( &vecScale, &Anim->m_ScaleKeys[Key1].m_vecKey,
							&Anim->m_ScaleKeys[Key2].m_vecKey, Scalar );

				// Create scale matrix and combine with transformation
				D3DXMATRIX matScale;
				D3DXMatrixScaling(&matScale, vecScale.x, vecScale.y, vecScale.z);
				//Anim->m_Bone->TransformationMatrix *= matScale;
				matAnimation *= matScale;
			}
			
			// Rotation
			if(Anim->m_NumRotationKeys && Anim->m_RotationKeys) {

				// Loop for matching rotation key
				DWORD Key1 = 0, Key2 = 0;
				//DWORD Key1 = 0, Key2 = 0;
				for(DWORD i=0;i<Anim->m_NumRotationKeys;i++) {
					if(Time >= Anim->m_RotationKeys.m_Time)
						Key1 = i;
				}

				//if(Key1 < 0)
				//{
				//	Key1 = 0;
				//	Key2 = 0;
				//}
				//else
				{
					// Get 2nd key number
					Key2 = (Key1>=(Anim->m_NumRotationKeys-1))?Key1:Key1+1;
				}
				// Get difference in keys' times
				DWORD TimeDiff = Anim->m_RotationKeys[Key2].m_Time-
					             Anim->m_RotationKeys[Key1].m_Time;
				//if(!TimeDiff)
				//	TimeDiff = 1;

				float Scalar;

				if(TimeDiff == 0)
				{
					Scalar = 0;
				}
				else
				{
					// Calculate a scalar value to use
					Scalar = (float)(Time - Anim->m_RotationKeys[Key1].m_Time) / (float)TimeDiff;
				}

				
				// slerp rotation values
				D3DXQUATERNION quatRotation;
				D3DXQUATERNION q1,q2;
				q1.x = -(Anim->m_RotationKeys[Key1].m_quatKey.x);
				q1.y = -(Anim->m_RotationKeys[Key1].m_quatKey.y);
				q1.z = -(Anim->m_RotationKeys[Key1].m_quatKey.z);
				q1.w = Anim->m_RotationKeys[Key1].m_quatKey.w;
				
				q2.x = -(Anim->m_RotationKeys[Key2].m_quatKey.x);
				q2.y = -(Anim->m_RotationKeys[Key2].m_quatKey.y);
				q2.z = -(Anim->m_RotationKeys[Key2].m_quatKey.z);
				q2.w = Anim->m_RotationKeys[Key2].m_quatKey.w;

				D3DXQuaternionSlerp(&quatRotation,
					                &q1,
						            &q2,
							        Scalar);
				


				// Create rotation matrix and combine with transformation
				D3DXMATRIX matRotation;
				D3DXMatrixRotationQuaternion(&matRotation, &quatRotation);
				//Anim->m_Bone->TransformationMatrix *= matRotation;
				matAnimation *= matRotation;
			}

			// Translation
			if(Anim->m_NumTranslationKeys && Anim->m_TranslationKeys) {

				// Loop for matching translation key
				DWORD Key1 = 0, Key2 = 0;
				//DWORD Key1 = -1, Key2 = -1;
				for(DWORD i=0;i<Anim->m_NumTranslationKeys;i++) {
					if(Time >= Anim->m_TranslationKeys.m_Time)
						Key1 = i;
				}

				//if(Key1 < 0)
				//{
				//	Key1 = 0;
				//	Key2 = 0;
				//}
				//else
				{
					// Get 2nd key number
					Key2 = (Key1>=(Anim->m_NumTranslationKeys-1))?Key1:Key1+1;
				}

				// Get difference in keys' times
				DWORD TimeDiff = Anim->m_TranslationKeys[Key2].m_Time-
					             Anim->m_TranslationKeys[Key1].m_Time;
				//if(!TimeDiff)
				//	TimeDiff = 1;
				
				float Scalar;
				if(TimeDiff == 0)
				{
					Scalar = 0;
				}
				else
				{
					// Calculate a scalar value to use
					Scalar = (float)(Time - Anim->m_TranslationKeys[Key1].m_Time) / (float)TimeDiff;
				}

				// Calculate interpolated vector values
				//D3DXVECTOR3 vecPos = Anim->m_TranslationKeys[Key2].m_vecKey - 
				//	                 Anim->m_TranslationKeys[Key1].m_vecKey;
				//vecPos *= Scalar;
				//vecPos += Anim->m_TranslationKeys[Key1].m_vecKey;

				// apply the lerp function
				D3DXVECTOR3 vecPos;
				D3DXVec3Lerp( (D3DXVECTOR3*)&vecPos, &Anim->m_TranslationKeys[Key1].m_vecKey, 
							&Anim->m_TranslationKeys[Key2].m_vecKey, Scalar );

				// Create translation matrix and combine with transformation
				D3DXMATRIX matTranslation;
				D3DXMatrixTranslation(&matTranslation, vecPos.x, vecPos.y, vecPos.z);
				//Anim->m_Bone->TransformationMatrix *= matTranslation;
				matAnimation *= matTranslation;
			}
			
			// Matrix
			if(Anim->m_NumMatrixKeys && Anim->m_MatrixKeys) {
				// Loop for matching matrix key
				DWORD Key1 = 0, Key2 = 0;
				for(DWORD i=0;i<Anim->m_NumMatrixKeys;i++) {
					if(Time >= Anim->m_MatrixKeys.m_Time)
						Key1 = i;
				}

				// Get 2nd key number
				Key2 = (Key1>=(Anim->m_NumMatrixKeys-1))?Key1:Key1+1;

				// Get difference in keys' times
				DWORD TimeDiff = Anim->m_MatrixKeys[Key2].m_Time-
					             Anim->m_MatrixKeys[Key1].m_Time;
				if(!TimeDiff)
					TimeDiff = 1;

				// Calculate a scalar value to use
				float Scalar = (float)(Time - Anim->m_MatrixKeys[Key1].m_Time) / (float)TimeDiff;

				// Calculate interpolated matrix
				D3DXMATRIX matDiff = Anim->m_MatrixKeys[Key2].m_matKey - 
					                 Anim->m_MatrixKeys[Key1].m_matKey;
				matDiff *= Scalar;
				matDiff += Anim->m_MatrixKeys[Key1].m_matKey;

				// Combine with transformation
				//Anim->m_Bone->TransformationMatrix *= matDiff;
				matAnimation *= matDiff;
			}

			D3DXMATRIX matDiff = matAnimation - Anim->m_Bone->TransformationMatrix;

			// Add to transformation matrix
			Anim->m_Bone->TransformationMatrix += matDiff;

		}

		// go to next animation
		Anim = Anim->m_Next;
	}

it work no good can anybody help me for fixing? thanks alot [Edited by - Coder on March 19, 2005 10:31:28 PM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.