SFML community forums

Help => General => Topic started by: CodingMadeEasy on February 27, 2013, 09:34:15 am

Title: sf::View / sf::Shape Glitch
Post by: CodingMadeEasy on February 27, 2013, 09:34:15 am
When the view's center is set to the players position each frame, the player movement is fine but when the view is not centered to the players position, the rectangle jitters back and forth when receiving input commands.

Here's my code

#include<SFML/Graphics.hpp>
#include<string>
#include<iostream>

int main()
{
        sf::Vector2i screenDimensions(800, 600);

        sf::RenderWindow Window;
        Window.create(sf::VideoMode(screenDimensions.x, screenDimensions.y), "My First Sfml Game");

        //Window.setKeyRepeatEnabled(false);

        Window.setVerticalSyncEnabled(true);

        sf::Texture bTexture;
        sf::Sprite bImage;

        if(!bTexture.loadFromFile("Background.png"))
                std::cout << "Could not load backgorund image" << std::endl;

        bImage.setTexture(bTexture);

        sf::RectangleShape rect(sf::Vector2f(20, 20));
        rect.setFillColor(sf::Color::Red);

        sf::Clock clock;

        float moveSpeed = 2000.0f;

        sf::View view;
        view.reset(sf::FloatRect(0, 0, screenDimensions.x, screenDimensions.y));
        view.setViewport(sf::FloatRect(0, 0, 1.0, 1.0));

        sf::Vector2f position(screenDimensions.x / 2, screenDimensions.y / 2);
        view.setCenter(position);

        while(Window.isOpen())
        {
                clock.restart();
                sf::Event Event;
                while(Window.pollEvent(Event))
                {
                        switch(Event.type)
                        {
                        case sf::Event::Closed:
                                Window.close();
                                break;
                        case sf::Event::KeyPressed:
                                if(Event.key.code == sf::Keyboard::Escape)
                                        Window.close();
                                break;
                        }
                }
       
                if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
                        rect.move(moveSpeed * clock.getElapsedTime().asSeconds(), 0);
                else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
                        rect.move(-moveSpeed * clock.getElapsedTime().asSeconds(), 0);

                if(rect.getPosition().x + 10 > screenDimensions.x / 2)
                        position.x = rect.getPosition().x + 10;
                if(rect.getPosition().y + 10 > screenDimensions.y / 2)
                        position.y = rect.getPosition().y + 10;

                view.setCenter(position);
                Window.setView(view);

                Window.draw(bImage);
                Window.draw(rect);
                Window.display();
                Window.clear();
        }
}

 
Title: Re: sf::View / sf::Shape Glitch
Post by: CodingMadeEasy on February 27, 2013, 10:34:41 pm
bump
Title: AW: sf::View / sf::Shape Glitch
Post by: eXpl0it3r on February 28, 2013, 12:39:23 am
I can't test the code, but from your description things don't really make sense. Why would there be a difference if you move the view with the player as center or a few pixels off?
Also what exactly do you understand under 'jitter'?

Are your player and view values integers?
Title: Re: sf::View / sf::Shape Glitch
Post by: CodingMadeEasy on February 28, 2013, 04:18:52 am
I'm not to sure as to why it's jittery when the view is not centered to the player :/. The view and the players position are bother determined as floats. And what I mean by jittering is that the player movement isn't smooth. It'll move in one direction and then slightly move back in the opposite direction. It's like it's staggering if that makes any sense.
Title: Re: sf::View / sf::Shape Glitch
Post by: cire on February 28, 2013, 05:57:59 am
I don't see the behavior you describe when I run this code.
Title: Re: sf::View / sf::Shape Glitch
Post by: CodingMadeEasy on February 28, 2013, 08:38:13 am
Hmmm. Maybe it's just my computer :/