Trying to get a node moving...

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
NoobieMan
Posts: 14
Joined: Fri Feb 02, 2007 12:31 pm

Trying to get a node moving...

Post by NoobieMan »

New problem. Have tried to solve few days... Well anyway i have tried to get my node to move when i press 'W'. Nothing happens when i press it. Code...:

Code: Select all

#include <iostream>
#include "C:/irrlicht-1.2/irrlicht-1.2/include/irrlicht.h"

using namespace std;
using namespace irr;

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

#pragma comment(lib, "C:/irrlicht-1.2/irrlicht-1.2/lib/Win32-visualstudio/Irrlicht.lib")

IAnimatedMeshSceneNode* node = 0;
IrrlichtDevice* device = 0;

class myEventReceiver : public IEventReceiver
{
public:
	virtual bool OnEvent(SEvent event)
	{
		if (node != 0 && event.EventType == irr::EET_KEY_INPUT_EVENT && !event.KeyInput.PressedDown)
		{
			switch(event.KeyInput.Key)
			{
			case KEY_KEY_W:
				{
					vector3df v = node->getPosition();
					v = v + (vector3df(1,0,0));
				}
				return true;
			}
		}

		return false;
	}
};

int main()
{
	myEventReceiver receiver;

	device = createDevice(EDT_DIRECT3D9, dimension2d<s32>(640,480), 16, false, false, false, &receiver);

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

	node = smgr->addAnimatedMeshSceneNode(smgr->getMesh("C:/irrlicht-1.2/irrlicht-1.2/media/sydney.md2"));

	if (node)
	{
		node->setMaterialFlag(EMF_LIGHTING,false);
		node->setFrameLoop(320,360);
		node->setAnimationSpeed(30);
		node->setMaterialTexture(0, driver->getTexture("C:/irrlicht-1.2/irrlicht-1.2/media/sydney.bmp"));
	}

	smgr->addCameraSceneNodeFPS(0, 100.0f, 100.0f);
	device->getCursorControl()->setVisible(false);

	while(device->run())
	{
		driver->beginScene(true,true,SColor(255,90,90,156));
		smgr->drawAll();
		driver->endScene();
	}

	device->drop();
	return 0;
}
Didnt understand that Movement tutorial very well, because i couldnt use that addTestSceneNode... Any helpers? :D
pwierz
Posts: 59
Joined: Sun Aug 20, 2006 3:32 pm

Post by pwierz »

Code: Select all

               vector3df v = node->getPosition(); 
               v = v + (vector3df(1,0,0)); 

//ADD this
node->SetPosition(v);
Getposition doesn't return a pointer to the nodes position, it returns the value. So you need to set the position once you modify it.
belfegor
Posts: 383
Joined: Mon Sep 18, 2006 7:22 pm
Location: Serbia

Post by belfegor »

addCubeSceneNode instead addTestSceneNode.
Small FPS demo made using Irrlicht&NewtonDEMO
InfoHERE
Its at very early stage but i think im crazy enough to finish it all alone.
NoobieMan
Posts: 14
Joined: Fri Feb 02, 2007 12:31 pm

Post by NoobieMan »

Whoops, i was stupid. Thought this moves only 1 px(?) per every hit. And i dont have a clue how could i get it to move all the time when W, in this case, is pressed down. Help me :D

@belfegor: thanks for that.
zeno60
Posts: 342
Joined: Sun May 21, 2006 2:48 am
Location: NC, USA
Contact:

Post by zeno60 »

You can use the bool keys[] method, or you can simply create a global bool, set it to true when your key is pressed, then in the main device loop, if that bool is true, move the player. Keep in mind, both methods will move the player 1 point each loop, which varies greatly among computers, best to timestamp it and have it move a speed (points per millisecond).

For the first method, look at the second post by vitek in this forum for an example: http://irrlicht.sourceforge.net/phpBB2/ ... light=keys
NoobieMan
Posts: 14
Joined: Fri Feb 02, 2007 12:31 pm

Post by NoobieMan »

I should've tried even few minutes to solve it, because after i posted, i tried to remove that ! before event.KeyInput.PressedDown. It worked, lol :D
Thanks for zeno60, i think i'll try that method too, for practice.
NoobieMan
Posts: 14
Joined: Fri Feb 02, 2007 12:31 pm

Post by NoobieMan »

I guess i talked too soon. Got it work now, but i only can move forward & backward. I type :
"v.X += event.KeyInput.Key == KEY_KEY_W ? 2.0f : -2.0f;" for those and the same style for left and right, but i dont know how i should replace those 2.0f and -2.0f. I've tried like 0,0-2.0f and stuff but not working. Or is the problem somewhere else? Heres the code for what i've tried...

Code: Select all

