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

Author Topic: Stuck in event loop when using Event::MouseMoved  (Read 7298 times)

0 Members and 1 Guest are viewing this topic.

wh1t3crayon

  • Jr. Member
  • **
  • Posts: 93
    • View Profile
Stuck in event loop when using Event::MouseMoved
« on: November 20, 2014, 06:47:01 pm »
I'm trying to use relative mouse movement to move a sprite around the screen, but I'm having trouble. The sprite doesn't move until I move the mouse completely off the screen, but only because this movement breaks the loop of Event::MouseMoved. Here is the event code:
while(window.pollEvent(event)){
                if(event.type == sf::Event::Closed){
                        window.close();
                }
                if(event.type == sf::Event::MouseMoved){
                        sf::Vector2f mouseOffsetOrigin = static_cast<sf::Vector2f>(sf::Mouse::getPosition(window));
                        float mouseOffsetX, mouseOffsetY;
                        mouseOffsetX = sf::Mouse::getPosition(window).x - mouseOffsetOrigin.x;
                        mouseOffsetY = sf::Mouse::getPosition(window).y - mouseOffsetOrigin.y;
//flashlight is a sprite
                        board.GetFlashlight().move(mouseOffsetX, mouseOffsetY);
//keep the mouse in the center of the screen
                        sf::Mouse::setPosition(sf::Vector2i(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2), window);
                }
                if(event.key.code == sf::Keyboard::Escape){
                        window.close();
                }

The code is pretty simple I think, but it's not working properly because the event loop never breaks out.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Stuck in event loop when using Event::MouseMoved
« Reply #1 on: November 20, 2014, 06:55:20 pm »
The simple solution is to do it outside the event loop. Since your code doesn't use the event data at all, this shouldn't be hard. Just move this block of code outside the event loop.
Laurent Gomila - SFML developer

wh1t3crayon

  • Jr. Member
  • **
  • Posts: 93
    • View Profile
Re: Stuck in event loop when using Event::MouseMoved
« Reply #2 on: November 20, 2014, 07:10:52 pm »
Changing the if statement to
if(sf::Event::MouseMoved)
and putting the statement just outside my while loop still causes a weird bug. The sprite only moves at random times, not smoothly with the mouse movements, and the movements look rather jolty. I've searched but can't find a post that tells how to deal with and use relative mouse movement.

Gambit

  • Sr. Member
  • ****
  • Posts: 283
    • View Profile
Re: Stuck in event loop when using Event::MouseMoved
« Reply #3 on: November 20, 2014, 07:12:54 pm »
Why would you change the if statement to that? sf::Event::MouseMoved is valid, therefore the condition is always true and the statement will always execute. Do you mean
Code: [Select]
if (event.type == sf::Event::MouseMoved)?

wh1t3crayon

  • Jr. Member
  • **
  • Posts: 93
    • View Profile
Re: Stuck in event loop when using Event::MouseMoved
« Reply #4 on: November 20, 2014, 07:18:52 pm »
Yes that's what I meant, but it doesn't make a difference. The sprite still only moves randomly. Is there a problem with how I'm calculating the mouse delta?

Gambit

  • Sr. Member
  • ****
  • Posts: 283
    • View Profile
Re: Stuck in event loop when using Event::MouseMoved
« Reply #5 on: November 20, 2014, 07:21:50 pm »
Why dont you use sf::Mouse instead? That way you have the mouse position in real time.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Stuck in event loop when using Event::MouseMoved
« Reply #6 on: November 20, 2014, 07:36:54 pm »
If you put this code outside the event loop, obviously you musn't test the event (it's not valid outside the event loop). Just do it every frame, even when the mouse doesn't move.
Laurent Gomila - SFML developer

Hapax

  • Hero Member
  • *****
  • Posts: 3381
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Stuck in event loop when using Event::MouseMoved
« Reply #7 on: November 20, 2014, 07:57:29 pm »
One other thing that should be done here is to confirm that the event type is a keypress event before testing its keycode; when the event is not a keyboard event, event.key.code should not be accessed.

Mini-question: why, since you made mouseOffsetOrigin an sf::Vector2f, did you not make mouseOffset one, instead opting for two separate floats that create more code (in this case)?  :P
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

wh1t3crayon

  • Jr. Member
  • **
  • Posts: 93
    • View Profile
Re: Stuck in event loop when using Event::MouseMoved
« Reply #8 on: November 20, 2014, 08:12:40 pm »
If you put this code outside the event loop, obviously you musn't test the event (it's not valid outside the event loop). Just do it every frame, even when the mouse doesn't move.
Even when running the above code every frame, without the if statement, the bug is still there. I'm sure it has more to do with logic and less to do with sfml, but I'm still confused.

Hapax, I'll include your ideas so thanks for that, but I'm more concerned about getting this sprite to move first :P
« Last Edit: November 20, 2014, 08:15:49 pm by wh1t3crayon »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Stuck in event loop when using Event::MouseMoved
« Reply #9 on: November 20, 2014, 08:25:48 pm »
Can you show the new code?
Laurent Gomila - SFML developer

