moving vehicle

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
porki
Posts: 15
Joined: Fri Jun 08, 2007 2:18 pm

moving vehicle

Post by porki »

I have problem with my vehicle. I load it from 3ds file:
IVideoDriver* driver = device->getVideoDriver();
ISceneManager* smgr = device->getSceneManager();
IGUIEnvironment* guienv = device->getGUIEnvironment();
IAnimatedMesh* mesh = smgr->getMesh("../../media/truck.3ds");
IAnimatedMeshSceneNode* node = smgr->addAnimatedMeshSceneNode( mesh );
if (node)
{
node->setMaterialFlag(EMF_LIGHTING, false);
node->setMD2Animation ( scene::EMAT_STAND );
}
smgr->addCameraSceneNode(0, vector3df(0,100,-100), vector3df(0,5,0));
scene::ICameraSceneNode * cam = smgr->addCameraSceneNodeFPS();
device->getCursorControl()->setVisible(true);
But I can't make it move like in tutorial 4 ( using W and S keys). I can compile it but pressing keys does nothing. What should I change in code if my model is loaded as AnimatedMesh, not as ISceneNode
Please help!
Perceval
Posts: 158
Joined: Tue May 30, 2006 2:42 pm

Post by Perceval »

Post all the code, because we can't see if you've made an error in the event receiver.
porki
Posts: 15
Joined: Fri Jun 08, 2007 2:18 pm

Post by porki »

Here is code. Watch out node and node1:)
#include <irrlicht.h>
#include <iostream>
using namespace irr;
#pragma comment(lib, "Irrlicht.lib")
scene::IAnimatedMesh* mesh = 0;
scene::IAnimatedMeshSceneNode* node1 = 0;
scene::ISceneNode* node = 0;
IrrlichtDevice* device = 0;
class MyEventReceiver : public IEventReceiver
{
public:
virtual bool OnEvent(SEvent event)
{
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.Y += event.KeyInput.Key == KEY_KEY_W ? 2.0f : -2.0f;
node1->setPosition(v);
}
return true;
}
}
return false;
}
};
int main()
{device = createDevice( video::EDT_OPENGL, dimension2d<s32>(1024, 768), 16,false, false, false, 0);
video::IVideoDriver* driver = device->getVideoDriver();
scene::ISceneManager* smgr = device->getSceneManager();
mesh = smgr->getMesh("../../media/truck.3ds");
scene::IAnimatedMeshSceneNode* node1 = smgr->addAnimatedMeshSceneNode( mesh );
node1->setPosition(core::vector3df(0,0,30));
node1->setMaterialFlag(video::EMF_LIGHTING, false);
scene::ICameraSceneNode * cam = smgr->addCameraSceneNodeFPS(0, 100.0f, 100.0f);
device->getCursorControl()->setVisible(true);
int lastFPS = -1;
while(device->run())
{
driver->beginScene(true, true, video::SColor(255,113,113,133));
smgr->drawAll(); // draw the 3d scene
device->getGUIEnvironment()->drawAll();
driver->endScene();
int fps = driver->getFPS();
if (lastFPS != fps)
{
core::stringw tmp(L"Movement Example - Irrlicht Engine [");
tmp += driver->getName();
tmp += L"] fps: ";
tmp += fps;

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

return 0;
}
Perceval
Posts: 158
Joined: Tue May 30, 2006 2:42 pm

Post by Perceval »

You need to declare a MyEventReceiver object and pass it to the device :

Code: Select all

int main() {
MyEventReceiver receiver;
device = createDevice( video::EDT_OPENGL, dimension2d<s32>(1024, 768), 16,false, false, false, &receiver);
...
porki
Posts: 15
Joined: Fri Jun 08, 2007 2:18 pm

Post by porki »

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

Still nothing:/
porki
Posts: 15
Joined: Fri Jun 08, 2007 2:18 pm

Post by porki »

These line are in my code, so something is still wrong
video::E_DRIVER_TYPE driverType = video::EDT_DIRECT3D9;
printf("Please select the driver you want for this example:\n"\
" (a) Direct3D 9.0c\n (b) Direct3D 8.1\n (c) OpenGL 1.5\n"\
" (d) Software Renderer\n (e) Burning's Software Renderer\n"\
" (f) NullDevice\n (otherKey) exit\n\n");
char i;
std::cin >> i;

switch(i)
{
case 'a': driverType = video::EDT_DIRECT3D9;break;
case 'b': driverType = video::EDT_DIRECT3D8;break;
case 'c': driverType = video::EDT_OPENGL; break;
case 'd': driverType = video::EDT_SOFTWARE; break;
case 'e': driverType = video::EDT_BURNINGSVIDEO;break;
case 'f': driverType = video::EDT_NULL; break;
default: return 0;
}
MyEventReceiver receiver;

IrrlichtDevice* device = createDevice( driverType, core::dimension2d<s32>(640, 480),16, false, false, false, &receiver);
if (device == 0)
return 1; // could not create selected driver.
Perceval
Posts: 158
Joined: Tue May 30, 2006 2:42 pm

Post by Perceval »

Ok, i think i've understood. You're declaring 2 IAnimatedMeshSceneNode* node1. The test in the event receiver might be done on the wrong one (i've seen someone that made the same error some time ago). So, in your main, change :

Code: Select all

scene::IAnimatedMeshSceneNode* node1 = smgr->addAnimatedMeshSceneNode( mesh ); 
to :

Code: Select all

node1 = smgr->addAnimatedMeshSceneNode( mesh ); 
I don't really understand why compilers allow this code to compile, since it looks like a double declaration.
porki
Posts: 15
Joined: Fri Jun 08, 2007 2:18 pm

Post by porki »

ok, that works. Yes, it was a double declaration, I don't know why I didn't catch that
Thank you!
TheMiss
Posts: 14
Joined: Sun Apr 15, 2007 6:46 pm

Post by TheMiss »

You can declare double variables because the first 'scene::IAnimatedSceneNode *node' is a gobal variable and the second one is local variable. So when you use the same variable a compiler will use local one... :lol:

sorry my bad ENGLISH it is not my native language :wink:
prajay.shetty
Posts: 14
Joined: Tue Jun 05, 2007 8:04 am
Location: INDIA,MAHARASHTRA

plzz help

Post by prajay.shetty »

porky can u help me out i just wanted a simple code to move an object
such as sydney.md2 plzz help
olivehehe_03
Posts: 157
Joined: Tue Mar 20, 2007 8:30 am

Post by olivehehe_03 »

@prajay.shetty
Have a look at the examples that come with Irrlicht for help about how to move objects. Also search the forums or have a look at my reply to porki's question here
Tell me what you cherish most. Give me the pleasure of taking it away.
Post Reply