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

Author Topic: Flickering  (Read 4888 times)

0 Members and 1 Guest are viewing this topic.

vovik_0_1

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Flickering
« 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;
}


« Last Edit: April 17, 2014, 05:51:16 pm by vovik_0_1 »

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Flickering
« Reply #1 on: April 17, 2014, 11:47:15 pm »
I watched the video twice but I didn't see any flickering. :/

Veritas

  • Jr. Member
  • **
  • Posts: 79
    • View Profile
    • Email
Re: Flickering
« Reply #2 on: April 18, 2014, 12:57:32 am »
Same here, I don't see any flickering.
"Lasciate ogni speranza, voi ch'entrate"

StDH

  • Jr. Member
  • **
  • Posts: 56
  • {⛺.⛺}
    • View Profile
    • Worst ever ^^
Re: Flickering
« Reply #3 on: April 18, 2014, 12:01:38 pm »
i see it but a bit.
are your character images all same size ?
<StDH> Imagine a girl writing you this: My farts smell good
<Slipxy> married.

ChronicRat

  • Sr. Member
  • ****
  • Posts: 327
  • C++ programmer
    • View Profile
    • My blog
Re: Flickering
« Reply #4 on: April 18, 2014, 12:03:27 pm »
May be discrete movement?

Rhimlock

  • Jr. Member
  • **
  • Posts: 73
    • View Profile
Re: Flickering
« Reply #5 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.

vovik_0_1

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Re: Flickering
« Reply #6 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);
« Last Edit: April 23, 2014, 11:11:02 pm by vovik_0_1 »

Rhimlock

  • Jr. Member
  • **
  • Posts: 73
    • View Profile
Re: Flickering
« Reply #7 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);

didii

  • Full Member
  • ***
  • Posts: 122
    • View Profile
Re: Flickering
« Reply #8 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.

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Flickering
« Reply #9 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.
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor