PDA

View Full Version : Hit Detection



vance00
10-30-2008, 10:51 PM
Hi

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

Thanks!

_NK47
10-31-2008, 02:35 AM
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:
http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=30

ZbuffeR
10-31-2008, 06:22 AM
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.

MrVoid
10-31-2008, 08:10 PM
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.

vance00
11-01-2008, 01:43 PM
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.