views:

164

answers:

2

I'm using Matlab to create a small chess game for one of my courses this semester. The thing I'm having trouble with is having the user be able to select one of the chess pieces.

To simplify things, I'm making it so that the user selects a piece by clicking on the square that the chess piece resides on rather than clicking the piece itself (which I assume would be much more difficult). I know how to get the x and y coordinates of the view-port, but how do I transform these coordinates into 3-space coordinates?

I know that there are multiple x,y,z coordinates associated with each view-port coordinate, but I'm only interested in the x,y,z coordinate where z = 0 (since the board itself is in the x,y plane that intersects the z axis where z = 0).

Thanks!

A: 

Not MATLAB-specific, but this may help: Given the camera parameters and the click location, you could project a ray into the scene and test for intersections, but it would probably be a whole lot easier to just consider the inverse problem: Map your chess-grid squares onto screen coordinates, and then just compare in 2D against the click location. Note that this is already being done in rendering the scene.

tzaman
+2  A: 

You may want to have a look at select3d from the file exchange. It allows you to select patch objects in 3D (which is probably how you want to create your board, anyway).

Jonas

related questions