Accessing and operating bones
From MaratisWiki
Revision as of 11:06, 5 February 2014 by Dahnielson (Talk | contribs)
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()); } } } }