I'm using GLFW for my window and input related work. I'm calculating delta time and multiplying it with my player speed and then finally adding it to the current position of the player. The thing is that, though it makes my motion frame independent the player movement appears to slow and speed up interchangeably. Well the reason would be the different values of delta time at each frame. What should I do to smooth out this animation?
Here is the relevant code, and a capture of what's going on:
float deltaTime = 0;
float oldTime = 0;
//The direction in which the player will move
int playerX = 0;
int playerY = 0;
//callback method for keyboard events
void keyboard_callback(GLFWwindow* window, int key, int scancode, int action, int mods) {
if (key == GLFW_KEY_W && action == GLFW_REPEAT) {
playerX = 0; playerY = 1;
}
if (key == GLFW_KEY_S && action == GLFW_REPEAT) {
playerX = 0; playerY = -1;
}
if (key == GLFW_KEY_D && action == GLFW_REPEAT) {
playerX = 1; playerY = 0;
}
if (key == GLFW_KEY_A && action == GLFW_REPEAT)
{
playerX = -1; playerY = 0;
}
}
//update player's position
void update() {
player->move(playerX, playerY, TIME_PER_FRAME);
}
//set up glfw keyboard callback function and other stuff
void init() {
glClearColor(0, 0, 0, 0);
player->bindVertexAttributes(shader.getAttributeLocation("position"));
glfwSetKeyCallback(window->getGLFWWindow(), keyboard_callback);
}
//wait logic
float expected_frame_end = glfwGetTime() + TIME_PER_FRAME;
void wait() {
while (glfwGetTime() < expected_frame_end) {
}
expected_frame_end += TIME_PER_FRAME;
playerX = playerY = 0;
}
//rendering function
void render() {
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
player->setUniformMatrixLocation(shader.getUniformLocation("projectionMatrix"), shader.getUniformLocation("transformationMatrix"));
shader.useProgram();
update();
player->render();
wait();
shader.stopProgram();
}
//main function
int main(int argc, char** argv) {
init();
//main loop
while (!glfwWindowShouldClose(window->getGLFWWindow())) {
render();
window->swapBuffers();
glfwPollEvents();
}
glfwTerminate();
return 0;
}
delta
jitter? Could it be that you are querying system time with not enough precision? There are several ways to query the time. Which one do you use? \$\endgroup\$