I instantiate an NPC object I wrote with IAnimatedMesh and IAnimatedSceneNode and set the initial MD2 animation. Everything works perfectly. But whe I change the MD2 animation of my NPC, based on the keys pressed by the player, it just freezes the animation on the last keyframe. When I change back to the animation set the first time, the MD2 animation runs correctly. Am I doing anything wrong, like, not calling a method that keeps running the animation?
Here is the code
IEventReceiver object calls this function:
Code: Select all
void NPC::walkingMode(bool walk) {
canWalk = walk;
if (!canWalk) {
node->setMD2Animation(irr::scene::EMAT_STAND);
weaponNode->setMD2Animation(irr::scene::EMAT_STAND);
}
}
My game loop funciton calls NPC::update everyframe before drawing to check if anything has changed in the NPC's state.
Code: Select all
void NPC::update() {
if (canWalk) {
node->setMD2Animation(irr::scene::EMAT_RUN);
weaponNode->setMD2Animation(irr::scene::EMAT_RUN);
core::matrix4 matrix = node->getRelativeTransformation();
core::vector3df forward(matrix.M[0], matrix.M[1], matrix.M[2]);
core::vector3df pos = node->getPosition();
pos += forward * 0.5;
node->setPosition(pos);
}
}
Code: Select all
Game::Game() {
// ...
scene::IAnimatedMesh* npcMesh;
scene::IAnimatedMesh* weaponMesh;
scene::IAnimatedMeshSceneNode* npcNode;
scene::IAnimatedMeshSceneNode* weaponNode;
core::vector3df weaponVec;
npcs = new NPC*[gd->npcNumber];
// To read the correct position in the GameData array
int index = 0;
for(int i = 0; i < gd->npcModel.size(); i++) {
// Load a mesh, create a scene node with it, placed the node in the Scene Graph.
npcMesh = sceneMgr->getMesh(gd->npcModel[i].c_str());
npcNode = sceneMgr->addAnimatedMeshSceneNode(npcMesh);
if (npcNode) {
npcNode->setPosition(core::vector3df(gd->npcCoord[index], gd->npcCoord[index + 1], gd->npcCoord[index + 2]));
npcNode->setRotation(core::vector3df(gd->npcRot[index], gd->npcRot[index + 1], gd->npcRot[index + 2]));
npcNode->setScale(core::vector3df(gd->npcScale[index], gd->npcScale[index + 1], gd->npcScale[index + 2]));
npcNode->setMaterialType(video::EMT_SOLID);
npcNode->setMaterialFlag(video::EMF_LIGHTING, false);
npcNode->setMaterialTexture(0, driver->getTexture(gd->npcTexture[i].c_str()));
npcNode->setMD2Animation(scene::EMAT_STAND);
}
// Load NPC's weapon
weaponMesh = sceneMgr->getMesh(gd->npcWeaponModel[i].c_str());
weaponNode = sceneMgr->addAnimatedMeshSceneNode(weaponMesh, npcNode, -1);
weaponNode->setMaterialTexture(0, driver->getTexture(gd->npcWeaponTexture[i].c_str()));
weaponNode->setMaterialType(video::EMT_SOLID);
weaponNode->setMaterialFlag(video::EMF_LIGHTING, false);
weaponNode->setMD2Animation(scene::EMAT_STAND);
index += 3;
// Instantiate a NPC object with this scene node and mesh.
npcs[i] = new NPC(npcMesh, npcNode, weaponNode);
} // end of for (int i = 0; i < gd->npcModel.size(); i++)
// ...
}
Thanks in advance