Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Convert coordinates from mouse to opengl/cartesian format?  (Read 11464 times)

0 Members and 1 Guest are viewing this topic.

alaterale

  • Guest
Convert coordinates from mouse to opengl/cartesian format?
« 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!

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Convert coordinates from mouse to opengl/cartesian format?
« Reply #1 on: September 30, 2009, 07:15:49 pm »
Quote from: "alaterale"
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?
Code: [Select]
float MouseRelativeToCenterX = MouseX - Window.GetWidth() / 2.f;
float MouseRelativeToCenterY = MouseY - Window.GetHeight() / 2.f;
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

alaterale

  • Guest
Convert coordinates from mouse to opengl/cartesian format?
« Reply #2 on: September 30, 2009, 08:07:36 pm »
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).

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Convert coordinates from mouse to opengl/cartesian format?
« Reply #3 on: September 30, 2009, 08:26:08 pm »
If your Y axis is flipped compared to SFML you just have to invert the Y coordinate.
Laurent Gomila - SFML developer

alaterale

  • Guest
Convert coordinates from mouse to opengl/cartesian format?
« Reply #4 on: September 30, 2009, 08:31:42 pm »
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.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Convert coordinates from mouse to opengl/cartesian format?
« Reply #5 on: September 30, 2009, 09:28:29 pm »
gluUnproject is ok if you give it all the necessary data (modelview, projection, viewport).
Laurent Gomila - SFML developer

alaterale

  • Guest
Convert coordinates from mouse to opengl/cartesian format?
« Reply #6 on: September 30, 2009, 09:37:05 pm »
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).

 

anything