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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - tdevereaux

Pages: [1]
1
Wow, thank you so much that was definitely my issue.

I made a few changes based on your comments.

I changed my update code to always use the delta time UNLESS the delta is larger than my Fixed Update Time. In which case I will loop using the Fixed Update Time until the delta time is less, then I continue to update using the remainder of the delta. I simulated a GPU hiccup by adding a sf::sleep of half a second.

I also added full screen mode to ensure VSYNC was working, which I believe it was as my graphics card displays the FPS for the window as 60 both in windowed and full screen.


It now runs nice and smooth ;D

I apologize about the code formatting, I had pasted directly from visual studio but I guess the tabbing got messed up.

Here is my updated code:
#include <SFML/Graphics.hpp>

#include <time.h>
#include <iostream>

namespace
{
    static const float PPU = 16.0f;
    const float MOVE_SPEED = 64.0f;
    const sf::Vector2f VIEW_SIZE{ 320.0f, 180.0f };
    const float FIXED_DELTA_TIME = 1.0f / 30.0f; // Fixed Time Update rate.
}


void update(sf::Transformable& player, float delta)
{
    sf::Vector2f velocity;
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W))
        velocity.y = -MOVE_SPEED;
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A))
        velocity.x = -MOVE_SPEED;
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::S))
        velocity.y = MOVE_SPEED;
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::D))
        velocity.x = MOVE_SPEED;

    if ((velocity.y > 0.0f || velocity.y < 0.0f) && (velocity.x > 0.0f || velocity.x < 0.0f))
        velocity *= sin(45.0f);

    player.move(velocity * delta);
}

int main()
{
    sf::RenderWindow window(sf::VideoMode(1920, 1080), "Shiver", sf::Style::Default);
    // Tried with V-SYNC true and Framerate limit off.
    window.setVerticalSyncEnabled(true);
    //window.setFramerateLimit(60);

    sf::View view(sf::Vector2f(0.0f, 0.0f), VIEW_SIZE);

    sf::Clock clock;

    srand(time(NULL));

    sf::Texture treeTexture;
    if (!treeTexture.loadFromFile("Assets/evergreen-tree.png"))
        return 0;

    sf::Sprite player;
    player.setTexture(treeTexture);
    player.setTextureRect(sf::IntRect(0, 0, 48, 48));

    std::vector<sf::Sprite> trees;
    for (unsigned int i = 0; i < 100; ++i)
    {
        sf::Sprite tree;
        tree.setTexture(treeTexture);
        tree.setTextureRect(sf::IntRect(0, 0, 48, 48));
        tree.setPosition(rand() % 10 * i, rand() % 10 * i);
        trees.push_back(tree);
    }

    unsigned int fixedUpdateCount = 0;

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed || event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape)
                window.close();
            else if (event.type == sf::Event::Resized)
            {
                auto width = VIEW_SIZE.y / event.size.height * event.size.width;
                view.setSize(width, VIEW_SIZE.y);
            }
            else if (event.type == sf::Event::KeyPressed)
            {
                if (event.key.code == sf::Keyboard::F11)
                {
                    auto mode = sf::VideoMode::getDesktopMode();
                    window.create(sf::VideoMode(mode.width, mode.height), "Shiver", sf::Style::Fullscreen);
                    window.setVerticalSyncEnabled(true);

                    auto width = VIEW_SIZE.y / mode.height * mode.width;
                    view.setSize(width, VIEW_SIZE.y);
                }
                // Added a sleep to simulate a stutter in the render time.
                else if (event.key.code == sf::Keyboard::Space)
                {
                    sf::sleep(sf::seconds(0.5f));
                }
            }
        }

        auto delta = clock.restart().asSeconds();

        // If our delta time is greater than our fixed update time
        // We update at the fixed rate until we catch up.
        while (delta > FIXED_DELTA_TIME)
        {
            update(player, FIXED_DELTA_TIME);
            delta -= FIXED_DELTA_TIME;

            ++fixedUpdateCount;
        }

        update(player, delta);

        if (fixedUpdateCount > 0)
        {
            std::cout << "Fixed Update: " << fixedUpdateCount << std::endl;

            fixedUpdateCount = 0;
        }

        view.setCenter(player.getPosition());

        window.clear(sf::Color(150, 150, 150));
        window.setView(view);
        for (auto& tree : trees)
        {
            window.draw(tree);
        }
        window.draw(player);
        window.display();

    }

    return 0;
}
 

