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

Author Topic: Jerky gameplay  (Read 15755 times)

0 Members and 1 Guest are viewing this topic.

Jycerian

  • Newbie
  • *
  • Posts: 49
  • Weakness of attitude becomes weakness of character
    • View Profile
Jerky gameplay
« on: November 19, 2014, 03:23:47 pm »
Hi guys, made a small sample program. That will eventually become my second game. But I got this weird jerky game-play when moving around etc... also notice some screen-tearing and I dont get what causes this weird phenomenon.

Executable: http://puu.sh/cX70G/9dc21552d5.zip
Project Files: http://puu.sh/cX7j3/fd2d7c3238.zip

Anyone got any clue's? Is it also jerky on your computer?

const sf::Time Game::kTimePerFrame = sf::seconds(1.f / 60.f);

Game::Game()
    : window_(sf::VideoMode(720, 540), "Prototype"),
      player_("assets/textures/player.png") {
  window_.setVerticalSyncEnabled(true); // Without this there is less stuttering but also takes like 45% cpu
  camera_.AdaptSize(window_); // Simply gets window size, and set view to same size.
  camera_.Attach(player_); // Set view/camera center to player

  if (!background_texture.loadFromFile("assets/textures/background.png"))
    std::cout << "Failed to load background.png" << std::endl;

  background_.setTexture(background_texture);
}

// Logic loop is fixed at 60, render loop unlimited but limited with v-sync.
void Game::Run() {
  sf::Clock clock;
  sf::Time timeSinceLastUpdate = sf::Time::Zero;
  while (window_.isOpen()) {
    timeSinceLastUpdate += clock.restart();
    while (timeSinceLastUpdate > kTimePerFrame) {
      timeSinceLastUpdate -= kTimePerFrame;

      ProcessEvents();
      Update(kTimePerFrame);
    }
    Render();
  }
}

void Game::ProcessEvents() {
  sf::Event event;
  while (window_.pollEvent(event)) {
    if (event.type == sf::Event::Closed)
      window_.close();

    player_.ProcessEvents(event); // Player events like walking...
  }
}

void Game::Update(const sf::Time& dt) {
  player_.Update(dt);
  camera_.Update(dt);
}

void Game::Render() {
  window_.setView(camera_.GetView()); // Get sf::view from camera class.
  window_.clear(sf::Color(15, 15, 15, 255));
  window_.draw(background_);
  window_.draw(player_);
  window_.display();
}
 

The camera class and player class are very simple so I didn't include them. Could the problem be in this code? I also have an old Asus k52n laptop.
« Last Edit: November 19, 2014, 04:02:22 pm by Jycerian »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Jerky gameplay
« Reply #1 on: November 19, 2014, 03:38:45 pm »
Please reproduce it with a minimal complete example and give details. The reason why I'm saying this because "jerky gameplay" can mean several things and result from various reasons, including bugs in your own code, e.g. bad time handling or too low framerate.

You can use VSync against screen tearing.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Jerky gameplay
« Reply #2 on: November 19, 2014, 04:02:04 pm »
Here is a small example that should run reasonably smooth and without tearing. Does it?

Jycerian

  • Newbie
  • *
  • Posts: 49
  • Weakness of attitude becomes weakness of character
    • View Profile
Re: Jerky gameplay
« Reply #3 on: November 19, 2014, 04:07:13 pm »
Here is a small example that should run reasonably smooth and without tearing. Does it?

Also pretty jerky on my machine, windows 7. Can you run my executable smoothly?
« Last Edit: November 19, 2014, 04:24:43 pm by Jycerian »

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Jerky gameplay
« Reply #4 on: November 19, 2014, 05:10:37 pm »
Can you run my executable smoothly?
No. I don't have access to a Windows machine.
« Last Edit: November 19, 2014, 11:12:30 pm by Jesper Juhl »

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Jerky gameplay
« Reply #5 on: November 19, 2014, 09:00:01 pm »
Here is a small example that should run reasonably smooth and without tearing. Does it?
Just tried this out. Needs more balls  :P
The ball doesn't collide properly with the window edge. I suggest you change the ball's origin from:
ball.setOrigin(ball_radius / 2, ball_radius / 2);
to
ball.setOrigin(ball.getRadius(), ball.getRadius());
to correctly use the filled circle's radius (ball_radius - 4) to position the origin.

An easy oversight to make  ;)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Jycerian

  • Newbie
  • *
  • Posts: 49
  • Weakness of attitude becomes weakness of character
    • View Profile
Re: Jerky gameplay
« Reply #6 on: November 19, 2014, 09:58:00 pm »
Here is a small example that should run reasonably smooth and without tearing. Does it?
Just tried this out. Needs more balls  :P
The ball doesn't collide properly with the window edge. I suggest you change the ball's origin from:
ball.setOrigin(ball_radius / 2, ball_radius / 2);
to
ball.setOrigin(ball.getRadius(), ball.getRadius());
to correctly use the filled circle's radius (ball_radius - 4) to position the origin.

