Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

skow

How to find cords of a mouse on a 2d plane?

This topic is 5487 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''m not sure how to do this... Mabey someone here can help me out. I have a RTS game with a camera that rotates around and up and down. For movement I need to know where the user clicks a unit to move, after it is selected. I just need to find out how to get the projected cords along the X/Z plane. Does any one know a tutorial on how to do so, or have a good explination or sample code? Thanks, any help is apricated.

Share this post


Link to post
Share on other sites
Advertisement
Hi. You might be able to get away with using some win32 functions...such as This will give you the X and Y screen coords. Here''s a short example:

POINT myPoint;
GetCursorPos(&myPoint);
MyX = myPoint.x;
MyY = myPoint.y;

Also, here is a "picking" tutorial that uses Opengl/Glut, and shows 4 snowmen on the screen. the tutorial goes through and shows you how to detect which snowman you are picking with the click of your mouse.

http://www.lighthouse3d.com/opengl/picking/index.php3?color1

I hope this helps.

Share this post


Link to post
Share on other sites
Yeah i know about how to "pick" what oject is clicked on.

Also i know about getting the mouse position, but i need a way to calculate (project) screen cords to the XZ plane.

Any one else got any ideas, or awnsers?

Share this post


Link to post
Share on other sites
Surely some one must know.

I figure most RTS''s need this in open gl

Share this post


Link to post
Share on other sites
I believe the functions are something like gluProject()/gluUnproject() that convert screen-to-world or world-to-screen coordinates. Search a bit, see if you come up with anything.

Share this post


Link to post
Share on other sites
glGetIntegerv(GL_VIEWPORT,@viewport);
wx := LOWORD(lParam);
t:= HIWORD(lParam);
wy := viewport[3]-t;
glGetDoublev(GL_MODELVIEW_MATRIX,@modelview);
glGetDoublev(GL_PROJECTION_MATRIX,@projection);
glReadPixels(LOWORD(lParam),HIWORD(lParam),1,1,GL_DEPTH_COMPONENT,GL_FLOAT,@wz);
gluUnProject(wx,wy,wz,@modelview,@projection,@viewport,ox,oy,oz);

after everything is rendered.
might need tweaking, because this is straight copy/paste from my code.
and btw, his s**t is delphi (but i love it).

Share this post


Link to post
Share on other sites
oh, the coords of point are ox,oy,oz.
for testing, you just can put vertexf(ox,oy,oz);

Share this post


Link to post
Share on other sites
Ok I'm slightly confused i pas in the xmouse and ymouse components in, and 1 for the z component, and I'll get the x,y,z compnents of it projected in 3d space in my game.

I want this to be in the XZ plane, so i get the point back and and make a line between the camera and the point, and see where it intersects the y=0 plane.

I want to make sure I'm understanding this right before i get into coding this.


Is this right? Is there an easier way?

[edited by - skow on July 8, 2003 8:56:26 PM]

Share this post


Link to post
Share on other sites
no, you just pass in mouse x and y coords on the screen and you get exact point in world, where mouse points.
for eg. in your RTS game, this would be a point on your heightmap(???).
i think i''ll write a small progie for you later today.

Share this post


Link to post
Share on other sites
here ya go.
this one uses nehe''s simple basecode.
added lines are commented //Gytis.

http://onemancrew.sinfree.net/tmp/screen2world.exe

Share this post


Link to post
Share on other sites

  • 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!