Hello everyone! I'm new to SFML, but come from using XNA, Allegro, and some SDL back in the day.
I've always been use to just having a function that kept my games at 60 FPS lock, and all game play, logic, rendering was done in the same loop, with no independent timers, 100% based on frames rendered.
I'm extremely interested in duplicating the Time Step as per the article: http://gafferongames.com/game-physics/fix-your-timestep/ but I'm running into some issues as I've never used Delta Time, or any methods prior, everything was handled for me before.
NOTE: I turned on VSYNC in my Window Class to keep the rate at 60 FPS.
My current code for main.cpp:
#include "Game.h"
#include <iostream>
int main()
{
// Time Test
sf::Clock MainClock;
double Time = 0.0;
const double DeltaTime = 0.01;
double CurrentTime = MainClock.getElapsedTime().asSeconds();
double Accumulator = 0.0;
Game GameTest;
while (GameTest.IsWindowOpen())
{
double NewTime = MainClock.getElapsedTime().asSeconds();
double FrameTime = NewTime - CurrentTime;
CurrentTime = NewTime;
Accumulator += FrameTime;
while (Accumulator >= DeltaTime)
{
// Game Loop
GameTest.Input(DeltaTime);
GameTest.Logic(DeltaTime);
// AI
// Physics
Accumulator -= DeltaTime;
Time += DeltaTime;
}
// Render Graphics
GameTest.Render();
// FPS - Shows in Console Window
std::cout << "FPS: " << 1.0f / FrameTime << std::endl;
}
return 0;
}
My game.cpp code for moving the sprite:
// Input
void Game::Input(double TempUpdatesPerSecond)
{
// Keyboard Movement for guy1 --- TEST !!!
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
{
guy1.move(0, -32 * TempUpdatesPerSecond);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
{
guy1.move(0, 32 *TempUpdatesPerSecond);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
{
guy1.move(32 * TempUpdatesPerSecond, 0);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
{
guy1.move(-32 * TempUpdatesPerSecond, 0);
}
}
In the article I'm having trouble understanding what I do with Time? Why is it passed into my Update function along with Delta Time? Do I have my variables set up properly?
From the Article - Second Last Code Snip:
double t = 0.0;
const double dt = 0.01;
double currentTime = hires_time_in_seconds();
double accumulator = 0.0;
while ( !quit )
{
double newTime = hires_time_in_seconds();
double frameTime = newTime - currentTime;
currentTime = newTime;
accumulator += frameTime;
while ( accumulator >= dt )
{
integrate( state, t, dt );
accumulator -= dt;
t += dt;
}
render( state );
}
I also have a cout to show how many frames per second are being rendered out to make sure it matches with vsync.
Since DeltaTime = 0.01; does this mean I'm moving at 0.01 * units per frame, at a maximum of 0.06 * units per second assuming steady 60 FPS with Vsync?
I would like to get my code working properly as to match the second last part of the article before learning how to do the final step. I also have no idea about interpolation.
I was also reading online that Delta Time is a very poor technique to use, and you should only program based on a fixed amount of ticks? My issue is making sure my games run good on 60 Hz or 200+ hz monitors while keeping the logic at a fixed update rate, and using any left over time to render as many frames as possible, I just need some guidance along the way.
Thank you!