Hit Detection


How do I do hit detection? When I click on something on the screen, how do I identify what object that is?


i will provide the concept here.
basically when you click on the screen you obtain the x,y coordinates in screen space. you need to transform this pickpoint back to the world/eye space to do proper collision testing. this is done with the inverse of the modelviewprojection matrix which is not that simple as it sounds because the matrix projects and thus is singular and singular matrices have no inverse because they would device by 0 otherwise. once you obtained the matrix you transform your pick point back, construct a ray from it and test it for collision with your objects, either by bounding volume or raw triangle testing.
there are some glu functions gluProject/gluUnproject i think they deal with it. im using the approach described above.

i wouldn’t say its for beginners though. check:

Another way is when you have click coordinates, you render (hidden from the user) a 1 pixel zone at this position, with each object drawn with a different flat color. Then glReadPixels it.

I’ve successfully used the method suggested by ZbuffeR but with a larger rendering area (such as 5x5 pixels). I then chose the object represented by the color with the largest number of pixels in the rendered area. This approach worked better for me because my users tend to think of object proximity rather than individual pixels. It can also be difficult for users to utilize single pixel picking because the exact pick point isn’t always obvious with some mouse pointers.

Thanks for the info!
Looks like I should look over matrices and understand that first.
I was hoping there is a library that I can use to do simple hit detection but it sounds like it is not as simple as I first imagined.

HI guys, how can this be possible there was a lot of thread regarding this toppic going up to 2013, and i can t find it anymore, is it possible that the guy who wrote it erased everything?