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

Author Topic: Correct mouse position after moving view?  (Read 2723 times)

0 Members and 2 Guests are viewing this topic.

Hmmz

  • Newbie
  • *
  • Posts: 5
    • View Profile
Correct mouse position after moving view?
« on: January 31, 2014, 08:01:53 pm »
How do I retrieve the correct mouse position after moving view?

At the moment, I am doing it this way:
pixelPos = Mouse::getPosition(window);
coordPos = window.mapPixelToCoords(pixelPos);

I tried passing the currently used view as the second parameter to the 'mapPixelToCoords' method, but it didn't help. I am trying to snap objects to a 48x48 grid, but the farther I move the view, the more mouse input deviates from the expected.

What am I doing wrong?

http://pastebin.com/6tGSwLQz
« Last Edit: January 31, 2014, 08:08:32 pm by Hmmz »

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Correct mouse position after moving view?
« Reply #1 on: January 31, 2014, 08:49:09 pm »
I tried your code and it works perfectly. How far are we supposed to move the view to notice the problem? I moved it up to 15000, 13000.

Hmmz

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Correct mouse position after moving view?
« Reply #2 on: January 31, 2014, 09:01:38 pm »
I tried your code and it works perfectly. How far are we supposed to move the view to notice the problem? I moved it up to 15000, 13000.

Oh. Sorry. I am moving it to the left.
Here's how it looks:
http://i.imgur.com/icSpNmx.png

Notice the cursor and where the object spawned. Hm... strangely enough, it does seem to work okay if moving to the right. I wonder if I am perhaps not handling negative values correctly... but if that were the case, the objects would snap to the left of the cursor and not to the right.

Or are we not allowed to use negative coords in general? If not, maybe I should just limit myself to positive values and be done with (actually, that would be one heck of a way to avoid cryptic algorithms)

« Last Edit: January 31, 2014, 09:07:43 pm by Hmmz »

Estivo

  • Jr. Member
  • **
  • Posts: 74
    • View Profile
    • Email
Re: Correct mouse position after moving view?
« Reply #3 on: January 31, 2014, 09:42:03 pm »
I have my view class with getPosition method, I think is the best option. If you have your custom view you move object by
Quote
obj.pos = mouse + view.getPosition();

I think it is the easiest way.

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Correct mouse position after moving view?
« Reply #4 on: January 31, 2014, 09:54:57 pm »
When x is negative, the remainder of x % 48 is also negative. You then remove the negative remainder from x, -- gives + so it actually adds the remainder and your rectangle is drawn to a multiple of 48 to the right of where it should be.

For an obvious solution, remove 48 to x when x is < 0. (do the same for y) There are probably better solutions. :P
int x = static_cast<int>(std::trunc(coordPos.x));
int y = static_cast<int>(std::trunc(coordPos.y));

if (x < 0) x -=48;
if (y < 0) y -=48;

x -= x % 48;
y -= y % 48;

coordPos.x = static_cast<float>(x);
coordPos.y = static_cast<float>(y);

Hmmz

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Correct mouse position after moving view?
« Reply #5 on: January 31, 2014, 11:07:20 pm »
Well... now I feel dumb. Can't believe I was thinking that I was doing something incorrectly from the coding point of view rather than doing simple mathematics!

But I decided to limit myself to just positive coordinates. Since my game is going to be data-driven, negative positions inside a JSON file would make a really ugly sight, I presume. :p