Jump to content
  • Advertisement
Sign in to follow this  
fmwyso

Mouse Cursor Position

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

Hey, I need to find the placement of my mouse in X and Y coordinates. I have a 2-d "map" if you will and am in need of just getting those x, y coords... It should be known that I'd actually rather X, Y coords in integer rather than float / double... I tried google searching but from what I've seen... A. The codes don't work. B. I don't understand them. C. I'm not using them correctly. Any help is appreciated, thanks. P.S. I am using CodeBlocks and using the default setup with NeHe's new lesson tutorial. (I'm new, so please explain things that you may assume I already know... Worst case scenario is I have to a read a few more lines which I don't mind one bit.)

Share this post


Link to post
Share on other sites
Advertisement
Which mouse coords do you need. On screen (i.e. like x pixels right of the windows left border) or within the world (e.g. on the map).

If on screen, then you should have a look into your language's/systems libraries (or tell us ;) ).

If within the world, here's a short explanation.

I'm not sure how much you know OpenGL already, so here's a short summary of the transformation stages of a point from local (object) space to screen.

1a. transform by model matrix to world space
1b. transform by view matrix to eye space
2. transform by projection matrix to screen space (values in range [-1,1] for x,y and [0,1] for z)
3. use viewport information to transform into pixel coordinates.

In OpenGL model and view matrix are merged together to form the modelview matrix, so I named those steps 1a and 1b.

In order to transform from pixel coordinates to world space you have to apply steps 1b to 3 in reverse order and using the inverse matrices (i.e. instead of multiplying with the projection matrix, you multiply with the inverse projection matrix).

In a 2d game you often have orthogonal projection matrices, so unprojection is quite easy. The frustum is basically a box so just remap your values from [-1,1] to [left,right] for x, doing the same for y and z (note that z is [0,1] to [near,far]) for step 2.

Hope that helps.

Edit: If you don't understand what I wrote, you maybe should read a bit on matrix algebra in general and its use in 3D application. Among the most important keywords are projection matrix, unprojection, modelview matrix, screen space, eye space, world space and viewport.

Share this post


Link to post
Share on other sites
Thanks for your detailed post and I could understand what you said (not extensively but still did understand what you meant :P).

My program is 2-d, and I have set up the scale / projection to be in the exact pixels the frame size is. I'm assuming that would mean that the mouse coords on the screen (or I should say window) would be exactly what I want...

Just so you know... My map doesn't really exist, I don't have any textures... The only thing this project will become is just basically a GUI in OpenGL. The reason for this project is to better understand some functions of OpenGL, and it has definitely gotten me more familiar with this very confusing thing :P.

Anyway, I do just need the mouse coordinates on the screen... I checked out some of the libraries but noticed they only work for the frame that calls it or something? All I know is that it wasn't working for me... Then, I tried to use ScreenToClient but I couldn't find out how to get the handle of my OpenGL window ><.

Currently, the only thing I know about Mouse Coords is that you can do the C++ function GetCursorPos(&var), but I need to find out how to get the coords in the window ><. So, could you explain how I could do that? By the way, the way my X/Y is setup is so that the origin is in the lower-left corner of my window.

Share this post


Link to post
Share on other sites
So if I understand you correctly you need to get the mouse coordinates relative to your OpenGL window?

To answer this, we'd need to know your OS and programming language.

Share this post


Link to post
Share on other sites
This probably sounds really stupid... but how do I get the handle to my OpenGL window? ><

Btw, I think I forgot to mention that I'm using the NeHe's new tutorial code for everything... (I'm just re-writing the draw function and a few other things).

Share this post


Link to post
Share on other sites
Quote:
Original post by fmwyso
This probably sounds really stupid... but how do I get the handle to my OpenGL window? ><


Are you not using Windows API to create the opengl window? If so, what are you using?

Share this post


Link to post
Share on other sites
I don't THINK I'm using Windows API >,<...

Here's the code I'm using to open the window, etc.

http://nehe.gamedev.net/wiki/NewLesson1.ashx

I honestly have no clue how most of it works to be honest :P ><.

Share this post


Link to post
Share on other sites
Ah, I see you're using SDL. In that case it's also quite simple to retrieve the mouse position.

Just add an SDL_MOUSEMOTION case in the event switch and use event.motion.x and .y.


int mouseX = 0, mouseY = 0;
...
switch(event.type)
...
case SDL_MOUSEMOTION:
{
mouseX = event.motion.x;
mouseY = event.motion.y;
break;
}
...

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!