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

Author Topic: sf::Mouse::getPosition(*window) is returning the wrong location.  (Read 7331 times)

0 Members and 1 Guest are viewing this topic.

chrisciscoioio

  • Newbie
  • *
  • Posts: 17
    • View Profile
sf::Mouse::getPosition(*window) is returning the wrong location.
« on: September 29, 2012, 08:19:44 pm »
I am going to start this off with I am sure I am missing something, but I am not sure what.

sf::Vector2i pos = sf::Mouse::getPosition(*window); doesnt seem to be returning the correct y value, the x value appears right however, I have attached an image that shows where my mouse is when clicked compared with where the point appears.

[attachment deleted by admin]

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: sf::Mouse::getPosition(*window) is returning the wrong location.
« Reply #1 on: September 29, 2012, 08:48:10 pm »
The image tells nothing..I don't get your problem, remember that it returns pixels not coords and that y is positive the lower cursor is.
Back to C++ gamedev with SFML in May 2023

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: sf::Mouse::getPosition(*window) is returning the wrong location.
« Reply #2 on: September 29, 2012, 10:46:56 pm »
The top left corner is at (0, 0).

chrisciscoioio

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: sf::Mouse::getPosition(*window) is returning the wrong location.
« Reply #3 on: September 30, 2012, 12:51:11 am »
If I click on the black dot it creates a point at the green dot.

I guess this is because I am rendering in OpenGL, and just using SFML for windows and input.

How can I transform the one set of coords into the other?

codecranker

  • Guest
Re: sf::Mouse::getPosition(*window) is returning the wrong location.
« Reply #4 on: September 30, 2012, 01:56:10 am »
I think its because of the confusion between local coordinates vs world coordinates. If you are using sf::Sprite, you can use Sprite.TransformToGlobal or Sprite.TransformToLocal to convert your coordinates.

chrisciscoioio

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: sf::Mouse::getPosition(*window) is returning the wrong location.
« Reply #5 on: September 30, 2012, 02:03:42 am »
I am not using sf::Sprite.

I think that is the issue, I am just not sure how to fix it, sf::Sprite is not an option.

codecranker

  • Guest
Re: sf::Mouse::getPosition(*window) is returning the wrong location.
« Reply #6 on: September 30, 2012, 02:11:10 am »
what are you using then, if not sf::Sprite?

if you are using opengl, then I guess you have to apply the right transformations to your GL_MODELVIEW matrix at the right place i.e before you issue your draw commands. you can refer to the documentation of glLoadMatrix(), glLoadIdentity(), glPushMatrix() and glPopMatrix(). These methods will come handy.

chrisciscoioio

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: sf::Mouse::getPosition(*window) is returning the wrong location.
« Reply #7 on: September 30, 2012, 02:35:26 am »
Drawing is fine, this is an issue I am having integrating SFML with OpenGL to get world coords.

What I don't have is converting the sf::mouse position to world coords. Everything else already works as long as I can get the correct coords.

Right now on my screen, the x works fine, because I am not moving the camera, but the y value is off, if I am above the mid line of the screen it is below it, and if I am below the mid line it is above it.
« Last Edit: September 30, 2012, 02:41:04 am by chrisciscoioio »

codecranker

  • Guest
Re: sf::Mouse::getPosition(*window) is returning the wrong location.
« Reply #8 on: September 30, 2012, 03:13:14 am »
aha ok.
so the coordinate system is reversed I guess. you have to apply correction to your y-value. If I understood correctly, it seems like the mid line is somehow corresponds to the center of the screen. If your x-value is good, then simply negating the y-value should solve the problem. Otherwise, may be you can try the following:

SFML- top-left = (0, 0). mouse position should be relative to this.
w, h = screen width, screen height
assuming your (0, 0) is (w/2, h/2). screen center = your camera center. (this is my assumption).

you can apply correction to your mouse_x and mouse_y
mouse_x = mouse_x - w/2
mouse_y = mouse_y - h/2

if your center is different you can apply correction to your received mouse coordinates accordingly.

chrisciscoioio

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: sf::Mouse::getPosition(*window) is returning the wrong location.
« Reply #9 on: October 01, 2012, 12:27:19 am »
That doesn't actually work but I played with a variety of systems, and I found one that solves this, baring any camera movement which I would have to adjust by.

But if I take the screenHeight and minus the pos.y it will work.

 

anything