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

Author Topic: Wrapping world view movement glitch  (Read 1973 times)

0 Members and 1 Guest are viewing this topic.

Bogdan

  • Jr. Member
  • **
  • Posts: 93
    • View Profile
Wrapping world view movement glitch
« on: April 18, 2015, 02:50:17 pm »
Hi,

I'm working on a wrapping world functionality. So far it works. But only after having moved to the left, so the "left keyboard key is pressed if condition: if(fview2>=1)" is activated at least once with the consequence that everything works perfectly forever.
But if I go only to the right and the condition as mentioned is never activated, the view becomes somehow distorted after going too far to the right. Only after having activated the "left if condition" at least once, the distortion gets away and everything is fine forever. I really don't know whats wrong. Maybe it has something to do with the fact, that the fview2 value changes to strange numbers under certain conditions when it shouldn't have (f.e. 0.09999999 instead of 0.1 or -7.30157e-008 instead of 0). Did I encounter an sfml bug?
Many thanks in advance.

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

int main()
{
    sf::RenderWindow mMainWindow(sf::VideoMode(1000,600), "Map", sf::Style::Close);
        sf::View view(sf::Vector2f(500, 300), sf::Vector2f(1000, 600));
        sf::View view2(sf::Vector2f(500, 300), sf::Vector2f(1000, 600));

    mMainWindow.setFramerateLimit(60);

        sf::Image mapimage;
        mapimage.loadFromFile("world1.jpg");  //1000*600 px
        sf::Texture maptexture;
        maptexture.loadFromImage(mapimage);
        sf::Sprite mapsprite(maptexture);
        mapsprite.setPosition(0, 0);

        sf::RectangleShape viewrect;
        viewrect.setSize(sf::Vector2f(1000, 600));
        viewrect.setPosition(0, 0);
        viewrect.setFillColor(sf::Color(250,0,0,40));

        sf::RectangleShape viewrect2;
        viewrect2.setSize(sf::Vector2f(1000, 600));
        viewrect2.setPosition(0, 0);
        viewrect2.setFillColor(sf::Color(0,0,250,40));

        float fview2 = 1;
        view2.setViewport(sf::FloatRect(fview2, 0, 1, 1));

    while (mMainWindow.isOpen())
    {
        sf::Event event;

        while (mMainWindow.pollEvent(event))
        {
            switch (event.type)
            {
            case sf::Event::Closed:
                mMainWindow.close();
                break;
            }
                }
                if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
                {
                        system("cls");
                        view.move(-100, 0);
                        fview2=fview2+0.1f;

                        if(fview2>=1)
                        {      
                                fview2=0;
                                view.reset(sf::FloatRect(900, 0, 1000, 600));
                                view2.reset(sf::FloatRect(-100, 0, 1000, 600));
                        }

                        view2.setViewport(sf::FloatRect(fview2, 0, 1, 1));
                        std::cout << "fview2 " << fview2 << std::endl;
                }
                if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
                {
                        system("cls");
                        view.move(100, 0);
                        fview2=fview2-0.1f;

                        if(fview2<-0.09)
                        {
                                fview2=0.9f;
                                view.reset(sf::FloatRect(0, 0, 1000, 600));
                        }
                        view2.setViewport(sf::FloatRect(fview2, 0, 1, 1));
                        std::cout << "fview2 " << fview2 << std::endl;
                }

        mMainWindow.clear();

                mMainWindow.setView(view);
                mMainWindow.draw(mapsprite);
                mMainWindow.draw(viewrect);

                mMainWindow.setView(view2);
                mMainWindow.draw(mapsprite);
                mMainWindow.draw(viewrect2);

        mMainWindow.display();
    }
    return 0;
}
« Last Edit: April 19, 2015, 09:10:57 am by Bogdan »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
AW: Wrapping world view movement glitch
« Reply #1 on: April 19, 2015, 04:00:20 am »
Sounds like the floating point precision issue, thus it a general programming issue.
What is "too far" by your definition?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Bogdan

  • Jr. Member
  • **
  • Posts: 93
    • View Profile
Re: Wrapping world view movement glitch
« Reply #2 on: April 19, 2015, 09:04:13 am »
With too far to the right I mean, if an fview2 value smaller than -7.45058e-008 is reached for the first time. This is when the value mentioned is reached and you hit the right key again, than you see the distortion and this distortion stays unless the left key is pressed while fview2>=1. If this condition is true at least once, than everything works fine, no matter if you go then to the right or left and so on. How can the float variable become so imprecise if I only add/subtract values of 0.1 to/from it. Maybe it has something to do with the 2-base system being unable to represent 0.1 correctly?
« Last Edit: April 19, 2015, 09:16:51 am by Bogdan »

 

anything