wh1t3crayon

  • Jr. Member
  • **
  • Posts: 93
    • View Profile
Re: Stuck in event loop when using Event::MouseMoved
« Reply #10 on: November 20, 2014, 08:29:05 pm »
void Engine::Process(){
        while(window.pollEvent(event)){
                if(event.type == sf::Event::Closed){
                        window.close();
                }
                if(event.key.code == sf::Keyboard::Escape){
                        window.close();
                }
        }

        //create a .01 by .01 position of mouse used for checking for float rect collisions
        sf::FloatRect mousefr(sf::Mouse::getPosition(window).x, sf::Mouse::getPosition(window).y, .01f, .01f);
        if(screen.intersects(mousefr)){
                if(board.GetMouseIn() == false){
                        sf::FloatRect mouseTestfr(WALL_SIZE, WALL_SIZE, SCREEN_WIDTH - WALL_SIZE * 2, SCREEN_HEIGHT - WALL_SIZE * 2);
                        if(mouseTestfr.intersects(mousefr)){
                                board.SetMouseIn(true);
                        }
                }
                if(board.GetMouseIn() == true){
                       
                        //get mouse coords, calculate offset of mouse movement, move flashlight by that much
                        sf::Vector2f mouseOffsetOrigin = static_cast<sf::Vector2f>(sf::Mouse::getPosition(window));
                        sf::Vector2f mouseOffset(sf::Mouse::getPosition(window).x - mouseOffsetOrigin.x, sf::Mouse::getPosition(window).y - mouseOffsetOrigin.y);
                        board.GetFlashlight().move(mouseOffset.x, mouseOffset.y);
                        sf::Mouse::setPosition(sf::Vector2i(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2), window);
That's everything possibly relevant in my process input function

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Stuck in event loop when using Event::MouseMoved
« Reply #11 on: November 20, 2014, 08:39:03 pm »
And what's the problem with this code? Obviously it can't be the initial problem (stuck in event loop) ;)
Laurent Gomila - SFML developer

wh1t3crayon

  • Jr. Member
  • **
  • Posts: 93
    • View Profile
Re: Stuck in event loop when using Event::MouseMoved
« Reply #12 on: November 21, 2014, 02:26:57 pm »
I'll try and explain my goal first and then explain what is happening. A video may be needed :-\ So, in theory, I will move the mouse, which will be centered in the screen, and the sprite moves based on how much the mouse moves from the center. After this calculation, I place the mouse at the center for the next loop. But instead the sprite only moves at random intervals whenever I move the mouse, but mostly the sprite doesn't even move, regardless of how fast or slow I move the mouse.

Update: It became even more confusing. I set the mouse to only recenter itself after every 100 ticks, which gives the mouse a solid two seconds to calculate its offset and have the flashlight move. But the issue still remains, which shows that it's not being caused by the function for retrieving mouse coordinates being too slow, which was my first guess.
« Last Edit: November 21, 2014, 05:23:11 pm by wh1t3crayon »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Stuck in event loop when using Event::MouseMoved
« Reply #13 on: November 21, 2014, 05:52:53 pm »
You should write from scratch a very simple code that only does this (moves a sprite depending on mouse delta), there's too much unrelated stuff in your original code for you (or us) to spot the error.
Laurent Gomila - SFML developer

wh1t3crayon

  • Jr. Member
  • **
  • Posts: 93
    • View Profile
Re: Stuck in event loop when using Event::MouseMoved
« Reply #14 on: November 21, 2014, 10:15:01 pm »
sf::RenderWindow window;
                        window.create(sf::VideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 32), "Test");
                        sf::Vector2i mouseOffsetOrigin = sf::Vector2i(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2);
                        sf::Vector2f mouseOffset(std::ceil(sf::Mouse::getPosition(window).x - mouseOffsetOrigin.x), std::ceil(sf::Mouse::getPosition(window).y -                                                                        mouseOffsetOrigin.y));
                        sf::Texture texture;
                        texture.loadFromFile("example.png");
                        sf::Sprite sprite(texture);
                        while(true){
                                sprite.move(mouseOffset.x, mouseOffset.y);
                                sf::Mouse::setPosition(mouseOffsetOrigin, window);
                window.clear();
                                window.draw(sprite);
                        }
I didn't test, but that should? compile in a main() function. Anyways, to reiterate, the sprite seems rather unresponsive. The sprite moves now at least (line 3 of my code was setting the origin to the mouse's current position, I fixed that to the screen's center), but the sprite seems to drift and is oversensitive. I know the problem has now gone from "bug with sf::Event" to "help me move my sprite smoothly" but it's still annoying to me. How can I move the sprite smoothly with the mouse opposed to its current jerkiness?

Edit: I just thought of a great metaphor to explain this "jerkiness." When I move my cursor onscreen outside my application, it stays in a pretty straight, coherent line. When I move my sprite in the program's window, the sprite looks like a really drunk version of the cursor that can't even walk a straight line :P
« Last Edit: November 21, 2014, 10:52:01 pm by wh1t3crayon »