Archived

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

skow

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

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
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
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
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
Guest Anonymous Poster
Unable to login >-[

If you know your camera position in 3d space you can use math to compute your intersection -> this is a line/plane intersection, your line being defined by your camera center and the "looking-at" direction vector.

line equation :
x = xC + dir.x*t
y = yC + dir.y*t
z = zC + dir.z*t

plane :
ax + by +cz + d = 0 -> being xz plane gives :
by = 0;

then combining with line equation :
yC + dir.y*t = 0 ->
t = -yC / dir.y

then
x = xC + dir.x*t
z = zC + dir.z*t

If your terrain is not a plane it''s a bit more complicated, but it can also be done.

Share this post


Link to post
Share on other sites
Ok I wrote some code to find the point on a plance between 2 points.

I''m getting conflicting information.

"for eg. in your RTS game, this would be a point on your heightmap"

Or is the point on the far clipping plane?

I havnt had time to dl and look at the samle code, ill do that in a few hours when i get time.

Share this post


Link to post
Share on other sites
The way I''ve always heard to do 3d selection is the ray method. What you do is unProject to the near clip plane and then you unProject to the far clip plane. This gives you two points from which a ray can be created. Using this ray you can hit test all of the objects in your scene ( that are relevant, like your terrain ) and then and if the ray passes through, you select the object. This also allows for full 3d selection, instead of being restrained to one plane. Of course the math is slightly more complicated though.

Share this post


Link to post
Share on other sites
Yeah, i can just use the camera posistion for the close point. By defualt does it return the point of the mouse on the far clip plane?


I ran the demo on http://onemancrew.sinfree.net
has the cords on the far clipping plane, unless some polygon was in the way (the read pixels).

If i do it with out the read pixels and just set the wz for a high number will that just give me the back clipping plane?

BTW i apricate the help, thanks guys

[edited by - skow on July 9, 2003 12:49:59 PM]

Share this post


Link to post
Share on other sites
Erm nevermind, I just tested it, if i omit the pixel check and set the wz to 1, it will always give me the back cliping plane.

I dont know why i didnt jsut check instead of asking first

Thanks again for the help.

Share this post


Link to post
Share on other sites
Hmmm, your (Gytis) code seems to work fine in your exe, but placed after some of my render code, it''s throwing funny numbers.

http://spongemop.dyndns.org/arch_mouse.jpg The last three numbers, after time and fps.

Looks to me like a bad cast between floating point and integer, but all the data types are the same as they are in your program. The three double coordinates are sprintf''ed as doubles in that screenshot, then printed using my bitmap font-print function.

Any idea what would be causing that?

Share this post


Link to post
Share on other sites
convert it to an int before being displayed.

do a
int temp = ox
then display temp and see if the numbers are still that funkey.

Share this post


Link to post
Share on other sites
Hm, another problem
It seems to only want to return wz correct, like, 10% of the time. Randomly. The rest of the time it will set wz to 0, and the x/y/z coordinates will be my viewpoint, rather than where I clicked. O.o

Mop~

Share this post


Link to post
Share on other sites