Hello, I started c++ a while ago when I realized I needed an engine. For the past week I've been searching for a good one when I came across Irrlicht. I instantly fell in love with it. I have been going through the tutorials, however I can get my mind around number 4. Could someone explain step-by-step how this code works?
/*
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 (node != 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 = node->getPosition();
v.Y += event.KeyInput.Key == KEY_KEY_W ? 2.0f : -2.0f;
node->setPosition(v);
}
return true;
}
}
return false;
}
};
Also how do you handle input from mouse movement? Thanks in advance
Last edited by CameroKid on Fri Jul 14, 2006 10:13 pm, edited 1 time in total.
input from the mouse, keyboard, or in-game gui is all handled through the event system. the event handler gets a struct "SEvent", which contains the information about which keys are down or up, what the x and y position of the mouse is, and any info about changes made to elements of the gui.
you create a class derived from IEventReciever, an instance of which you pass into the createDevice() call, and it is designated to recieve events from the engine. the OnEvent method you implement in that class should check the various elements of the SEvent struct for the input you care about.
My irrlicht-based projects have gone underground for now, but if you want, check out my webcomic instead! http://brokenboomerang.net
Thanks, I think I'm starting to understand Key Pressing, but how do I handle mouse movements? For example rotating an object right as you move your mouse right.
Thanks, that did help. I set it up so an X movement moves the object. Another problem came up, when I'm not tuching the mouse the object still moves. Heres the two main lines:
Logically, let's examine the second line. It is adding the mouse X position times 0.0000001f to the X position. So, even if the mouse does not move, it is still adding the same number it added last time.
OK I think I understand what's happening. The MOUSE_X = event.MouseInput.X command returns the pixal the mouse is on correct? what I need to do is set the cursor to 0,0 each loop. But how do I do that?
Nonono, all you have to do is use the code I posted. Yes, it returns the pixel the mouse is on in that frame. So if you store the previous frame's pixel and the current frames pixel and subtract them, you will have the difference between the last frame and the current frame - exactly what you want. IE:
Frame 0, mouse x is 40
Frame 1, mouse x is 60
Frame 1 (60) - Frame 0 (40) = Difference (20)
Frame 0, mouse x is 70
Frame 1, mouse x is 70
Frame 1 (70) - Frame 0 (70) = Difference (0)
#include <stdio.h>
#include <wchar.h>
#include <irrlicht.h>
bool keys[irr::KEY_KEY_CODES_COUNT];//HOLDS THE INFO IF OUR KEYS ARE UP OR DOWN
float MOUSE_WHEEL;
float MOUSE_X;
float MOUSE_Y;
float PREV_MOUSE_X;
using namespace irr;
#pragma comment(lib, "Irrlicht.lib")
scene::ISceneNode* node = 0;
IrrlichtDevice* device = 0;
class MyEventReceiver : public IEventReceiver
{
public:
virtual bool OnEvent(SEvent event)
{
if(event.EventType == irr::EET_KEY_INPUT_EVENT)
{//if the keys gave input
keys[event.KeyInput.Key] = event.KeyInput.PressedDown;
return true;//we set it on if it was pushed, off if it was let up
}
if(event.EventType == irr::EET_MOUSE_INPUT_EVENT)
{
// mouse move
if (event.MouseInput.Event == EMIE_MOUSE_MOVED)
{
PREV_MOUSE_X = MOUSE_X;
MOUSE_WHEEL = event.MouseInput.Wheel;//update mouse wheel as many chances as we can get
MOUSE_X = event.MouseInput.X;//update mouse wheel as many chances as we can get
MOUSE_Y = event.MouseInput.Y;//update mouse wheel as many chances as we can get
}
//left mouse button
if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN)
{
keys[KEY_LBUTTON] = 1;//on
return true;//TRUE
}
if (event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP)
{
keys[KEY_LBUTTON] = 0;//off
return true;//TRUE
}
//right mouse button
if (event.MouseInput.Event == EMIE_MMOUSE_PRESSED_DOWN)
{
keys[KEY_MBUTTON] = 1;//on
return true;//TRUE
}
if (event.MouseInput.Event == EMIE_MMOUSE_LEFT_UP)
{
keys[KEY_MBUTTON] = 0;//off
return true;//TRUE
}
//right mouse button
if (event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN)
{
keys[KEY_RBUTTON] = 1;//on
return true;//TRUE
}
if (event.MouseInput.Event == EMIE_RMOUSE_LEFT_UP)
{
keys[KEY_RBUTTON] = 0;//off
return true;//TRUE
}
return true;//TRUE
}
return false;
}
};
int main()
{
MyEventReceiver receiver;
for(int x=0; x<irr::KEY_KEY_CODES_COUNT; x++) keys[x] = false; //reset all keys
device = createDevice(video::EDT_OPENGL, core::dimension2d<s32>(640, 480),
16, false, false, true, &receiver);//without "&receiver" in that last field this device would not care if we had an event receiver or not
video::IVideoDriver* driver = device->getVideoDriver();
scene::ISceneManager* smgr = device->getSceneManager();
/*
This creates the block we will move using the keys and mouse buttons
*/
node = smgr->addTestSceneNode();
node->setPosition(core::vector3df(0,0,30));
node->setMaterialTexture(0, driver->getTexture("../../media/wall.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.
*/
smgr->addCameraSceneNode(0, core::vector3df(0,80,-100), core::vector3df(0,5,0));
while(device->run())
{
driver->beginScene(true, true, video::SColor(255,113,113,133));
smgr->drawAll(); // draw the 3d scene
core::vector3df v = node->getPosition();
// v.X += (keys[irr::KEY_KEY_W] - keys[irr::KEY_KEY_S]) * 0.1f;// : -2.0f;
// v.Z += (keys[irr::KEY_KEY_A] - keys[irr::KEY_KEY_D]) * 0.1f;
// v.Y += (keys[irr::SEvent::X] - keys[irr::KEY_RBUTTON]) * 0.1f;
v.X += (MOUSE_X - PREV_MOUSE_X) * 0.005f;
node->setPosition(v);
driver->endScene();
}
/*
In the end, delete the Irrlicht device.
*/
device->drop();//go bye bye
return 0;
}
stupid me. I caught it. I had PREV_MOUSE_X = MOUSE_X; in a If statement where the mouse moved. I just moved it to right after the movement command. Thanks alot for the help all