Thanks again!

2
I have been researching this issue for a couple weeks now until I finally decided to post the question myself.

I am having an issue where when I move the view the sprites in the scene sometimes stutter or "shake". It happens every few seconds or so. Sometimes there could be perfectly smooth movement then next it's stuttering like crazy.

I have looked into Fixed Time steps, interpolation, Clamping positions to pixel perfect and other such things in the various forums posts I've seen that also have the issue. Nothing seems to work.

I am working on my laptop and have tried using both integrated graphics and nvidia GPU (GeForce GTX 1050 Ti Max-Q Design)

You can see here from the video what I mean:

and here is my current code:

#include <SFML/Graphics.hpp>

#include <time.h>
#include <iostream>

sf::Vector2f linearInterpolation(sf::Vector2f start, sf::Vector2f target, float alpha)
{
    return (target * alpha + start * (1.0f - alpha));
}

sf::Vector2f PixelPerfectClamp(sf::Vector2f position, float pixelsPerUnit)
{
        sf::Vector2f inPixels((int)std::round(position.x * pixelsPerUnit), (int)std::round(position.y * pixelsPerUnit));

        return inPixels / pixelsPerUnit;
}

static const float PPU = 16.0f;

const float MOVE_SPEED = 64.0f;

int main()
{
    const std::string windowTitle{ "Demo" };
    sf::RenderWindow window(sf::VideoMode(1920, 1080), "Shiver", sf::Style::Default);
        // Tried with V-SYNC true and Framerate limit off.
    window.setVerticalSyncEnabled(true);
        //window.setFramerateLimit(60);

    sf::View view(sf::Vector2f(0.0f, 0.0f), sf::Vector2f(320, 180));

        sf::Clock clock;
        sf::Time timeSinceLastUpdate = sf::Time::Zero;
        const sf::Time TimePerFrame = sf::seconds(1.0f / 120.0f); // Fixed Time Update rate.

    srand(time(NULL));

    sf::Texture treeTexture;
    if (!treeTexture.loadFromFile("Assets/evergreen-tree.png"))
        return 0;

    sf::Sprite player;
    player.setTexture(treeTexture);
    player.setTextureRect(sf::IntRect(0, 0, 48, 48));

    std::vector<sf::Sprite> trees;
    for (unsigned int i = 0; i < 100; ++i)
    {
        sf::Sprite tree;
        tree.setTexture(treeTexture);
        tree.setTextureRect(sf::IntRect(0, 0, 48, 48));
        tree.setPosition(rand() % 10 * i, rand() % 10 * i);
        trees.push_back(tree);
    }

        // Positions for interpolation.
    sf::Vector2f previousPos = player.getPosition();
    sf::Vector2f currentPos = player.getPosition();
       
        while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed || event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape)
                window.close();
        }

                auto frameTime = clock.restart();

                if (frameTime.asSeconds() > 0.25f)
                        frameTime = sf::seconds(0.25f);

                timeSinceLastUpdate += frameTime;

                //while (timestep.isUpdateRequired()) // this is true as long as there are unprocessed timesteps.
                while (timeSinceLastUpdate >= TimePerFrame)
        {
                        timeSinceLastUpdate -= TimePerFrame;
            previousPos = currentPos;

            sf::Vector2f velocity;
                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W))
                                velocity.y = -MOVE_SPEED;
                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A))
                                velocity.x = -MOVE_SPEED;
                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::S))
                                velocity.y = MOVE_SPEED;
                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::D))
                                velocity.x = MOVE_SPEED;

                        if ((velocity.y > 0.0f || velocity.y < 0.0f) && (velocity.x > 0.0f || velocity.x < 0.0f))
                                velocity *= sin(45.0f);

                        currentPos += velocity * TimePerFrame.asSeconds();
        }

                float interpolationAlpha =      timeSinceLastUpdate / TimePerFrame;

                // Tried with and without interpolating.
                sf::Vector2f newPosition = currentPos;
        //sf::Vector2f newPosition = linearInterpolation(previousPos, currentPos, interpolationAlpha);

        player.setPosition(newPosition);
        view.setCenter(newPosition);

        window.clear(sf::Color(150, 150, 150));
        window.setView(view);
        for (auto& tree : trees)
        {
            window.draw(tree);
        }
        window.draw(player);
        window.display();

    }

    return 0;
}
 

Any help would be appreciated. I have attached the sprite image as well.

Thanks!

Pages: [1]
anything