# Irrlicht mouse input

I am trying to make a chess game at this moment, using Irrlicht.

It will be a 3D Chess game but apart from most chess games it has one special quality: Free movement of the camera.

The Chess game is found in a closed room, the player can move into that room as in a MMORPG or such ( w,a,s,d move and hold right-click to rotate the camera ). It can move anywhere he wants but there will be collision detection between the player and the pieces/board/walls.

Now,I want to make the pieces move when the player click on them.Untill now I basically got the coords of a squeare ( the window ones ) And I checked the coords of the mouse click.But considering my moveable camera the coords of a squeare will never be the same, so how should I make the game realise which square I click?. My board is an obj object made in blender.

Question: How to get the mouse input in order to realise which squeare has been clicked?

Edit due to comment:

I have looked upon the tutorial for collision detection don't worry. The main ideea is that I am not sure how to "draw" a ray from my mouse to the square or the first thing it his, or how to determine if it hit a square or not the "wooden" thing around it.

This is how the game begins:

It happens that you can move around and click where u want to.So it can be from a diffrent angle such as this:

Or even from under the table:

The main ideea is that i want to know which square he hit, and if he hit a square an not the wooden thinggy + the mouse isn't always clicked from the middle of the screen such as in the tutorial, it can be clicked from anywhere.

• If you're asking how to detect collision of a ray with a square, here's a tutorial I found just googling for "irrlicht collision detection". If you're asking how to figure out where the camera is pointing, surely you already know that if you're able to draw the scene from the camera's perspective. – Anko May 6 '13 at 10:33

Since the board will always be at the same y coordinate, you could convert mouse co-ordinates to game world co-ordinates and then work out which square is hit. core::line3d getRayFromScreenCoordinates(const float pX, const float pY, IrrlichtDevice *device ) { core::line3d ln(0,0,0,0,0,0);

    irr::scene::ICameraSceneNode* camera = device->getSceneManager()->getActiveCamera();

if (!camera)
return ln;

const scene::SViewFrustum* f = camera->getViewFrustum();

core::vector3df farLeftUp = f->getFarLeftUp();
core::vector3df lefttoright = f->getFarRightUp() - farLeftUp;
core::vector3df uptodown = f->getFarLeftDown() - farLeftUp;

const core::rect<s32>& viewPort = device->getVideoDriver()->getViewPort();
core::dimension2d<u32> screenSize(viewPort.getWidth(), viewPort.getHeight());

f32 dx = pX / (f32)screenSize.Width;
f32 dy = pY / (f32)screenSize.Height;

if (camera->isOrthogonal()) {
ln.start = f->cameraPosition + (lefttoright * (dx-0.5f)) + (uptodown * (dy-0.5f));
} else {
ln.start = f->cameraPosition;
}
ln.end = farLeftUp + (lefttoright * dx) + (uptodown * dy);

return ln;
}

core::plane3d< f32 > ground(core::vector3d<f32>(0, 0, 0), //plane through 0,0,0
core::vector3d<f32>(0, 1, 0)); // plane on y axis

core::vector3df convertMousePositionToWorld(float x, float y) {
core::line3df line = getRayFromScreenCoordinates(x, y, &device);
core::vector3d< f32 >startintersection;
ground.getIntersectionWithLine(line.start,line.end,startintersection);

return startintersection;
}

• I don't really get the ideea that the board will be always at the same y since you rotate and move around it so it can change in my opinion ( Tho in the gameworld it has always the same coords ). As far as i looked through the code returns a 3d vector of where it hits things?, if yes how do I know he clicked on a square and not on the wooden thing around it?, or is line.end the ingame coords of where it clicked? Also if this is the way that means I should find the in-game coords of all the squares? – user1640736 May 6 '13 at 21:24
• The Y of the board should stay the same, it's the camera that moves around it (surely?). The 3d vector that is returned (startintersection) is the point at which the mouse pointer intersects the game world at y=0. – user14992 May 6 '13 at 21:37
• To find out which square is selected, couldn't you divide the X and Z by the size of the square? i.e when x = 9.8 and a square has a width of 2: 9.8/2 = 4.8 and therefor the mouse pointer is over the 4th square. – user14992 May 6 '13 at 21:39
• I think I finally understand what you mean,by the constant y u mean that the board isn't moving at all and by assuming y=0 u mean that the board is drawn at the origin of the sistem. Correct me if I am wrong. And as a separate question kinda, you told me about the width of the square but is the Irrlicht sistem defined the same way as the blender one? or do I have to do a lot of tests to realise how big is 1 square – user1640736 May 8 '13 at 19:08