[solved] Basic Movement from the turorial

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
natol
Posts: 25
Joined: Fri Aug 13, 2004 5:12 pm
Location: USA, KS

[solved] Basic Movement from the turorial

Post by natol »

ok ok, it's been a long time since I have looked at this engine and I was messing around with the movement tutorial.

I can get a test scene node to move just fine but when I try to get the model to move it won't work....just stands there....but no errors....

here is the code:

Code: Select all

/*
This Tutorial shows how to move and animate SceneNodes. The
basic concept of SceneNodeAnimators is shown as well as manual
movement of nodes using the keyboard.

As always, I include the header files, use the irr namespace,
and tell the linker to link with the .lib file.
*/
#include <stdio.h>
#include <wchar.h>
#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")

/*
In this tutorial, one of our goals is to move a scene node using some
keys on the keyboard. We store a pointer to the scene node we want to
move with the keys here.
The other pointer is a pointer to the Irrlicht Device, which we need
int the EventReceiver to manipulate the scene node and to get the 
active camera.
*/

scene::ISceneNode* anms = 0;
scene::ISceneNode* node1 = 0;
scene::ISceneNode* node = 0;
IrrlichtDevice* device = 0;


/*
To get events like mouse and keyboard input, or GUI events like 
"the OK button has been clicked", we need an object wich is derived from the 
IEventReceiver object. There is only one method to override: OnEvent. 
This method will be called by the engine when an event happened. 
We will use this input to move the scene node with the keys W and S.
*/
class MyEventReceiver : public IEventReceiver
{
public:
	virtual bool OnEvent(SEvent event)
	{
		/*
		If the key 'W' or 'S' was left up, we get the position of the scene node,
		and modify the Y coordinate a little bit. So if you press 'W', the node
		moves up, and if you press 'S' it moves down.
		*/

		if (node1 != 0 && event.EventType == irr::EET_KEY_INPUT_EVENT&&
			!event.KeyInput.PressedDown)
		{
			switch(event.KeyInput.Key)
			{
			case KEY_KEY_W:
			case KEY_KEY_S:
				{
					core::vector3df v = node1->getPosition();
					v.X += event.KeyInput.Key == KEY_KEY_W ? 2.0f : -2.0f;
					node1->setPosition(v);
				}
				return true;
			}
		}

		return false;
	}
};


/*
The event receiver for moving a scene node is ready. So lets just create
an Irrlicht Device and the scene node we want to move. We also create some
other additional scene nodes, to show that there are also some different 
possibilities to move and animate scene nodes.
*/
int main()
{
	MyEventReceiver receiver;

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

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


	/*
	Create the node for moving it with the 'W' and 'S' key. We create a
	'test node', which is a cube built in into the engine for testing 
	purposes. We place the node a (0,0,30) and we	assign a texture to it
	to let it look a little bit more interesting.
	*/
	node = smgr->addTestSceneNode();
	node->setPosition(core::vector3df(0,0,30));
	node->setMaterialTexture(0, driver->getTexture("../../media/wall.bmp"));

	/*
	The last scene node we add to show possibilities of scene node animators is 
	a md2 model, which uses a 'fly straight' animator to run between to points.
	*/
	IAnimatedMesh* mesh = smgr->getMesh("../../media/sydney.md2");
	IAnimatedMeshSceneNode* node1 = smgr->addAnimatedMeshSceneNode( mesh );
                            node1->setMaterialFlag(video::EMF_LIGHTING, false);
		                    node1->setPosition(core::vector3df(0,0,20));
		                    node1->setRotation(core::vector3df(0,180.0f,0));
		                    node1->setMaterialTexture(0, driver->getTexture("../../media/sydney.bmp"));
	


	/*
	To be able to look at and move around in this scene, 
	we create a first person shooter style camera and make the 
	mouse cursor invisible.
	*/
	scene::ICameraSceneNode* cam = smgr->addCameraSceneNodeFPS(0, 100.0f, 100.0f);
	device->getCursorControl()->setVisible(false);
	

	/*
	We have done everything, so lets draw it. We also write the current
	frames per second and the name of the driver to the caption of the
	window.
	*/
	int lastFPS = -1;

	while(device->run())
	{
		driver->beginScene(true, true, video::SColor(255,113,113,133));
		smgr->drawAll();
		driver->endScene();

		int fps = driver->getFPS();

		if (lastFPS != fps)
		{
			wchar_t tmp[1024];
			swprintf(tmp, 1024, L"Movement Example - Irrlicht Engine [%s] fps:%d", 
				driver->getName(), fps);

			device->setWindowCaption(tmp);
			lastFPS = fps;
		}
	}

	/*
	In the end, delete the Irrlicht device.
	*/
	device->drop();
	
	return 0;
}

Any Help is greatly appreciated
Pharoseer
Posts: 5
Joined: Wed Sep 03, 2003 6:41 am

Post by Pharoseer »

It took me a second but I spotted your problem. At the top you have a declaration:

Code: Select all

scene::ISceneNode* node1 = 0;
then down in your main function you have the declaration:

Code: Select all

IAnimatedMeshSceneNode* node1 = smgr->addAnimatedMeshSceneNode( mesh );
See the problem? You're moving node1, but it's the GLOBAL version of node1, not the node1 that is local to your main() function. I spent 20 minutes looking for a similar bug myself just the other day. Cut & paste can kill you sometimes. :)

-Pharoseer
natol
Posts: 25
Joined: Fri Aug 13, 2004 5:12 pm
Location: USA, KS

question ????

Post by natol »

well here's my question.

If I don't declare the node as global then when the class complains that the node isn't declared yet.

In the tutorial for the test node it is declared as a global before the main and it works fine. It's just when I go to try and use the animated node that it doesn't work.
cartoonit
Posts: 286
Joined: Mon Nov 15, 2004 6:36 pm

Post by cartoonit »

Yeah its all to do with scope. A variable has different scope depending where you declare it, I'm tempted to do a whole post on this subject but I think you can find enough information from your compiler help or even MSDN help. :roll:
Post Reply