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

Author Topic: Slow motion  (Read 1618 times)

0 Members and 1 Guest are viewing this topic.

tortoise74

  • Newbie
  • *
  • Posts: 2
    • View Profile
Slow motion
« on: September 19, 2020, 03:30:29 pm »
Hi,
    New user here. I have a very simple SFML test application on ubuntu 20.04 using SFML 2.5.1.
There are three circles. One under user control and one bouncing left to right.

Problems:
  • The one moving left to right is not very fast.
  • Even so the one under user control cannot keep up with it.
  • I am using a delta of 1 (pixel per frame?). If I increase this motion is jumpy rather than smooth.

Put simply it feels clunky.
So I am assuming I've made a noob error of some kind.
However, this seems to be the same basic loop SFML commonly used.
How can I make this fast and slick feeling instead?


#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
    const float FPS = 120.0f; //The desired FPS. (The number of updates each second).
    bool redraw = true;      //Do I redraw everything on the screen?
    window.setFramerateLimit(FPS);
    sf::CircleShape shape2(100.f);
    sf::CircleShape shape(10.f);
    sf::Clock clock;
    shape2.setFillColor(sf::Color::Green);
    shape.setFillColor(sf::Color::Red);

    sf::CircleShape alien(10.f);
    alien.setFillColor(sf::Color::Yellow);
    auto alienSpeed = 1.f;
   
    while (window.isOpen())
    {
        sf::Event event;
        float xDelta = 0;
        float yDelta = 0;
        bool moved = false;
        if (window.pollEvent(event))
        {
           if (event.type == sf::Event::Closed)
           {
              window.close();
           }
           if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
            {
               xDelta -= 1.f;
               moved = true;
            }
            else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
            {
               xDelta += 1.f;
               moved = true;
            }
            if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
            {
               yDelta -= 1.f;
               moved = true;
            }
            else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
            {
               yDelta += 1.f;
               moved = true;
            }
        }

        if (xDelta !=0 || yDelta != 0)
        {
           shape.move(xDelta,yDelta);
        }

        alien.move(alienSpeed, 0.f);
        if (alien.getPosition().x >= 190)
        {
           alienSpeed = -1.f;
        }
        else if (alien.getPosition().x <= 0)
        {
           alienSpeed = 1.f;
        }

        if(redraw)
        {
           window.clear();
           window.draw(shape2);
           window.draw(shape);
           window.draw(alien);
           window.display();
        }
    }

    return 0;
}


Note: cross posted to https://gamedev.stackexchange.com/questions/185873/sfml-test-app-feels-slow-clunky
« Last Edit: September 19, 2020, 03:42:37 pm by tortoise74 »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Slow motion
« Reply #1 on: September 19, 2020, 05:57:39 pm »
You should not be mixing events and real-time inputs, see the dedicated tutorials.

Events: https://www.sfml-dev.org/tutorials/2.5/window-events.php
Real-time input: https://www.sfml-dev.org/tutorials/2.5/window-inputs.php
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

tortoise74

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Slow motion
« Reply #2 on: September 19, 2020, 09:39:16 pm »
That's much better. It is as I thought a noob error.
With isKeyPressed outside the loop its much better.
It should be:

        if (window.pollEvent(event))
        {
           if (event.type == sf::Event::Closed)
           {
              window.close();
           }
        }

        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
        {
               xDelta -= 1;
        }

Thank you.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Slow motion
« Reply #3 on: September 20, 2020, 08:38:50 am »
Keep in mind though, as someone commented on your StackExchange question, you should use a time step/framerate independent calculation.
For example you can take the duration of the last frame and multiple the wanted velocity by it.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/