Mouse event

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
skelli
Posts: 20
Joined: Sat Jul 07, 2007 7:07 pm

Mouse event

Post by skelli »

hey,

Im having trouble trying to get the mouse to click on a box in a scene
here is my code:

Code: Select all

#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")


class MyEventReceiver : public IEventReceiver 
{ 
public: 
   virtual bool OnEvent(SEvent event) 
   {     
    if (event.EventType == EET_MOUSE_INPUT_EVENT) 
      { 
   switch (event.MouseInput.Event) { 
            case EMIE_LMOUSE_LEFT_UP: 
                lmouse = true;
				std::cout("box clicked")
                return true; 
                break; 
            case EMIE_RMOUSE_LEFT_UP: 
                rmouse = true;
                std::cout("box clicked")
                return true; 
                break; 

            default: 
                return false; 
        } 
         return true; 
      } 
      return false; 
   } 
}; 
int main()
{
MyEventReceiver receiver;

device->setEventReceiver(&receiver);

IrrlichtDevice *device =createDevice(EDT_SOFTWARE, dimension2d<s32>(512, 384), 16,false, false, false, 0);
IVideoDriver* driver = device->getVideoDriver();
ISceneManager* smgr = device->getSceneManager();
 
scene::ISceneNode *box = smgr->addCubeSceneNode(1,0,-1,core::vector3df(10,10,10), 
core::vector3df(0,0,0),core::vector3df(100,10,100));

scene::IAnimatedMesh* mesh = smgr->getMesh("C:/Documents and Settings/Senan/My Documents/3dsmax/export/test.3DS");
scene::ISceneNode* node = 0;
scene::ISceneNode *box = smgr->addCubeSceneNode(1,0,-1,core::vector3df(10,10,10), 
core::vector3df(0,0,0),core::vector3df(100,10,100));

if (mesh)
node = smgr->addOctTreeSceneNode(mesh->getMesh(0));


scene::ITriangleSelector* selector = 0;

	if (mesh)
	{		
		node->setPosition(core::vector3df(-1370,-130,-1400));
		selector = smgr->createOctTreeTriangleSelector(mesh->getMesh(0), node, 128);
		node->setTriangleSelector(selector);
		selector->drop();
	}


scene::ICameraSceneNode* camera = smgr->addCameraSceneNodeFPS();
	camera->setPosition(core::vector3df(-1000,80,-150));
scene::ISceneNodeAnimator* anim =smgr->createCollisionResponseAnimator(
selector, camera, core::vector3df(30,50,30),core::vector3df(0,-3,0),core::vector3df(0,50,0));

	camera->addAnimator(anim);
	anim->drop();


int lastFPS = -1;
 
 
if(lmouse==true) 
{
	node=smgr->getSceneCollisionManager()->getSceneNodeFromScreenCoordinatesBB(mousepos, 0); 
	if(stringc(node->getName())=="box") 
	{
		box=node; 
    } 
    else 
    { 
		box=0; 
    } 
} 


while(device->run()){
	driver->beginScene(true, true, SColor(255,100,101,140));

	smgr->drawAll();
    core::line3d<f32> line;
    line.start = camera->getPosition();
	line.end = line.start + (camera->getTarget() - line.start).normalize() * 1000.0f;
	core::vector3df intersection;
	core::triangle3df tri;

if (smgr->getSceneCollisionManager()->getCollisionPoint(line, selector, intersection, tri))
{
    node = smgr->getSceneCollisionManager()-> getSceneNodeFromCameraBB(camera);	
	driver->endScene();

}
	device->drop(); return 0;
}
Any suggestions? i think there is something wrong with my events?

Thanks
LE_Shatai
Posts: 6
Joined: Fri Jul 06, 2007 7:02 am

Post by LE_Shatai »

Hi

I am not sure, but I guess the problem is
the

Code: Select all

   selector->drop(); 
.
Does it work without this line?
LE_Shatai
Posts: 6
Joined: Fri Jul 06, 2007 7:02 am

Post by LE_Shatai »

You also have typos in your eventhandler. Try this:

Code: Select all

	switch (event->MouseInput.Event) 
	{
		case EMIE_LMOUSE_LEFT_UP:
			cout << "LCLICKED: box clicked\n";
			return true;
		break;
		case EMIE_RMOUSE_LEFT_UP:
			cout << "RCLICKED: box clicked\n";
			return true;
		break;
	}
	flush(cout);
	return false; 
skelli
Posts: 20
Joined: Sat Jul 07, 2007 7:07 pm

Post by skelli »

im getting these errors:

Code: Select all

'box': identifier not found, even with argument-dependent lookup
 'box': identifier not found, even with argument-dependent lookup
 'mousepos' : undeclared identifier
c 'box' : redefinition; multiple initialization
        box.cpp(49) : see declaration of 'box'
 'cout': identifier not found, even with argument-dependent lookup
 error C3861: 'flush': identifier not found, even with argument-dependent lookup
 error C3861: 'cout': identifier not found, even with argument-dependent lookup
 error C2065: 'cout' : undeclared identifier
also i think i might be missing the raytrace but im not sure how to implement that. I found the collision tutorial very confusing at the end when you change the lights on the fairys
Perceval
Posts: 158
Joined: Tue May 30, 2006 2:42 pm

Post by Perceval »

You have not declared mousepos before using it. For the three last errors, you have just forgotten to include a file (stdlib.h i think).
For the box, this code

Code: Select all

scene::ISceneNode *box = smgr->addCubeSceneNode(1,0,-1,core::vector3df(10,10,10),
core::vector3df(0,0,0),core::vector3df(100,10,100)); 
is written at least twice. :)
skelli
Posts: 20
Joined: Sat Jul 07, 2007 7:07 pm

