Hey I'm trying to get a smooth animation for my player movement. Earlier I changed my frames to render at fixed time(1/60 sec). Well that makes my player now run at a constant speed but the animation now appear to be choppy and it seems like the player jumps from one point to another instead of smoothly transitioning between them. Below is my code and an gif animation of player movement. How do I make the animation appear smooth?
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;
}
expected_frame_end
to be outside the function? Shouldn't it be inside and get executed every time the function is called? As it stands right now, it will only ever execute once, and it is global. \$\endgroup\$ – user1118321 Dec 8 '17 at 3:30