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

Author Topic: Elapsed Time is scary  (Read 1304 times)

0 Members and 1 Guest are viewing this topic.

Michalus

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Elapsed Time is scary
« on: July 19, 2013, 08:50:07 pm »
Why my player doesent move smooth?
        while(renderWindow->isOpen())
        {
                sf::Clock elapsedTime;

                sf::Event renderWindowEvent;
                while(renderWindow->pollEvent(renderWindowEvent))
                {
                        handleEvent(renderWindowEvent);
                }

                update(elapsedTime.getElapsedTime());

                renderWindow->clear(backgroundColor);
                render(*new sf::SpriteBatch(renderWindow));
                renderWindow->display();
        }
                if(sf::Keyboard::isKeyPressed(sf::Keyboard::D))
                {
                        pos.x += 1000 * elapsedTime.asSeconds();
                }
                if(sf::Keyboard::isKeyPressed(sf::Keyboard::A))
                {
                        pos.x -= 1000 * elapsedTime.asSeconds();
                }
 

I got the most time, if I print the time out as Seconds 3.25-e-005.
Whats that and how I can fix that?
Normal the player should in that case move 1000 pixels per seconds, but he doesent.
Please help.


Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Re: Elapsed Time is scary
« Reply #1 on: July 19, 2013, 09:26:04 pm »
Your clock only count the time your app needs to process the events.. And it seems that your `if(sf::Keyboard::isKeyPressed(` are outside the main loop.

BTW, 3.25e-005 is the scientific notation. http://www.wolframalpha.com/input/?i=3.25e-005
SFML / OS X developer

Michalus

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: Elapsed Time is scary
« Reply #2 on: July 19, 2013, 09:45:39 pm »
Ah ok now I have the mistake.
But sometimes i can see a small irregularity.
How can made it better?

Player class:
        sf::CircleShape player;
        sf::Vector2f pos;

        void handleEvent(sf::Event &renderWindowEvent)
        {
        }
        void update(sf::Time &elapsedTime)
        {
                if(sf::Keyboard::isKeyPressed(sf::Keyboard::D))
                {
                        pos.x += 10000 * elapsedTime.asSeconds();
                }
                if(sf::Keyboard::isKeyPressed(sf::Keyboard::A))
                {
                        pos.x -= 10000 * elapsedTime.asSeconds();
                }
                player.setPosition(pos);
        }
        void render(sf::SpriteBatch &spriteBatch)
        {
                spriteBatch.draw(player);
        }
 
Game Loop
sf::Clock elapsedTime;

        while(renderWindow->isOpen())
        {
                sf::Event renderWindowEvent;
                while(renderWindow->pollEvent(renderWindowEvent))
                {
                        handleEvent(renderWindowEvent);
                }

                update(elapsedTime.restart());

                renderWindow->clear(backgroundColor);
                render(*new sf::SpriteBatch(renderWindow));
                renderWindow->display();
        }
 
« Last Edit: July 19, 2013, 09:48:19 pm by Michalus »