0

I am interested in 'picking' (i.e. selecting) objects in my canvas by clicking on it.

  • I am not interested in picking by color (as that doesn't allow me to know which object has been clicked)
  • I am not interested in using gl.RenderMode(GL_SELECT). Anyway, my understanding is that this is obsolete and may not even be supported in WebGL.

Updated

I have an idea. Please let me know if this idea is flawed. I have a 3d world and a camera object - meaning i have a camera position, lookat veector, and up vector.

My idea is this:

"Fire" a vector/ray through the 3d world (this vector is basically the camera's lookat vector). Then i just iterate through all of my 3d objects and check if it intersects with this ray/vector and return the object which is the closest distance to the camera. My collision detection, for the time being, would be a simple bounding sphere. I can get more sophisticated later by using a more complex/accurate collision detection.

The only gap I see is this:

I do not know how to create the ray/vector based off of the mouse coordinate. I can get the (X, Y) of the mouse click on the canvas element. But not sure how to translate this to be the starting point of the ray/vector in world coordinate.

Any thoughts on this approach? Any ideas regarding the gap concern that i have?

genpfault
  • 51,148
  • 11
  • 85
  • 139
AlvinfromDiaspar
  • 6,611
  • 13
  • 75
  • 140
  • http://stackoverflow.com/questions/7364693/how-to-get-object-in-webgl-3d-space-from-a-mouse-click-coordinate or perhaps this one: https://www.udacity.com/course/viewer#!/c-cs291/l-124106599/m-175393398 – Cyclonecode Jun 04 '14 at 22:55
  • I would like to NOT use a 3rd party library. – AlvinfromDiaspar Jun 04 '14 at 23:55
  • If you don't know how to get a vector based on the mouse coordinate, then you could look at the library functions and see how they do it. Watch the film in the second link, it explains alot. – Cyclonecode Jun 05 '14 at 00:03
  • A helpful keyword is "Unproject", using the mouse x/y and 2 different z values (I use -1 and 1) – Tom Jun 05 '14 at 00:19
  • Please take a look at my reply on a similar question: http://stackoverflow.com/questions/23644470/how-to-convert-mouse-coordinate-on-screen-to-3d-coordinate/23645092#23645092 – glampert Jun 05 '14 at 02:54
  • Ok, but im just basically asking for peoples opinion on my approach to this problem. I gave specific example of how im thinking about doing this. But basically i do not see why unprojecting is necessary. If i have a camera location plus its lookat vector, then i have enough to cast a ray. What is wrong with this simple approach? – AlvinfromDiaspar Jun 05 '14 at 06:11
  • @AlvinfromDiaspar Unprojection is the process of getting the ray. If you already have it, then cool, go cast the ray. – Colonel Thirty Two Jun 12 '14 at 23:47

0 Answers0