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;
}
Thanks