Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

I am currently creating a 3D Camera on the Playstation Mobile SDK (which is basically openGL with just a little bit of abstraction).

The game I am trying to make is a 3D Tactical. That means my world is composed of 3D cubes, all of the same size (unit size) and placed at integer position.

I'd like to be able to detect on which block the player clicks. That means, some sort of raytracing from the camera to detect the nearest block. My problem is that I am new to all this maths/physics/3d stuff. How could I implement this easily (and in a efficient enough way) ?

My camera currently have a position, and two modes : Target mode (it uses a LookAt on a point) and direction mode (it uses a lookAt on its position + the direction). That means I have access to the direction of the camera.

share|improve this question

marked as duplicate by Nathan Reed, msell, Josh Petrie, Byte56, bummzack May 10 '13 at 22:11

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

This seems to be the same question as:… – Nathan Reed May 10 '13 at 16:54
This question seems to be the same as you say. Sorry for the duplicate, I didn't use good enough keywords to find it. Thanks for the link anyway :) – Malharhak May 11 '13 at 13:47
up vote 1 down vote accepted

What you need to do is unproject a 2D screen coordinate to a ray within the world. Perform intersection tests of some sort with the ray, and then perform whatever handling logic you want. The first link explains the process of creating a ray from a screen coordinate, it uses DirectX, but the principle is the same. How you choose to identify the object you are clicking on is up to you. Chances are you will want some sort of acceleration structure to minimize the number of intersection tests, which can be done using some form of space partitioning, such as BSP/KD-Tree/Quad/Oct/whatever.

The second link provides a method for performing AABB intersection tests with a ray, and is one method you could use for identifying which cube you intersect. -Unproject -Slab test

EDIT Another note, you don't have to actually calculate a ray from a 2D coordinate, but instead could simply use the unprojected 3D coordinate to perform containment tests rather than intersection tests. You would still need to perform some sort of hierarchical bounding box tests to eventually identify what object you are clicking on is.

share|improve this answer
Your unprojecting link seems to be what I needed to convert screen to world. I'll check this out. This seems to be less hard than I thought. Thanks a lot :) – Malharhak May 10 '13 at 17:03

Not the answer you're looking for? Browse other questions tagged or ask your own question.