I have this simple struct that is supposed to calculate the time difference between 2 frames:
struct Clock
{
public:
Clock() {};
~Clock() {};
double CalculateDifference()
{
previousFrame = currentFrame;
currentFrame = SDL_GetPerformanceCounter();
deltaTime = (double)((currentFrame - previousFrame) * 1000 / SDL_GetPerformanceFrequency());
return deltaTime;
}
private:
uint64_t currentFrame = SDL_GetPerformanceCounter();
uint64_t previousFrame = 0;
double deltaTime = 0.0;
};
This is how I'm using it in my game loop:
DeltaTime = clock.CalculateDifference() * 0.001; // *0.001 to convert from ms to seconds because I heard deltatime should be in seconds...?
then:
velocityY -= 0.02f * m_DeltaTime;
Player.GetPosition().y += velocityY ;
The code above occurs every frame because I am making a simple doodle jump clone wherein the player is constantly jumping from one platform to another. I am using low level OpenGL programming for flexibility and exercise. I nearly got the game done but my timestamps seems messed up as sometimes the player jumps higher in one frame than previous frame...
Can someone please help me identify the issue with this?
Thanks!