detect if key was released.

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
mcbart
Posts: 19
Joined: Thu Aug 07, 2008 11:36 am

detect if key was released.

Post by mcbart »

hi!
I have written program where user can move mesh and camera in difference ways. I have one problem, because when i press and hold key W mesh move forward and we can see animation of running. When i stop pressing and holding key W mesh stop moving, but we can still see animation if running. How to remove this bug??
Code of app:

Code: Select all

#include <irrlicht.h>
#include <stdio.h>




using namespace irr;


using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;

#pragma comment(linker, "/SUBSYSTEM:windows /ENTRY:mainCRTStartup")
#pragma comment(lib, "Irrlicht.lib")

bool key_W;
bool key_A;
bool key_D;
bool key_S;
bool key_Space;

 bool first= true;
 bool animation=false;
int speed=5;
 

class MyEventReceiver : public IEventReceiver
{
public:
	virtual bool OnEvent(SEvent event)
     {



 
            if ( event.EventType == irr::EET_KEY_INPUT_EVENT)
            {
    
    
   
if(event.KeyInput.Key== KEY_KEY_W )
		{
			key_W=event.KeyInput.PressedDown;
		}
	 
		 if(event.KeyInput.Key== KEY_KEY_A)
		 {
			 key_A=event.KeyInput.PressedDown;
		 }
		 if(event.KeyInput.Key==KEY_KEY_D)
		 {
             key_D=event.KeyInput.PressedDown;
             }
             if(event.KeyInput.Key==KEY_KEY_S)
             { 
             key_S=event.KeyInput.PressedDown;
             }
           
           }


	
return false;	
	} 
	

 };
int main()
{MyEventReceiver receiver;


	IrrlichtDevice *device =
		createDevice( video::EDT_OPENGL, dimension2d<s32>(640, 480), 16,
			false, false, false, &receiver);


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


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

   

	guienv->addStaticText(L"Hello World! This is the Irrlicht Software renderer!",
		rect<int>(10,10,260,22), true);
		
		
	device->getFileSystem()->addZipFileArchive      ("media/map-20kdm2.pk3");

	
	scene::IAnimatedMesh* q3levelmesh = smgr->getMesh("20kdm2.bsp");
	scene::ISceneNode* q3node = 0;
	
scene::IAnimatedMeshSceneNode* node = 0;///obiekt postaci gracza
	


 	
scene::IAnimatedMesh* mesh = smgr->getMesh("media/sydney.md2");
node = smgr->addAnimatedMeshSceneNode(mesh);
node->setPosition(core::vector3df(1900*2,255*2,3700*2));
node->setFrameLoop(1,1);
//pierwsza część detektora kolizji dla obiektu gracza
	
scene::ITerrainSceneNode* terrain = smgr->addTerrainSceneNode("media/terrain-heightmap.bmp");
terrain->setScale(core::vector3df(40, 4.4f, 40));terrain->setMaterialFlag(video::EMF_LIGHTING, false);terrain->setMaterialTexture(0, driver->getTexture("media/terrain-texture.jpg"));terrain->setMaterialTexture(1, driver->getTexture("media/detailmap3.jpg"));	
terrain->setMaterialType(video::EMT_DETAIL_MAP);
terrain->scaleTexture(1.0f, 20.0f);
// create triangle selector for the terrain	
scene::ITriangleSelector* selector =    smgr->createTerrainTriangleSelector(terrain, 0);
terrain->setTriangleSelector(selector);
selector->drop();
scene::ISceneNodeAnimator* anim = smgr->createCollisionResponseAnimator(selector, node, core::vector3df(60,100,60),core::vector3df(0,-1,0), core::vector3df(0,10,0));node->addAnimator(anim);anim->drop();

//kamera		
scene::ICameraSceneNode* camera ;
camera = smgr->addCameraSceneNode(0, vector3df(0,30,-40), vector3df(0,5,0)); 



	while(device->run())
	{   core::vector3df pos = node->getPosition(); 
         core::vector3df rot= node->getRotation(); 
  float diry = ((rot.Y+90)*3.14)/180;

if(key_W==true)
{  if (first==true)
		{animation= true;}
 pos.X += speed * cos((rot.Y) * 3.14 / 180); 
 pos.Z -= speed * sin((rot.Y) * 3.14 / 180); 

}else
   {animation=false;
                    first=true;
                   }
  
     
if (key_A==true)
{ rot.Y -= 1;}
if(key_D==true)
{  rot.Y+=1;}
if(key_S==true)
{  pos.X -= speed * cos((rot.Y) * 3.14 / 180); 
   pos.Z += speed * sin((rot.Y) * 3.14 / 180); 
   }
     

 //---------------------------------------------------------------------------------
 //-----------------------------------------------------------------------------------
     node->setRotation(rot); 
    
    double xf = (pos.X-sin(diry)*150); 
double yf =(pos.Z-cos(diry)*150); 
double zf =200; 


node->setPosition(pos); 


         camera->setTarget(pos); 

         pos.Y +=200.f; 
         pos.Z -= 150.f; 

         camera->setPosition(vector3df(xf,zf,yf)); 

 //--------------------------------------------------------------------
 //----------------------------------------------------------------------  
      if(animation==true)	
			{  
					node->setFrameLoop(320, 360);
						animation=false;
	first=false;
						
			}
			
			
   
         


          
		driver->beginScene(true, true, SColor(255,100,101,140));
     
		smgr->drawAll();
		guienv->drawAll();

		driver->endScene();
	}

	device->drop();

	return 0;
}




B@z
Posts: 876
Joined: Thu Jan 31, 2008 5:05 pm
Location: Hungary

Post by B@z »

for me, i'm using states for animation.
first declare cAnim and pAnim
set pAnim to -1, cAnim to 0 (stand)

and then, in your control part, set cAnim to stand.
and then, if w pressed then set cAnim to run (1)
and so on the other animations.

and when your control part is over,
do that:

Code: Select all

if (cAnim != pAnim)
{
  if (cAnim == 0) node->setFrameLoop(1, 1); // your stand anim 
  if (cAnim == 1) node->setFrameLoop(320, 360); // your walk anim
}
of course, you should use define, or enum to make it easier to remember
like

Code: Select all

enum ANIMATIONS
{
  ANIMATION_STAND,
  ANIMATION_WALK //etc
};
and cAnim = ANIMATOIN_STAND;
Image
Image
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

didn't you miss pAnim = cAnim; in the first snippet ??? ;)

Code: Select all

if (cAnim != pAnim)
{
  if (cAnim == 0) node->setFrameLoop(1, 1); // your stand anim
  if (cAnim == 1) node->setFrameLoop(320, 360); // your walk anim
  pAnim = cAnim;
}
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
B@z
Posts: 876
Joined: Thu Jan 31, 2008 5:05 pm
Location: Hungary

Post by B@z »

oh yeah xD
sorry forgot it
Image
Image
mcbart
Posts: 19
Joined: Thu Aug 07, 2008 11:36 am

Post by mcbart »

Thanks for help. :D
Post Reply