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

Started by
20 comments, last by skow 20 years, 9 months ago
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.
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.
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?
Surely some one must know.

I figure most RTS''s need this in open gl
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.
_______________________________________Pixelante Game Studios - Fowl Language
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).
oh, the coords of point are ox,oy,oz.
for testing, you just can put vertexf(ox,oy,oz);
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]
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.
here ya go.
this one uses nehe''s simple basecode.
added lines are commented //Gytis.

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

This topic is closed to new replies.

Advertisement