###

Question:

I'm starting study opengl, and im tring to make a 3d chess like, but i cant figureout, how i can know where i have clicked in the "table" to make the proper animations, any advice ?

###

Solution:1

This is called "3D picking". You have to translate screen coordinates into world coordinates. From there, do a ray/collision object (bounding box?) intersection test. If they intersect, that's where the user clicked.

You'll have to do a little bit more than this to solve the depth-order problem, like finding the first time to intersection of each object, then selecting the one with the lowest (positive) time.

If you google for "3D picking" you might find what you are looking for.

Here is a tutorial: http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=32

Note that this is not specific to any shape of bounding object, be it a bounding box, a polygon, a curve, etc. You just have to figure out the math for the intersection test for each type of object you want to support.

**Edit:**

I didn't read that tutorial before I linked it, I just figured NEHE is where all the cool kids learn OpenGL (admittedly ten years ago...).

Here is something from the OpenGL FAQ about picking:

###

Solution:2

waldecir, look for a raypick function. It's the name for sending a ray from the scene's camera center through the pixel you clicked on (actually, through that pixel's translated position on the camera's plane representing the "glass surface of the screen" in the 3D world) and return the frontmost polygon the ray hits together with some information. Usually coordinates within the polygon's surface axes, e.g. UV or texture coordinates. By checking the coordinates, you can determine which square the user clicked on.

Rays can be sent from any position and in any direction, so likely you'd have to get the camera position and its plane center, but the documentation should be able to help you there.

**Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com**

EmoticonEmoticon