Jump to content
  • Advertisement
Sign in to follow this  
SigmaX

Mouse Touch Area

This topic is 4254 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!