Binding Animations to Keys

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
monkeycracks
Posts: 1029
Joined: Thu Apr 06, 2006 12:45 am
Location: Tennesee, USA
Contact:

Binding Animations to Keys

Post by monkeycracks »

How would you set an animation to play when you pressed a certain key? Such as pressing E the mesh would play the run animation. While standing still it would play the stand animation.

I know I have to set this up in my event receiver, but if I have it set to play the EMAT_STAND animation and then in the receiver have it set up so that when you press E, it runs EMAT_RUN...it crashes the program.

Can someone post a code snippet or in depth-advice and NOT flame me about searching because I've already tried that (I looked through 7 out of about 30 pages of irrelevance).

Thanks in advance.
gfxstyler
Posts: 222
Joined: Tue Apr 18, 2006 11:47 pm

Post by gfxstyler »

could you post your code so we can guess where its crashing :) ?
monkeycracks
Posts: 1029
Joined: Thu Apr 06, 2006 12:45 am
Location: Tennesee, USA
Contact:

Post by monkeycracks »

Don't have it anymore, removed it so I could work on other things while I was waiting. Here's what I think it was.

Code: Select all

	        IAnimatedMeshSceneNode* pcam=sm->addAnimatedMeshSceneNode(sm->getMesh("media/mdls/sydney.md2"));
            pcam->setMaterialFlag(EMF_LIGHTING, false); 
            pcam->setScale(vector3df(2,2,2)); 
            pcam->setMD2Animation(EMAT_STAND);
            pcam->setAnimationSpeed(80);
		    pcam->setPosition(vector3df(200,600,200));
		    pcam->setRotation(vector3df(0,270.0f,0));
		    pcam->setMaterialTexture(0, driver->getTexture("media/txts/sydney.bmp"));
and in the event receiver

Code: Select all

	if (event.EventType == EET_KEY_INPUT_EVENT &&
		event.KeyInput.Key == KEY_E_E&&
		event.KeyInput.PressedDown == false)
	{
pcam->setMD2Animation(EMAT_RUN);
pcam->setAnimationSpeed(80);
	}
else 
It went something like that.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Yes, but the important thing is how you pass the pcam to the event receiver. It's highly likely that you are using a null pointer in the event receiver.
monkeycracks
Posts: 1029
Joined: Thu Apr 06, 2006 12:45 am
Location: Tennesee, USA
Contact:

Post by monkeycracks »

I didn't understand what you meant
Could you clarify?
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Your IAMSN *pcam is local to the main method or wherever you defined it (the first code fragment). Although the nme in the event receiver is the same it does not use the first mentioned pointer, but maybe a global one which is initially 0 or some other wrong value. You have to set the pointer to the correct value.
monkeycracks
Posts: 1029
Joined: Thu Apr 06, 2006 12:45 am
Location: Tennesee, USA
Contact:

Post by monkeycracks »

Ok so here's what I did to try to solve that.

Code: Select all

		if (event.EventType == EET_KEY_INPUT_EVENT &&
		    event.KeyInput.Key == KEY_KEY_E)
		    {
            IVideoDriver* driver = device->getVideoDriver();
            ISceneManager* sm = device->getSceneManager();
	        IAnimatedMeshSceneNode* pcam=sm->addAnimatedMeshSceneNode(sm->getMesh("media/mdls/sydney.md2"));
	        pcam->setMD2Animation(EMAT_RUN);
         }
else

That was in my event receiver.

Code: Select all

	        IAnimatedMeshSceneNode* pcam=sm->addAnimatedMeshSceneNode(sm->getMesh("media/mdls/sydney.md2"));
            pcam->setMaterialFlag(EMF_LIGHTING, false); 
            pcam->setScale(vector3df(2,2,2)); 
            pcam->setMD2Animation(EMAT_STAND);
            pcam->setAnimationSpeed(80);
		    pcam->setPosition(vector3df(200,600,200));
		    pcam->setRotation(vector3df(0,270.0f,0));
		    pcam->setMaterialTexture(0, driver->getTexture("media/txts/sydney.bmp"));
And that was my original drawing of Sydney, In the event receiver I restated that as you said and it drew a new sydney model and played EMAT_RUN on her at 0,0,0 (I think)....This is very confusing to me and I can't seem to figure it out. Though I'll tinker with it some more while I wait on a reply :D
monkeycracks
Posts: 1029
Joined: Thu Apr 06, 2006 12:45 am
Location: Tennesee, USA
Contact:

Post by monkeycracks »

Ok, I've come up totally clueless.
Can someone PLEASE help me, I've never been able to figure this out.
zeno60
Posts: 342
Joined: Sun May 21, 2006 2:48 am
Location: NC, USA
Contact:

Post by zeno60 »

This is a basic C++ problem, your pcam is going out of scope in the onevent function. I would advise you take a look at how to set up classes in C++ then have all your class members be the scene nodes etc. that you need for your 3d scene.

semi-pseudo code:

Code: Select all

class mygame : public IEventReciever
{
     public:
     mygame();
     ~mygame();
     virtual bool OnEvent(SEvent event)
     void run();

     private:
     IrrlichtDevice device;
     IAnimatedMeshSceneNode* pcam;

}

void mygame::run()
{
     device = createdevice();     
     pcam = addmeshfunction("mesh.md2");
     //irrlicht while loop and load other stuff etc.

}

bool mygame::OnEvent(SEvent event)
{
     if(event is keyinput && keyinput is key_e && key isn't pressed down)
     {
          pcam->setMD2Animation(EMAT_RUN);
     }

     return false;
}
Then in main you basically create your game class:

mygame game;

And call its run function:

game.run();
monkeycracks
Posts: 1029
Joined: Thu Apr 06, 2006 12:45 am
Location: Tennesee, USA
Contact:

Post by monkeycracks »

That's how I have it set up...

I was skimming the demo code and I figured HEY, and I set it up like that. (However I didn't just copy-paste the code, I actually typed it out and the such. I have a pointer to it in my class, but I think the problem is when I'm creating the mesh. I've not got a clue how to fix it though.
monkeycracks
Posts: 1029
Joined: Thu Apr 06, 2006 12:45 am
Location: Tennesee, USA
Contact:

Post by monkeycracks »

This is the second post I've made on this, I can't find anything via search, can someone at least post a link to an open source game with moving meshes? I'm really frustrated with this because I've tried everything.
monkeycracks
Posts: 1029
Joined: Thu Apr 06, 2006 12:45 am
Location: Tennesee, USA
Contact:

Post by monkeycracks »

Can someone PLEASE help me on this, I'm about to go crazy trying to solve this.
dhenton9000
Posts: 395
Joined: Fri Apr 08, 2005 8:46 pm

Post by dhenton9000 »

I have an animation viewer that allows you to read a custom file I have that stores animation names for a .x file.

http://s-fonline.com/webhosting/dhenton9000/models.php sample models, and source for the viewer, which would have the code you might want to look at.
monkeycracks
Posts: 1029
Joined: Thu Apr 06, 2006 12:45 am
Location: Tennesee, USA
Contact:

Post by monkeycracks »

Thanks for those, but I've already decided to just switch to a FPS camera due to not understanding how to bind animations.
Post Reply