SFML community forums
Help => Window => Topic started by: alaterale on September 30, 2009, 12:48:33 pm
-
Hi,
I've been trying to figure this (mouse picking and coordinates) out all night and I'm getting nothing but a headache... I haven't been able to find anything that directly does what I want here. It sounds like a stupid question, but my brain just doesn't seem to be working today...
The mouse coordinates are retrieved from top/left right? Can anyone tell me what formula would be best to convert this to start from the center of the screen (opengl style)? Everything I come up with has a flaw, but maybe I have to have a bunch of ifs to check for what quadrant the mouse is in and then do something different?
It's basically a mouse picking (screen to world coord) problem, but I have zero time to implement a big color checking, raycasting, etc, solution. For what I need it would seem like a simple formula (or multiple formulas) to transform top/left to center would suffice, at least for now as a hack until I have time later.
So, if I have an object at say (256, 90, 7) in opengl space, and I want to know when the mouse is near 256, 90, it seems like it would be a simple addition of half the width and height of the window, but that only works I think when its in that quadrant. Anyway, my overtired brain can't seem to piece it together... Normally I'd feel pretty emarassed to post what could just be a math stupidity on my part, but maybe someone else can give me a quick answer.
Longer term, I tried to use gluUnProject and such to get world coordinates, but it was always inaccurate in the results... I'm only using SFML::Window currently for window creation, so I don't think I can use the screen coord conversion in SFML::View.
I hope I didn't place this in the wrong category too. I apologize if this post sounds garbled or stupid, I haven't gotten much sleep in the last 48 hours.. Watch it be like some simple thing that I figure out right after posting :P Though maybe someone can help me get proper screen-to-world actually working though, which it would be cool. Ok, thanks!
-
The mouse coordinates are retrieved from top/left right? Can anyone tell me what formula would be best to convert this to start from the center of the screen (opengl style)?
What about this?
float MouseRelativeToCenterX = MouseX - Window.GetWidth() / 2.f;
float MouseRelativeToCenterY = MouseY - Window.GetHeight() / 2.f;
-
That's what I first tried, but it doesn't work for all coordinates.
One example is a 800x600 window, with the (opengl) point (-200, 100). I know this value in screen/mouse coordinates is (200, 200), so plugging into that formula, my x is correct (-200) but my y is not (-100 instead of 100). That's why I was thinking I probably have to do quadrants (at least for this kind of coordinate hack).
-
If your Y axis is flipped compared to SFML you just have to invert the Y coordinate.
-
Hehe, yeah I just saw that a few minutes ago... man I feel like an idiot. That's what I get for trying to solve a problem with no sleep at 6 in the morning :P
For future use too, any tips on how to project into proper world coordinates? I suspect I have to use gluUnProject (twice someone said), but last time I tried, it was close but off.
-
gluUnproject is ok if you give it all the necessary data (modelview, projection, viewport).
-
It was a while ago when I was trying that and I thought I was. I followed this:
http://softwareprodigy.blogspot.com/2009/08/gluunproject-for-iphone-opengl-es.html
and I got something that was ok, except it thought the center point was 100 to the left of what it really was :P
I figured it was a working library, just I couldn't see how following nearly the exact thing (had to change from floats to doubles) they did caused it not to work for me (since I don't think I'm doing anything screwy with my projection or modelview matrix).