Accessing and operating bones

(Difference between revisions)
Jump to: navigation, search
Line 49: Line 49:
 
//rotate the bone
 
//rotate the bone
 
bone->addAxisAngleRotation(MVector3(1, 0, 0), 10);
 
bone->addAxisAngleRotation(MVector3(1, 0, 0), 10);
 +
 +
//copy the rotation from another object (useful to create a ragdoll)
 +
MMatrix myMatrix = entity->getMatrix()->getInverse() * (*object->getMatrix());
 +
MObject3d * parentBone = bone->getParent();
 +
if(parentBone)
 +
{ //calculate object matrix in parentBone's space coordinate
 +
myMatrix = parentBone->getMatrix()->getInverse() * myMatrix;
 +
}
 +
//get rotation coords from myMatrix
 +
bone->setEulerRotation(myMatrix.getEulerAngles());
 
}
 
}
//update
 
//if you just get/set the position you won't need this; otherwise,
 
// and if there's no animations, you have to manually update the armature:
 
armature->processBonesLinking();
 
armature->updateBonesSkinMatrix();
 
 
}
 
}
 
}
 
}
 
</pre>
 
</pre>

Revision as of 00:48, 7 October 2013

Here's how you access and operate the bones of a mesh.

In this example I get the position of the bones, I set their position (both relative to the mesh/parent and in world coordinates), and I rotate the bones.

The code as it is has no use of course, but you can use it as a starting point to make meaningful modifications to the mesh armature.

MEngine * engine = MEngine::getInstance(); // get the engine instance
MLevel * level = engine->getLevel(); // get the current level
MScene * scene = level->getSceneByName("MyScene");
MOEntity * entity = scene->getEntityByName("MyEntity");

MOBone * bone;
MVector3 pos;

if(entity)
{	MMesh * mesh = entity->getMesh();
	if(mesh)
	{
		MArmature * armature = mesh->getArmature();
		if(armature)
		{
			unsigned int bones_number = armature->getBonesNumber();
			for(unsigned int i=0; i < bones_number; i++)
			{	bone = armature->getBone(i);
				if(you want to GET the position of the bone)
				{	//get position
					MMatrix4x4 boneWorldMatrix = (*entity->getMatrix()) * (*bone->getMatrix());
					pos = boneWorldMatrix.getTranslationPart();
				}
				else//, if you want to SET the position of the bone
				{
					if(you want to set the relative position)
					{
						pos = MVector3(0,0,0); //relative coordinates P(0,0,0)
						bone->setPosition(pos);
					}
					else//, if you want to set the world position
					{
						pos = MVector3(0,0,0); //world coordinates P(0,0,0)
						MObject3d * parentBone = bone->getParent();
						MVector3 P_entity = entity->getInversePosition(pos); //entity space
						if(parentBone)
						{	MVector3 P_parent = parentBone->getInversePosition(P_entity); //bone parent space
							bone->setPosition(P_parent);
						}
						else bone->setPosition(P_entity); //no parent so the bone is in entity space
					}
				}
				//rotate the bone
				bone->addAxisAngleRotation(MVector3(1, 0, 0), 10);

				//copy the rotation from another object (useful to create a ragdoll)
				MMatrix myMatrix = entity->getMatrix()->getInverse() * (*object->getMatrix());
				MObject3d * parentBone = bone->getParent();
				if(parentBone)
				{	//calculate object matrix in parentBone's space coordinate
					myMatrix = parentBone->getMatrix()->getInverse() * myMatrix;
				}
				//get rotation coords from myMatrix
				bone->setEulerRotation(myMatrix.getEulerAngles());
			}
		}
	}
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox