change model animation

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.
VeneX
Posts: 228
Joined: Sun Nov 30, 2003 3:32 pm
Location: The Netherlands
Contact:

change model animation

Post by VeneX »

I edited the hello world tutorial. I hoped, when you use the W button, sidney began to run, unfortunatly this doesn't work.

include poop enz...

Code: Select all

IAnimatedMeshSceneNode* node = 0;

class MyEventReceiver : public IEventReceiver 
{ 
   public: 

      virtual bool OnEvent(SEvent event) { 
         if (event.EventType == EET_KEY_INPUT_EVENT) { 
            switch(event.KeyInput.Key) { 
               case KEY_KEY_W:
                  if (event.KeyInput.PressedDown) { 
                     node->setMD2Animation(irr::scene::EMAT_RUN); 
                  } else { 
                     node->setMD2Animation(irr::scene::EMAT_STAND); 
                  } 
                  break; 
               return true; 
            } 
         } 
         return false; 
      } 
};

int main() 
{ 
   MyEventReceiver receiver;
the rest of code

What's wrong with this? Can someone help me,

Thanks in advance
Visit my website @ www.venex.be
Plethora project will be added to the site
AMD AthlonXP 2600+, 512MB DDR, Radeon M10 (mobile 9600) PRO 64MB, WinXP
Boogle
Posts: 162
Joined: Fri Nov 21, 2003 3:16 pm
Location: Toronto, Canada

Post by Boogle »

Looks fine to me.. what doesn't work about it? Does it not compile, or does W just not make Sydney run? Does the up arrow still make her run?
keless
Posts: 805
Joined: Mon Dec 15, 2003 10:37 pm
Location: Los Angeles, California, USA

Post by keless »

I'll tell u exactly whats wrong with it:

if you're not holding down the W key, its being set to 'run' then immediately back to 'stand'!

with your currently posted code, try holding down W for a while, and see if it runs while you do.
a screen cap is worth 0x100000 DWORDS
VeneX
Posts: 228
Joined: Sun Nov 30, 2003 3:32 pm
Location: The Netherlands
Contact:

Post by VeneX »

When I push the W button, nothig happens. The entire code is:
I thought the code was good too...

Code: Select all

#include <irrlicht.h> 
using namespace irr; 
using namespace core; 
using namespace scene; 
using namespace video; 
using namespace io; 
using namespace gui; 
#pragma comment(lib, "Irrlicht.lib") 

IAnimatedMeshSceneNode* node = 0;

class MyEventReceiver : public IEventReceiver 
{ 
   public: 

      virtual bool OnEvent(SEvent event) { 
         if (event.EventType == EET_KEY_INPUT_EVENT) { 
            switch(event.KeyInput.Key) { 
               case KEY_KEY_W:
                  if (event.KeyInput.PressedDown) { 
                     node->setMD2Animation(irr::scene::EMAT_RUN); 
                  } else { 
                     node->setMD2Animation(irr::scene::EMAT_STAND); 
                  } 
                  break; 
               return true; 
            } 
         } 
         return false; 
      } 
};

int main() 
{ 
   MyEventReceiver receiver;

   IrrlichtDevice *device = 
   createDevice(DT_SOFTWARE, dimension2d<s32>(512, 384), 16, false, false, 0); 

   device->setWindowCaption(L"Hello World! - Irrlicht Engine Demo"); 

   IVideoDriver* driver = device->getVideoDriver(); 
   ISceneManager* smgr = device->getSceneManager(); 
   IGUIEnvironment* guienv = device->getGUIEnvironment(); 

   guienv->addStaticText(L"Hello World! This is the Irrlicht Software engine!", 
      true, rect<int>(10,10,200,30)); 

   IAnimatedMesh* mesh = smgr->getMesh("../../media/sydney.md2"); 
   IAnimatedMeshSceneNode* node = smgr->addAnimatedMeshSceneNode( mesh ); 

   if (node) 
   { 
      node->setMaterialFlag(EMF_LIGHTING, false); 
      node->setFrameLoop(0, 310);    
      node->setMaterialTexture( 0, driver->getTexture("../../media/sydney.bmp") ); 
   } 

   smgr->addCameraSceneNode(0, vector3df(0,10,-40), vector3df(0,0,0)); 

   while(device->run()) 
   { 
      driver->beginScene(true, true, SColor(0,100,100,100)); 

      smgr->drawAll(); 
      guienv->drawAll(); 

      driver->endScene(); 
   } 

   device->drop(); 

   return 0; 
}
Visit my website @ www.venex.be
Plethora project will be added to the site
AMD AthlonXP 2600+, 512MB DDR, Radeon M10 (mobile 9600) PRO 64MB, WinXP
rt
Posts: 150
Joined: Sun Nov 30, 2003 6:54 am
Location: canada
Contact:

Post by rt »

there are two problems..

first, you need to fill in the last parameter of createDevice() to point to your event reciever.

the second problem is scope.

you define a "IAnimatedMeshSceneNode* node = 0;" with global scope at the top of your file, and this is the node that the event reciever is using.

however, in the scope of the main() function you then define "IAnimatedMeshSceneNode* node = smgr->addAnimatedMeshSceneNode( mesh );" which although it has the same name as the global 'node', it is a new variable, and not the one that the event reciever is referencing.

change this

Code: Select all

IAnimatedMeshSceneNode* node = smgr->addAnimatedMeshSceneNode( mesh ); 
to this

Code: Select all

node = smgr->addAnimatedMeshSceneNode( mesh ); 
it's good practice to prefix global variables with g_ so that you can quickly tell when you look at the code weeks later :lol:

hope this helps!
keless
Posts: 805
Joined: Mon Dec 15, 2003 10:37 pm
Location: Los Angeles, California, USA

Post by keless »

and (IMHO) its even better to only have one global, which is your main class, and store all other 'globals' inside of that, while providing access to them for your children. ack, that was poorly worded.

'hide all your globals in one class, so you only have one global'

this will force good OOP coding when creating new objects that would have been global.

Also, you definately need to do what RT said, esp with registering the event reciever. Once you do that however, you will see the problem I pointed out-- where she only runs if you press-and-hold the 'w' button.
a screen cap is worth 0x100000 DWORDS
VeneX
Posts: 228
Joined: Sun Nov 30, 2003 3:32 pm
Location: The Netherlands
Contact:

Post by VeneX »

All right my code is now:

Code: Select all

#include <irrlicht.h> 
using namespace irr; 
using namespace core; 
using namespace scene; 
using namespace video; 
using namespace io; 
using namespace gui; 
#pragma comment(lib, "Irrlicht.lib") 

IAnimatedMeshSceneNode* node = 0;

int main() 
{ 
   class MyEventReceiver : public IEventReceiver 
   { 
   public: 

      virtual bool OnEvent(SEvent event) { 
         if (event.EventType == EET_KEY_INPUT_EVENT) { 
            switch(event.KeyInput.Key) { 
               case KEY_KEY_W:
                  if (event.KeyInput.PressedDown) { 
                     node->setMD2Animation(irr::scene::EMAT_RUN); 
                  } else { 
                     node->setMD2Animation(irr::scene::EMAT_STAND); 
                  } 
                  break; 
               return true; 
            } 
         } 
         return false; 
      } 
   };

   MyEventReceiver receiver;

   IrrlichtDevice *device = 
   createDevice(DT_SOFTWARE, dimension2d<s32>(512, 384), 16, false, false, 0); 

   device->setWindowCaption(L"Hello World! - Irrlicht Engine Demo"); 

   IVideoDriver* driver = device->getVideoDriver(); 
   ISceneManager* smgr = device->getSceneManager(); 
   IGUIEnvironment* guienv = device->getGUIEnvironment(); 

   guienv->addStaticText(L"Hello World! This is the Irrlicht Software engine!", 
      true, rect<int>(10,10,200,30)); 

   IAnimatedMesh* mesh = smgr->getMesh("../../media/sydney.md2"); 
   node = smgr->addAnimatedMeshSceneNode( mesh ); 

   if (node) 
   { 
      node->setMaterialFlag(EMF_LIGHTING, false); 
      node->setFrameLoop(0, 310);    
      node->setMaterialTexture( 0, driver->getTexture("../../media/sydney.bmp") ); 
   } 

   smgr->addCameraSceneNode(0, vector3df(0,10,-40), vector3df(0,0,0)); 

   while(device->run()) 
   { 
      driver->beginScene(true, true, SColor(0,100,100,100)); 

      smgr->drawAll(); 
      guienv->drawAll(); 

      driver->endScene(); 
   } 

   device->drop(); 

   return 0; 
}
Sorry, but I don't now what you mean with create device
Visit my website @ www.venex.be
Plethora project will be added to the site
AMD AthlonXP 2600+, 512MB DDR, Radeon M10 (mobile 9600) PRO 64MB, WinXP
keless
Posts: 805
Joined: Mon Dec 15, 2003 10:37 pm
Location: Los Angeles, California, USA

Post by keless »

IrrlichtDevice *device =
createDevice(DT_SOFTWARE, dimension2d<s32>(512, 384), 16, false, false, 0);
the last parameter of the function createDevice() is supposed to be a pointer to your event handler.
a screen cap is worth 0x100000 DWORDS
Guest

Post by Guest »

:oops: sorry I don't understand, I am not using C++ for a long time
Boogle
Posts: 162
Joined: Fri Nov 21, 2003 3:16 pm
Location: Toronto, Canada

Post by Boogle »

Change

Code: Select all

IrrlichtDevice *device = 
createDevice(DT_SOFTWARE, dimension2d<s32>(512, 384), 16, false, false, 0); 
to

Code: Select all

IrrlichtDevice *device = 
createDevice(DT_SOFTWARE, dimension2d<s32>(512, 384), 16, false, false, &receiver); 
VeneX
Posts: 228
Joined: Sun Nov 30, 2003 3:32 pm
Location: The Netherlands
Contact:

Post by VeneX »

really, really thanks!

One question left, when I push and hold the W key, the anim gets stuck: she places one and a half step and gets stuck.
Visit my website @ www.venex.be
Plethora project will be added to the site
AMD AthlonXP 2600+, 512MB DDR, Radeon M10 (mobile 9600) PRO 64MB, WinXP
Byron

The model is not stuck

Post by Byron »

I think that the problem is that while you are holding down the 'W', the model keeps starting to run (the animation keeps starting over making it look like it is stuck), maybe try keeping track of the model animation, and if the model is running don't make her start running again

Byron
keless
Posts: 805
Joined: Mon Dec 15, 2003 10:37 pm
Location: Los Angeles, California, USA

Post by keless »

what byron said. in code:

Code: Select all

               case KEY_KEY_W: 
                  if (event.KeyInput.PressedDown && model_not_running) { 
                     node->setMD2Animation(irr::scene::EMAT_RUN); 
                  } else { 
                     node->setMD2Animation(irr::scene::EMAT_STAND); 
                  } 
keep track of whether or not the model is running, and add it to your key check.
a screen cap is worth 0x100000 DWORDS
VeneX
Posts: 228
Joined: Sun Nov 30, 2003 3:32 pm
Location: The Netherlands
Contact:

Post by VeneX »

what's wrong with this code?

Code: Select all

class MyEventReceiver : public IEventReceiver 
   { 
   public: 

      virtual bool OnEvent(SEvent event) { 
		 bool model_not_running;
		 model_not_running = true;
		 if (event.EventType == EET_KEY_INPUT_EVENT) { 
            switch(event.KeyInput.Key) { 
               case KEY_KEY_W:
                  if (event.KeyInput.PressedDown && model_not_running) { 
                     node->setMD2Animation(irr::scene::EMAT_RUN); 
					 model_not_running = false;
                  } else { 
                     node->setMD2Animation(irr::scene::EMAT_STAND);
					 model_not_running = true;
                  } 
                  break; 
               return true; 
            } 
         } 
         return false; 
      } 
   };
This is the way to all a bool, isn't it?
Visit my website @ www.venex.be
Plethora project will be added to the site
AMD AthlonXP 2600+, 512MB DDR, Radeon M10 (mobile 9600) PRO 64MB, WinXP
keless
Posts: 805
Joined: Mon Dec 15, 2003 10:37 pm
Location: Los Angeles, California, USA

Post by keless »

you cant store "model_not_running" inside of the OnEvent function (or if you do, it has to be static).

right now you're saying:

Code: Select all

      virtual bool OnEvent(SEvent event) { 
       bool model_not_running; 
       model_not_running = true;
On every event, the model IS NOT running. so, whether they are pushing or releasing the 'w' key, you're starting the event by saying the model is not running. if you really want to keep model_not_running inside of that function, change those first two lines to this one line:

Code: Select all

static bool model_not_running = true;
this will make sure that the variable 'model_not_running' retains its value across calls to OnEvent, and that it will only be initialized once.
a screen cap is worth 0x100000 DWORDS
Post Reply