Changing MD2 animation during runtime

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Ainsoph
Posts: 2
Joined: Mon Nov 08, 2004 11:55 am

Changing MD2 animation during runtime

Post by Ainsoph »

HI.

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);
	}
}
that sets the NPC to walk (true) or not, based on the keys pressed.

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);
	}
}
My Game object instatiates the NPC this way (gd is a GameData object that stores the parameters I need to create the game):

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++)

        // ...
}
Does anybody have any idea?
Thanks in advance
kaeles_not_logged

Post by kaeles_not_logged »

i think it might be a problem where you declared canwalk = walk, seeing as how its outside the game loop, it might not be updating......
Guest

Post by Guest »

Sorry, I forgot to mention. canWalk is a boolean variable of the NPC class; it is not a global variable
XargoL
Posts: 22
Joined: Sun Aug 01, 2004 7:55 pm

Re: Changing MD2 animation during runtime

Post by XargoL »

Ainsoph wrote: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);
	}
}
Won't this restart the animation every frame?
Ainsoph
Posts: 2
Joined: Mon Nov 08, 2004 11:55 am

Post by Ainsoph »

Xargol, I think you've gotta the point. Yeah, that solved the problem. Thank you
Post Reply