Jump to content
  • Advertisement
Sign in to follow this  
CBell

Mouse Coordinate Trouble

This topic is 3717 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

I have something displayed on the screen, and what I would like to do is if the left mouse button is clicked while near that object, have something happen. The trouble I'm having is that the mouse callback function only gets the window coordinates, not the object coordinates. How do I tell if the mouse is near the object when the left button is clicked? I'm pretty sure I have to do something with the x and y coordinates of the mouse, but I'm not sure what.

Share this post


Link to post
Share on other sites
Advertisement
An object in 3D or 2D? If 2D, is it a circle, rectangle, or something else. If it is 3D, do you want to check a bounding box around it, a bouding sphere, or each individual triangle? And lastly, what are you using to make your window and using to get the mouse coordinates?

Share this post


Link to post
Share on other sites
Is your game 2d or 3d?

For 2d, you just transform the mouse location (in window coordinates) into world coordinates (add the camera translation to the louse location, take into account rotation if needed).

For 3d you either can cast a ray from the mouse location into the scene and see what it hits, or project each object into screen coordinates and then check the distance to the mouse location, or use the OpenGL picking mode.

Share this post


Link to post
Share on other sites
Quote:
Original post by Tenac
An object in 3D or 2D? If 2D, is it a circle, rectangle, or something else. If it is 3D, do you want to check a bounding box around it, a bouding sphere, or each individual triangle? And lastly, what are you using to make your window and using to get the mouse coordinates?


It's in 3D, and a bounding box would be fine.

I'm using the glutMouseFunc() to get the mouse position.

What do you mean when you say make your window?

My main looks like:

int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize (500, 500);
glutInitWindowPosition (500, 500);
glutCreateWindow ("Window");
init ();
makeMenu();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(key);
glutMouseFunc(mouse);
glutSpecialFunc(special);
glutMotionFunc(motion);

return 0;
}

The reshape looks like:

void reshape(int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
gluPerspective(30.0, (GLfloat) w/(GLfloat) h, 1.0, 2000.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0, 0.0, 100.0, 0.0, 0.0, 0.0, 0, 1, 0);

}

Share this post


Link to post
Share on other sites
Well, a quick google came up with these for me:

http://gpwiki.org/index.php/OpenGL:Tutorials:Picking
http://www.codeproject.com/KB/openGL/openglselectobject.aspx
http://www.lighthouse3d.com/opengl/picking/
http://jerome.jouvie.free.fr/OpenGl/Tutorials/Tutorial27.php

Share this post


Link to post
Share on other sites
Quote:
Original post by Tenac
Well, a quick google came up with these for me:

http://gpwiki.org/index.php/OpenGL:Tutorials:Picking
http://www.codeproject.com/KB/openGL/openglselectobject.aspx
http://www.lighthouse3d.com/opengl/picking/
http://jerome.jouvie.free.fr/OpenGl/Tutorials/Tutorial27.php


Thanks, I tried google, but I guess I didn't type in the right thing.

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!