An easy oversight to make  ;)

Please keep it on-topic ;) that is not my example hihi.

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Jerky gameplay
« Reply #7 on: November 20, 2014, 01:24:40 am »
Please keep it on-topic
Sure. Post a complete and minimal example and I'll test it  :P
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Jycerian

  • Newbie
  • *
  • Posts: 49
  • Weakness of attitude becomes weakness of character
    • View Profile
Re: Jerky gameplay
« Reply #8 on: November 23, 2014, 07:26:37 pm »
Also has this weird jerky movement and screen tearing in another game I made but I decided to ignore it. Now I finally want to fix it somehow so I am able to get smooth gameplay in my next game.

I have an Asus k52n laptop running windows 7 64bit.

Cpu: AMD Athlon X2 P320 2,1GHz
Memory: 4 GB
Videocard: AMD Radeon HD 4200

Example:

#include <SFML/Graphics.hpp>

// 1 second / 60
const sf::Time timePerFrame = sf::seconds( 1.f / 60.f );

int main() {
  // Init window
  sf::RenderWindow window( sf::VideoMode( 720, 540 ), "Demo" );
  window.setVerticalSyncEnabled( true );

  // Init variables
  float playerSpeed = 200.f;
  bool isMovingUp = false;
  bool isMovingDown = false;
  bool isMovingLeft = false;
  bool isMovingRight = false;

  // Load images
  sf::Texture background_tex;
  if ( !background_tex.loadFromFile( "background.png" ) ) {
    // Error handling..
  }

  sf::Texture player_tex;
  if ( !player_tex.loadFromFile( "player.png" ) ) {
    // Error handling..
  }

  // Setup sprites
  sf::Sprite background;
  background.setTexture( background_tex );

  sf::Sprite player;
  player.setTexture( player_tex );
  player.setOrigin( player.getLocalBounds().width / 2,
    player.getLocalBounds().height / 2 );

  // Create view and set size equal to window
  sf::View camera;
  camera.setSize( sf::Vector2f(window.getSize()) );

  // Main loop, update at 60. Render at unlimited but locked
  // with v-sync at 60.
  sf::Clock clock;
  sf::Time timeSinceLastUpdate = sf::Time::Zero;
  while ( window.isOpen() ) {
    timeSinceLastUpdate += clock.restart();
    while ( timeSinceLastUpdate > timePerFrame ) {
      timeSinceLastUpdate -= timePerFrame;

      // event handling
      sf::Event event;
      while ( window.pollEvent( event ) ) {
        if ( event.type == sf::Event::Closed )
          window.close();

        if ( event.type == sf::Event::KeyPressed ) {
          if ( event.key.code == sf::Keyboard::Up )
            isMovingUp = true;
          else if ( event.key.code == sf::Keyboard::Down )
            isMovingDown = true;
          else if ( event.key.code == sf::Keyboard::Left )
            isMovingLeft = true;
          else if ( event.key.code == sf::Keyboard::Right )
            isMovingRight = true;
        }

        if ( event.type == sf::Event::KeyReleased ) {
          if ( event.key.code == sf::Keyboard::Up )
            isMovingUp = false;
          else if ( event.key.code == sf::Keyboard::Down )
            isMovingDown = false;
          else if ( event.key.code == sf::Keyboard::Left )
            isMovingLeft = false;
          else if ( event.key.code == sf::Keyboard::Right )
            isMovingRight = false;
        }
      }

      // update logic
      camera.setCenter( player.getPosition() );

      sf::Vector2f movement( 0.f, 0.f );
      if ( isMovingUp )
        movement.y -= playerSpeed;
      if ( isMovingDown )
        movement.y += playerSpeed;
      if ( isMovingLeft )
        movement.x -= playerSpeed;
      if ( isMovingRight )
        movement.x += playerSpeed;

      player.move( movement * timePerFrame.asSeconds());
    }

    // render
    window.setView( camera );

    window.clear();
    window.draw( background );
    window.draw( player );
    window.display();
  }
  return 0;
}
 
« Last Edit: November 23, 2014, 07:34:19 pm by Jycerian »

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Jerky gameplay
« Reply #9 on: November 24, 2014, 12:17:50 am »
I think I see what you mean by "jerky". This has been posted about on the forum before, actually. I'm not sure what the solution was, if there was one.

I have a request for you. Add this line of code:
window.setTitle("*");
(I put it before window.setView(camera);)
Let me know if anything changes.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Jycerian

  • Newbie
  • *
  • Posts: 49
  • Weakness of attitude becomes weakness of character
    • View Profile
Re: Jerky gameplay
« Reply #10 on: November 24, 2014, 09:52:07 am »
I think I see what you mean by "jerky". This has been posted about on the forum before, actually. I'm not sure what the solution was, if there was one.

