Mouse Touch Area

Started by
2 comments, last by SigmaX 17 years, 2 months ago
Hi! I'm having trouble trying to figure out out to tell if my mouse is clicking on a specific object or not. Say I have a rectangle glBegin(GL_QUADS); glTexCoord2i(0, 1);glVertex3f(0.0f, 0.0f, 0.0f); glTexCoord2i(1, 1);glVertex3f( 8.0f, 0.0f, 0.0f); glTexCoord2i(1, 0); glVertex3f( 8.0f, 3.0f, 0.0f); glTexCoord2i(0, 0); glVertex3f(0.0f, 3.0f, 0.0f); glEnd(); I know how to recieve the x,y coordinates of the mouse cursor, but how do I tell that I'm actually over this object? If the camera changes and the object becomes smaller what happens then? If you have any links or any ideas please let me know. Thanks!
-)------ Ed
Advertisement
When the camera moves around, then the relative location of your quad on the screen will change.
There are a couple ways to approach this.

My preference, is to imagine that the mouse cursor is a ray that shoots out from the front of the camera, and as the camera moves around this ray is attached to it.

Working in WorldSpace then, you just have to check if that ray intersects with your quad. Which is basically a line vs plane test. Followed by a coordinate system transform and coordinate check.


Alternatly, you can work in ScreenSpace, and project the 4 vertices of the quad onto the ScreenPlane, then compare their screencoords to the mouse cursor. The math needed is essentially the same, but I like this approach less because now you have to operate on 4 verts as opposed to just 1 mouse cursor.


Lastly you can use the glu picking functions. But these I really avoid because; the above methods are faster, and more accurate. You can probably find a fast tutorial on this one though, and it saves you the work of having to understand whats really going on; if you can live with that.
well, that's the famous "picking" problem.

Although GL supports "Selection and Feedback", it is VERY slow for practice. The best way is, imaging there is a ray from eye point to infinite, passing the cursor point. The object you are clicking is who collisions with this ray.

There are several steps to do so:
1, Calculate that picking-ray by gluUnProject function.
2, Detect if there are intersections between picking-ray and each object.
3, Maybe you want the selected object is the nearest one to the eye, so you need to sort all intersected objects by distance.
http://358574108.qzone.qq.com
Thank you both very much. It looks like I have a lot more reading to do, but atleast I know which direction to look in.

-)------ Ed

This topic is closed to new replies.

Advertisement