[ad_1]
This has been bugging me for few days now. I am attempting to cease motion jittering in my sport. I’ve regarded in the direction of semi and glued time steps – transferring in the direction of the latter beneath so I can actually perceive (as I hope to create multiplayer video games in future).
I’ve a really primary arrange with a ball that strikes at a continuing pace, until the person tries to manage it by urgent P to takeover. It is at the moment received a hard-coded 60 FPS worth. When the gathered time reaches that, the replace known as and the ball is moved.
My concern is that I get occasional stuttering throughout motion and I actually cannot grasp why. Is there something you guys would advocate to raised what I’ve beneath?
Thanks prematurely!
#embody <iostream>
#embody <SFML/Graphics.hpp>
int predominant() {
sf::RenderWindow window(sf::VideoMode(1920, 1080), "Ping Pong");
// window.setFramerateLimit(60);
// window.setKeyRepeatEnabled(false);
sf::Font font;
font.loadFromFile("arial.ttf");
sf::Textual content textual content;
textual content.setFillColor(sf::Shade::Inexperienced);
textual content.setCharacterSize(72);
textual content.setFont(font);
textual content.setString("whats up: ");
sf::CircleShape circle;
circle.setRadius(20.0f);
circle.setOrigin(circle.getRadius(), circle.getRadius());
circle.setPosition(window.getSize().x/2, window.getSize().y/2);
float TIME_PER_FRAME = 1.0f / 60.0f;
sf::Clock clock;
float lastFrameTime = clock.getElapsedTime().asSeconds();
float frameStartTime;
float frameTime;
float accumulator;
int updateCount;
bool movable = false;
whereas(window.isOpen()) {
updateCount = 0;
frameStartTime = clock.getElapsedTime().asSeconds();
frameTime = frameStartTime - lastFrameTime;
accumulator += frameTime;
lastFrameTime = frameStartTime;
// occasions
sf::Occasion occasion;
whereas(window.pollEvent(occasion)) {
if (occasion.kind == sf::Occasion::KeyPressed) {
if (occasion.key.code == sf::Keyboard::Escape) {
window.shut();
}
//make ball controllable by participant
if (occasion.key.code == sf::Keyboard::P) {
movable = (movable) ? false : true;
}
}
}
// replace
whereas (accumulator >= TIME_PER_FRAME) {
//character movable by WASD
if (movable) {
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) {
circle.transfer(sf::Vector2f(200*TIME_PER_FRAME,0));
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) {
circle.transfer(sf::Vector2f(-200*TIME_PER_FRAME,0));
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) {
circle.transfer(sf::Vector2f(0,-200*TIME_PER_FRAME));
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) {
circle.transfer(sf::Vector2f(0,200*TIME_PER_FRAME));
}
}
//automated motion to the correct
if (!movable) {
circle.transfer(sf::Vector2f(200*TIME_PER_FRAME,0));
}
//if ball goes off the correct of the display screen, seem on the left aspect
sf::Vector2f c_position = circle.getPosition();
if (c_position.x - circle.getRadius() > window.getSize().x) {
circle.setPosition(sf::Vector2f(0-circle.getRadius(), c_position.y));
}
accumulator -= TIME_PER_FRAME;
updateCount++;
}
//render
window.clear();
window.draw(circle);
window.draw(textual content);
window.show();
std::cout << "up to date " << updateCount << std::endl;
}
return 0;
}
```
[ad_2]