class myEventReceiver : public IEventReceiver
{
public:
	virtual bool OnEvent(SEvent event)
	{
		if (node != 0 && event.EventType == EET_KEY_INPUT_EVENT && event.KeyInput.PressedDown)
		{
			switch(event.KeyInput.Key)
			{
			case KEY_KEY_S:
			case KEY_KEY_W:
				{
					vector3df v = node->getPosition();
					v.X += event.KeyInput.Key == KEY_KEY_W ? 2.0f : -2.0f;
					node->setPosition(v);
				}
			case KEY_KEY_A:
			case KEY_KEY_D:
				{
					vector3df a = node->getPosition();
					a.X += event.KeyInput.Key == KEY_KEY_A ? 2.0f : -2.0f;
					node->setPosition(a);
				}
				return true;
			}
		}

		return false;
	}
};
Im so embarassed, asking all the time for how to and stuff... :oops:
pwierz
Posts: 59
Joined: Sun Aug 20, 2006 3:32 pm

Post by pwierz »

For keys 'A' and 'D'. Change it to a.Y or a.Z (depending on which axis you want to move). You have it as a.X which keeps you moving forward.
NoobieMan
Posts: 14
Joined: Fri Feb 02, 2007 12:31 pm

Post by NoobieMan »

Yeah, that works too but... When i change a.X to a.Z, it causes that if i move forward, it moves also to right. Same thing with if i move down...
pwierz
Posts: 59
Joined: Sun Aug 20, 2006 3:32 pm

Post by pwierz »

Add breaks to your case statement.
NoobieMan
Posts: 14
Joined: Fri Feb 02, 2007 12:31 pm

Post by NoobieMan »

I think i have had to Case breaks right. Im soo desperated, i dont have a clue :cry:

Well, if theres still someones who are willed to help still, then heres the code for moving class thing if it would help...

Code: Select all

class myEventReceiver : public IEventReceiver
{
public:
	virtual bool OnEvent(SEvent event)
	{
		if (node != 0 && event.EventType == EET_KEY_INPUT_EVENT && event.KeyInput.PressedDown)
		{
			switch(event.KeyInput.Key)
			{
			case KEY_KEY_S:
			case KEY_KEY_W:
				{
					vector3df v = node->getPosition();
					v.X += event.KeyInput.Key == KEY_KEY_W ? 2.0f : -2.0f;
					node->setPosition(v);
				}
			case KEY_KEY_A:
			case KEY_KEY_D:
				{
					vector3df a = node->getPosition();
					a.Z += event.KeyInput.Key == KEY_KEY_A ? 2.0f : -2.0f;
					node->setPosition(a);
				}
				return true;
			}
		}
		return false;
	}
};
I feel that i should stop trying and think for a new hobby...
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

Normally you would put a break after each block of a switch statement. This keeps the execution path from 'falling through' to the next case in the switch.

In your case it makes more sense to just return...

Code: Select all

switch(event.KeyInput.Key) 
{ 
   case KEY_KEY_S: 
   case KEY_KEY_W: 
      { 
         vector3df v = node->getPosition(); 
         v.X += event.KeyInput.Key == KEY_KEY_W ? 2.0f : -2.0f; 
         node->setPosition(v); 
      }
      return true; //<-- added
   case KEY_KEY_A: 
   case KEY_KEY_D: 
      { 
         vector3df a = node->getPosition(); 
         a.Z += event.KeyInput.Key == KEY_KEY_A ? 2.0f : -2.0f; 
         node->setPosition(a); 
      } 
      return true; 
} 
I think i have had to Case breaks right. Im soo desperated, i dont have a clue. I feel that i should stop trying and think for a new hobby...
Maybe. I'd suggest that you take some time to learn a little bit about C/C++ before you go trying to write a full blown program with it. You may find this to be a bit of help.
Athlon_Jedi
Posts: 156
Joined: Wed Jul 21, 2004 4:29 am
Location: Mishawaka, In

Uhh Seperate your cases

Post by Athlon_Jedi »


switch(event.KeyInput.Key)
{
case KEY_KEY_S:
case KEY_KEY_W:
{
vector3df v = node->getPosition();
v.X += event.KeyInput.Key == KEY_KEY_W ? 2.0f : -2.0f;
node->setPosition(v);
}
return true; //<-- added
case KEY_KEY_A:
case KEY_KEY_D:
{
vector3df a = node->getPosition();
a.Z += event.KeyInput.Key == KEY_KEY_A ? 2.0f : -2.0f;
node->setPosition(a);
}
return true;
}
not to sound condisending but shouldnt you have a keymap for EACH key you want to do something?


your S and D keys dont have any control statements to tell the engine what you want them to do.

seems like this code with the S and D keys would make them do what the A and W keys do since they have no control statements attached.
Post Reply