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

Author Topic: Calculating Mouse movement not accurate when View is modified  (Read 6759 times)

0 Members and 1 Guest are viewing this topic.

Guido_Ion

  • Newbie
  • *
  • Posts: 42
    • View Profile
    • Flow Soccer development
Hi! I'm implementing mouse movement that mimmics the movement of a GamePad Stick.

What I need is to calculate the movement each frame, I do this by calculating the difference between the previous position and the new one. The problem occurs when the View is modified by zooming and/or by moving. In this case even if I don't move the mouse, its position is modified.

This is part of the code:
Code: [Select]
static sf::Vector2f previousMousePosInCoords = Game::GetWindow()->mapPixelToCoords(sf::Mouse::getPosition(*Game::GetWindow()));
sf::Vector2f mousePosInCoords = Game::GetWindow()->mapPixelToCoords(sf::Mouse::getPosition(*Game::GetWindow()));
sf::Vector2f mouseDifferenceInCoords = mousePosInCoords - previousMousePosInCoords;
// if mouse moved
if (mouseDifferenceInCoords.x != 0.f && mouseDifferenceInCoords.y != 0.f) {
Input.thereIsMovement = true;
Input.directionNormalized = Game::SfVecToBoxVec(mouseDifferenceInCoords);
Input.directionLenght = Input.directionNormalized.Normalize();
previousMousePosInCoords = mousePosInCoords;
}
else Input.thereIsMovement = false;

I understand that if I work on pixels instead of coordinates it should work but the movement will depend on the resolution the game is played.
Other thing I could do is substract the amount of movement and zoom of the view but I think they might be a simpler better solution.
How could I do this?
« Last Edit: March 08, 2021, 09:37:52 pm by Guido Bisocoli »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11053
    • View Profile
    • development blog
    • Email
Re: Calculating Mouse movement not accurate when View is modified
« Reply #1 on: March 19, 2021, 09:40:06 am »
You set previousMousePosInCoords and mousePosInCoords to the same mouse position and then you take the difference which is of course 0.
Your previousMousePosInCoords should only be update after the calculation.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Guido_Ion

  • Newbie
  • *
  • Posts: 42
    • View Profile
    • Flow Soccer development
Re: Calculating Mouse movement not accurate when View is modified
« Reply #2 on: March 19, 2021, 05:55:52 pm »
But previousMousePosInCoords is static, so this line:

Code: [Select]
static sf::Vector2f previousMousePosInCoords = Game::GetWindow()->mapPixelToCoords(sf::Mouse::getPosition(*Game::GetWindow()));
is only assigned once. All the other times are assigned on this line:

Code: [Select]
previousMousePosInCoords = mousePosInCoords;
While mousePosInCoords is always assigned on this line:
Code: [Select]
sf::Vector2f mousePosInCoords = Game::GetWindow()->mapPixelToCoords(sf::Mouse::getPosition(*Game::GetWindow()));
This way the difference (mouseDifferenceInCoords) is working ok, it returns the difference.

The problem I have is when I move the view. The screen moves and even when there is no mouse movement it register a mouse movement because for SFML when the View is moved the mouse position is moved even if the mouse didn't move. That's the problem.

I guess I should substract the View movement to the mouse position making it messy. I was wondering if there is another way of doing it.
Thanks for answering!

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11053
    • View Profile
    • development blog
    • Email
Re: Calculating Mouse movement not accurate when View is modified
« Reply #3 on: March 19, 2021, 09:47:43 pm »
Well only you have the source code in context, I can't tell when which function is called.

I think using a static here doesn't really do what you want.
If it's a class member, then that makes it the same for all instances of said class.
If it's a local function static, it just means it's only available in this translation unit.

Here's some pseudo code on how you could approach it:
loop as long as the window is open
    process events (e.g. Closed event)

    set the current and converted mouse position
    take the delta from the previous mouse position
    set the previous mouse position equal to the current mouse position

    clear window
    draw your things
    display things to the window
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/