I have a request for you. Add this line of code:
window.setTitle("*");
(I put it before window.setView(camera);)
Let me know if anything changes.

Yeah I searched the forum but could't really find anything that would help me because everyone just tells em to enable v-sync..which I have. Also the setTitle did not change anything :C

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Jerky gameplay
« Reply #11 on: November 24, 2014, 02:34:23 pm »
Which version of SFML are you using? If it's not the latest revision from GitHub, please update it. There have been some changes to frame handling in the not-so-distant past.

And you should really reduce the example to a minimal one as much as you can. If this is a bug, we don't care about how the player moves or what textures are loaded... Since you say that Jesper's code shows the problem too, please reduce his code as much as possible.
« Last Edit: November 24, 2014, 02:37:07 pm by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Jerky gameplay
« Reply #12 on: November 24, 2014, 11:22:01 pm »
everyone just tells em to enable v-sync..which I have.
If it's a problem that v-sync that could solve, it's possible that v-sync is not getting used. Remember that SFML just tells your graphics driver that it thinks it should use v-sync; it's up to your graphics card's drivers whether or not to listen (there are often settings to tell the driver to listen to apps - you may need to make sure they aren't getting overridden).

the setTitle did not change anything
No worries. I accidentally found that it almost perfectly displayed motion when the title was being set to the star but jumpy and jerky when it was being set to nothing, or not being set at all. Strange effect that I can't explain but I suspect it may be something to do with forcing something to update somewhere. I just don't know. Thanks for trying it, anyway  :D
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Jycerian

  • Newbie
  • *
  • Posts: 49
  • Weakness of attitude becomes weakness of character
    • View Profile
Re: Jerky gameplay
« Reply #13 on: November 25, 2014, 09:58:48 am »
everyone just tells em to enable v-sync..which I have.
If it's a problem that v-sync that could solve, it's possible that v-sync is not getting used. Remember that SFML just tells your graphics driver that it thinks it should use v-sync; it's up to your graphics card's drivers whether or not to listen (there are often settings to tell the driver to listen to apps - you may need to make sure they aren't getting overridden).

Thanks, didn't think about it that much. Checked my AMD settings and it's set to off unless specified by an application so that should be ok I suppose.

Jycerian

  • Newbie
  • *
  • Posts: 49
  • Weakness of attitude becomes weakness of character
    • View Profile
Re: Jerky gameplay
« Reply #14 on: December 10, 2014, 03:16:13 pm »
Still have not resolved this problem. I have no clue what causes this. So here a very minimal example. It is harder to see with shapes instead of images but it is still there, some sort of image/sprite tearing (maby screen tearing but the background is not moving so can't confirm if it applies to everything) and sometimes it is a bit 'jerky'

EDIT: Updating SFML (10-12-2014) from GitHub also did not help.

#include <SFML/Graphics.hpp>

int main() {
  sf::RenderWindow window(sf::VideoMode(800, 600), "Jycerian", sf::Style::Close);
  window.setFramerateLimit(60);

  bool isMovingUp = false;
  bool isMovingDown = false;
  bool isMovingLeft = false;
  bool isMovingRight = false;

  sf::RectangleShape rect;
  rect.setSize(sf::Vector2f(32, 16));
  rect.setFillColor(sf::Color::Red);
  rect.setOutlineThickness(3);
  rect.setOutlineColor(sf::Color::Black);

  float moveSpeed = 100.f;

  sf::Clock clock;
  while (window.isOpen()) {
    sf::Event event;
    while (window.pollEvent(event)) {
      if (event.type == sf::Event::Closed)
        window.close();

      if (event.type == sf::Event::KeyPressed) {
        if (event.key.code == sf::Keyboard::W)
          isMovingUp = true;
        if (event.key.code == sf::Keyboard::S)
          isMovingDown = true;
        if (event.key.code == sf::Keyboard::A)
          isMovingLeft = true;
        if (event.key.code == sf::Keyboard::D)
          isMovingRight = true;
      }

      if (event.type == sf::Event::KeyReleased) {
        if (event.key.code == sf::Keyboard::W)
          isMovingUp = false;
        if (event.key.code == sf::Keyboard::S)
          isMovingDown = false;
        if (event.key.code == sf::Keyboard::A)
          isMovingLeft = false;
        if (event.key.code == sf::Keyboard::D)
          isMovingRight = false;
      }
    }

    sf::Vector2f velocity(0, 0);
    if (isMovingUp)
      velocity.y = -moveSpeed;
    if (isMovingDown)
      velocity.y = moveSpeed;
    if (isMovingLeft)
      velocity.x = -moveSpeed;
    if (isMovingRight)
      velocity.x = moveSpeed;

    rect.move(velocity * clock.restart().asSeconds());

    window.clear(sf::Color::White);
    window.draw(rect);
    window.display();
  }
}
« Last Edit: December 10, 2014, 03:42:22 pm by Jycerian »