Beispiel #1
0
// create constraint
void MBulletContext::createConstraint(unsigned int * constraintId, unsigned int parentObjectId, unsigned int objectId, const MVector3 & pivot, bool disableParentCollision)
{
	btRigidBody * bA = btRigidBody::upcast(m_collisionObjects[parentObjectId]);
	btRigidBody * bB = btRigidBody::upcast(m_collisionObjects[objectId]);
		
	if(bA && bB)
	{
		bA->setActivationState(DISABLE_DEACTIVATION);
		bB->setActivationState(DISABLE_DEACTIVATION);
		
		*constraintId = m_constraints.size();
		
		MVector3 position, euler;
		MQuaternion rotation;
		MMatrix4x4 matrix, matrix1, matrix2, rotMatrix;
		
		rotMatrix.setRotationEuler(90, 0, 0);
		
		getObjectTransform(parentObjectId, &position, &rotation);
		matrix1.setRotationAxis(rotation.getAngle(), rotation.getAxis());
		matrix1.setTranslationPart(position);
		
		getObjectTransform(objectId, &position, &rotation);
		matrix2.setRotationAxis(rotation.getAngle(), rotation.getAxis());
		matrix2.setTranslationPart(position + matrix2.getRotatedVector3(pivot));
		
		matrix = (matrix1.getInverse() * matrix2) * rotMatrix;
		
		euler = matrix.getEulerAngles();
		position = matrix.getTranslationPart();
		rotation.setFromAngles(euler.x, euler.y, euler.z);
		
		btTransform frameInA, frameInB;
		frameInA = btTransform::getIdentity();
		frameInA.setOrigin(btVector3(position.x, position.y, position.z));
		frameInA.setRotation(btQuaternion(rotation.values[0], rotation.values[1], rotation.values[2], rotation.values[3]));
		
		rotation.setFromAngles(90, 0, 0);
		frameInB = btTransform::getIdentity();
		frameInB.setOrigin(btVector3(pivot.x, pivot.y, pivot.z));
		frameInB.setRotation(btQuaternion(rotation.values[0], rotation.values[1], rotation.values[2], rotation.values[3]));
			
		btGeneric6DofSpringConstraint * constraint = new btGeneric6DofSpringConstraint(
			*bA, *bB, frameInA, frameInB, true
		);
		
		m_constraints.push_back(constraint);
		m_dynamicsWorld->addConstraint(constraint, disableParentCollision);
	}
}
Beispiel #2
0
bool xmlArmatureAnimLoad(const char * filename, void * data)
{
	TiXmlDocument doc(filename);
	if(! doc.LoadFile())
		return false;

	TiXmlHandle hDoc(&doc);
	TiXmlElement * pRootNode;
	TiXmlHandle hRoot(0);

	// Maratis
	pRootNode = hDoc.FirstChildElement().Element();
	if(! pRootNode)
		return false;

	if(strcmp(pRootNode->Value(), "Maratis") != 0)
		return false;

	hRoot = TiXmlHandle(pRootNode);

	// BonesAnim
	TiXmlElement * armatureAnimNode = pRootNode->FirstChildElement("ArmatureAnim");
	if(! armatureAnimNode)
		return false;

	unsigned int bonesAnimNumber = 0;
	armatureAnimNode->QueryUIntAttribute("num", &bonesAnimNumber);
	if(bonesAnimNumber == 0)
		return false;

	// create armature anim
	MArmatureAnim * armatureAnim = (MArmatureAnim *)data;
	MObject3dAnim * objAnims = armatureAnim->allocBonesAnim(bonesAnimNumber);

	// Bone
	TiXmlElement * boneNode = armatureAnimNode->FirstChildElement("Bone");
	for(boneNode; boneNode; boneNode=boneNode->NextSiblingElement("Bone"))
	{
		// position
		TiXmlElement * positionNode = boneNode->FirstChildElement("position");
		if(positionNode)
		{
			unsigned int kSize = 0;
			positionNode->QueryUIntAttribute("num", &kSize);
			MKey * keys = objAnims->allocPositionKeys(kSize);
			readVector3Keys(positionNode, keys);
		}

		// rotation
		TiXmlElement * rotationNode = boneNode->FirstChildElement("rotation");
		if(rotationNode)
		{
			unsigned int kSize = 0;
			rotationNode->QueryUIntAttribute("num", &kSize);
			MKey * keys = objAnims->allocRotationKeys(kSize);

			// k
			TiXmlElement * kNode = rotationNode->FirstChildElement("k");
			for(kNode; kNode; kNode=kNode->NextSiblingElement("k"))
			{
				int t = 0;
				MVector3 euler;
				MQuaternion * rotation = keys->createQuaternionData();

				kNode->QueryIntAttribute("t", &t);
				kNode->QueryFloatAttribute("x", &euler.x);
				kNode->QueryFloatAttribute("y", &euler.y);
				kNode->QueryFloatAttribute("z", &euler.z);

				rotation->setFromAngles(euler.x, euler.y, euler.z);

				keys->setT(t);
				keys++;
			}
		}

		// scale
		TiXmlElement * scaleNode = boneNode->FirstChildElement("scale");
		if(scaleNode)
		{
			unsigned int kSize = 0;
			scaleNode->QueryUIntAttribute("num", &kSize);
			MKey * keys = objAnims->allocScaleKeys(kSize);
			readVector3Keys(scaleNode, keys);
		}

		objAnims++;
	}

	return true;
}