SFML community forums

Help => General => Topic started by: vovik_0_1 on April 17, 2014, 05:14:36 pm

Title: Flickering
Post by: vovik_0_1 on April 17, 2014, 05:14:36 pm
Hey. I'm creating a game for SFML in VS 11 and connect Box2d.
Loading map using https://github.com/fallahn/sfml-tmxloader

The problem is that when the character still image flickers.

int main()
{
        sf::Vector2i screenSize(800u, 600u);    //разрешение экрана
        sf::RenderWindow renderWindow(sf::VideoMode(screenSize.x, screenSize.y), "TMX Loader - Box2D Body Creation Example"); //Создаем экран
        renderWindow.setVerticalSyncEnabled(true); // Включаем вертикальную синхронизацию (для плавной анимации)

        sf::View view; // Камера
        view.reset(sf::FloatRect(0.0f, 0.0f, screenSize.x, screenSize.y));
        view.setViewport(sf::FloatRect(0.0f, 0.0f, 2.0f, 2.0f)); // Увеличиваем изображение на 2


        //создать карту и загрузка карту
        tmx::MapLoader ml("maps/");
        ml.Load("platformer.tmx");

        //create a box2D world
        b2World world(tmx::SfToBoxVec(sf::Vector2f(0.f, 300.f))); // Гравитация
       
        CHARACTER Player;
        Player.Create();

        AnimationManager anim;
        anim.loadAnimXML("animations/Anim1.xml",Player.texture);

        const std::vector<tmx::MapLayer>& layers = ml.GetLayers();
        for (const auto& l : layers) // &#1057;&#1084;&#1086;&#1090;&#1088;&#1080;&#1084; &#1074;&#1089;&#1077; &#1089;&#1083;&#1086;&#1080; &#1085;&#1072; &#1082;&#1072;&#1088;&#1090;&#1077;.......... l &#1090;&#1086;&#1078;&#1077; &#1089;&#1072;&#1084;&#1086;&#1077; &#1095;&#1090;&#1086; layers... & &#1091;&#1082;&#1072;&#1079;&#1072;&#1090;&#1077;&#1083;&#1100;
        {
                if (l.name == "Stat") //&#1057;&#1084;&#1086;&#1090;&#1088;&#1080;&#1084; &#1089;&#1083;&#1086;&#1081; &#1082;&#1086;&#1090;&#1086;&#1088;&#1099;&#1081; &#1085;&#1072;&#1079;&#1099;&#1074;&#1072;&#1077;&#1090;&#1089;&#1103; Static
                {
                        for (const auto& o : l.objects) // o &#1090;&#1086;&#1078;&#1077; &#1089;&#1072;&#1084;&#1086;&#1077; &#1095;&#1090;&#1086; l.objects
                        {
                                b2Body* b1 = tmx::BodyCreator::Add(o, world);
                        }
                }
                else if (l.name == "Din")
                {
                        for (const auto& o : l.objects)
                        {
                                Player.body = tmx::BodyCreator::Add(o, world, true , b2BodyType::b2_dynamicBody);
                        }
                }
        }
       
        //-----------------------------------//
        sf::Clock clock;

        while(renderWindow.isOpen())
        {
               
                //poll input
                sf::Event evt;
                while(renderWindow.pollEvent(evt))
                {
                        if (evt.type == sf::Event::Closed)      
                                renderWindow.close();
         
                }

                //update
               
                world.Step(1.0f / 60.0f, 3, 3);
                //-----------------update the keyboard--------------------------------
                        Key_Update(Player,anim);
               //----------------update the keyboard-------------------------------    
       
                sf::Vector2f pos = tmx::BoxToSfVec(Player.body->GetPosition());        
               
                //-----------------------draw-------------------------------------------
                view.setCenter(pos.x+screenSize.x/4,pos.y+screenSize.y/4);
                renderWindow.setView(view);

                renderWindow.clear(sf::Color(107,140,255));

                renderWindow.draw(ml);

                anim.draw(renderWindow,pos);                   
                renderWindow.display();
        }

        return 0;
}

http://youtu.be/EpdfeN1Ud4M
Title: Re: Flickering
Post by: G. on April 17, 2014, 11:47:15 pm
I watched the video twice but I didn't see any flickering. :/
Title: Re: Flickering
Post by: Veritas on April 18, 2014, 12:57:32 am
Same here, I don't see any flickering.
Title: Re: Flickering
Post by: StDH on April 18, 2014, 12:01:38 pm
i see it but a bit.
are your character images all same size ?
Title: Re: Flickering
Post by: ChronicRat on April 18, 2014, 12:03:27 pm
May be discrete movement?
Title: Re: Flickering
Post by: Rhimlock on April 23, 2014, 02:48:51 pm
world.Step(1.0f / 60.0f, 3, 3);

You use 1/60 as your hardcoded timestep.


So when your step doesn't need exactly 1/60 seconds you might get your flickering.

Try to fix your timesteps and see if the flickering is gone.
Title: Re: Flickering
Post by: vovik_0_1 on April 23, 2014, 10:53:18 pm
i see it but a bit.
are your character images all same size ?
No other character size.

world.Step(1.0f / 60.0f, 3, 3);

You use 1/60 as your hardcoded timestep.


So when your step doesn't need exactly 1/60 seconds you might get your flickering.

Try to fix your timesteps and see if the flickering is gone.

Change
world.Step(1.0f / 60.0f, 3, 3);
does not help.
I understand that the processing speed in Box2D and the problems SFML.
Here's what will happen if to do so
world.Step(0.5f / 60.0f, 3, 3);
http://www.youtube.com/watch?v=KCFXX8MyfPc
Title: Re: Flickering
Post by: Rhimlock on April 24, 2014, 09:46:09 am
Change
world.Step(1.0f / 60.0f, 3, 3);
does not help.
I understand that the processing speed in Box2D and the problems SFML.
Here's what will happen if to do so
world.Step(0.5f / 60.0f, 3, 3);

I meant to use a clock, to get the exact time elapsed, not just to use another hardcoded timestep.

Something like this:
sf::Clock clock;
...
   world.Step(clock.restart().getElapsedTime().asSeconds(), 3, 3);
Title: Re: Flickering
Post by: didii on April 25, 2014, 03:49:09 am
The Box2D tutorial states:
Quote
We also don't like the time step to change much. A variable time step produces variable results, which makes it difficult to debug. So don't tie the time step to your frame rate (unless you really, really have to).
Which makes sense for a physics engine.
Title: Re: Flickering
Post by: zsbzsb on April 25, 2014, 04:03:27 am
Well great idea, but either way you need to account for delta time. So either directly pass delta time to box2d or implement a fixed time step.