Accessing and operating bones
(Difference between revisions)
(Created page with "Here's how you access and operate the '''bones''' of a ''mesh'' (MMesh). In this example I get the position of the bones, I set their position (both relative to the mesh ...") |
|||
Line 1: | Line 1: | ||
− | Here's how you access and operate the | + | Here's how you access and operate the [[MOBone|bones]] of a [[MMesh|mesh]]. |
− | In this example I get the position of the bones, I set their position (both relative to the mesh and in world coordinates), and I rotate the bones. | + | 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. | + | 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 [[MArmature|armature]]. |
<pre> | <pre> | ||
MEngine * engine = MEngine::getInstance(); // get the engine instance | MEngine * engine = MEngine::getInstance(); // get the engine instance |
Revision as of 16:08, 22 August 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); //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(); } } }