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

Author Topic: sf::View / sf::Shape Glitch  (Read 3884 times)

0 Members and 1 Guest are viewing this topic.

CodingMadeEasy

  • Newbie
  • *
  • Posts: 38
    • MSN Messenger - petetheheat_represent@msn.com
    • AOL Instant Messenger - 622+Amaretto+Ave
    • Yahoo Instant Messenger - petetheheat_baller@yahoo.com
    • View Profile
    • http://www.youtube.com/CodingMadeEasy
sf::View / sf::Shape Glitch
« 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();
        }
}

 

CodingMadeEasy

  • Newbie
  • *
  • Posts: 38
    • MSN Messenger - petetheheat_represent@msn.com
    • AOL Instant Messenger - 622+Amaretto+Ave
    • Yahoo Instant Messenger - petetheheat_baller@yahoo.com
    • View Profile
    • http://www.youtube.com/CodingMadeEasy
Re: sf::View / sf::Shape Glitch
« Reply #1 on: February 27, 2013, 10:34:41 pm »
bump

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11033
    • View Profile
    • development blog
    • Email
AW: sf::View / sf::Shape Glitch
« Reply #2 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?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

CodingMadeEasy

  • Newbie
  • *
  • Posts: 38
    • MSN Messenger - petetheheat_represent@msn.com
    • AOL Instant Messenger - 622+Amaretto+Ave
    • Yahoo Instant Messenger - petetheheat_baller@yahoo.com
    • View Profile
    • http://www.youtube.com/CodingMadeEasy
Re: sf::View / sf::Shape Glitch
« Reply #3 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.

cire

  • Full Member
  • ***
  • Posts: 138
    • View Profile
Re: sf::View / sf::Shape Glitch
« Reply #4 on: February 28, 2013, 05:57:59 am »
I don't see the behavior you describe when I run this code.

CodingMadeEasy

  • Newbie
  • *
  • Posts: 38
    • MSN Messenger - petetheheat_represent@msn.com
    • AOL Instant Messenger - 622+Amaretto+Ave
    • Yahoo Instant Messenger - petetheheat_baller@yahoo.com
    • View Profile
    • http://www.youtube.com/CodingMadeEasy
Re: sf::View / sf::Shape Glitch
« Reply #5 on: February 28, 2013, 08:38:13 am »
Hmmm. Maybe it's just my computer :/

 

anything