Post by skelli »

still no joy!! :(


getting a system.nullreferenceexeption when i run the program :?:

can someone please help im out of my wits here!!!
LE_Shatai
Posts: 6
Joined: Fri Jul 06, 2007 7:02 am

Post by LE_Shatai »

You are using the same name twice. Rename the secand added box to box2, or something like this.

Code: Select all

scene::ISceneNode *box = smgr->addCubeSceneNode(1,0,-1,core::vector3df(10,10,10),core::vector3df(0,0,0),core::vector3df(100,10,100));
[...]
scene::ISceneNode *box = smgr->addCubeSceneNode(1,0,-1,core::vector3df(10,10,10),core::vector3df(0,0,0),core::vector3df(100,10,100)); 
then these messages should be history.

Code: Select all

c 'box' : redefinition; multiple initialization
        box.cpp(49) : see declaration of 'box'
You need to include iostream.h

Code: Select all

//in Linux without .h ending
#include <iostream> 

//in Windows with .h ending
#include <iostream.h> 
Then the flush and cout error should disappear.

Maybe you should consider learning something more C++ before starting of writing a game, which is a bit complicated and complex, for the beginning.
Perceval
Posts: 158
Joined: Tue May 30, 2006 2:42 pm

Post by Perceval »

Maybe you should consider learning something more C++ before starting of writing a game, which is a bit complicated and complex, for the beginning.
Yes, you seem to have some lacks in C++.
getting a system.nullreferenceexeption when i run the program
This error is often related to bad use of pointers. Look at your code :

Code: Select all

device->setEventReceiver(&receiver);

IrrlichtDevice *device =createDevice(EDT_SOFTWARE, dimension2d<s32>(512, 384), 16,false, false, false, 0); 
You are using device before its declaration, that's why you get this error. You should have written :

Code: Select all

IrrlichtDevice *device =createDevice(EDT_SOFTWARE, dimension2d<s32>(512, 384), 16,false, false, false, 0); 
device->setEventReceiver(&receiver);
or :

Code: Select all

IrrlichtDevice *device =createDevice(EDT_SOFTWARE, dimension2d<s32>(512, 384), 16,false, false, false, &receiver);
Post Reply