Frame skips and Object jumps problem.

Started by
22 comments, last by Sharlock93 7 years, 7 months ago

This has me stumped for quiet sometime now that I don't know what to do anymore with it.

So basically I have a ball that just moves from one side of the screen to the other, and even though the game is running upwards of 100+ FPS, I still see the the ball jump or stagger around when it moves, as if its skipping frames or something, and I don't know where the problem is, and its just killing my mood :(

Here is a video of the thing:

and here is the relevant code:


int main(int argc, char ** argv) {
    //snip some code

    glfwSetTime(0);
    double total_time = 0;
    double current_time = glfwGetTime();
    double previous_time = current_time;
    glfwSwapInterval(0);

    while(run) {
        current_time = glfwGetTime();
        gamestate.update(&gamestate, &mouse_st, current_time - previous_time);
        previous_time = current_time;

        if(!gamestate.is_init) {
            gamestate.init(&gamestate);
            gamestate.is_init = 1;
        }

        if((glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)) {
                run  = false;
                glfwSetWindowShouldClose(window, true);
        }

        glClear(GL_COLOR_BUFFER_BIT); 
        gamestate.render(&gamestate);
        glfwSwapBuffers(window);
        glfwPollEvents(); 
    }

    glfwDestroyWindow(window);
    return 0;
} 

and here is what what update and render functions do:


void render(game_state *gamestate) { //Render Function

    int vpos = gamestate->vpos_attrib_loc;
    int color = gamestate->color_attrib_loc;
    int model = gamestate->model_t_attrib_loc;

    start_node = gamestate->named_objects;
    for(int i = 0; i < gamestate->object_count; ++i) {

        switch(start_node->type) {
            case BALL: {
                ball_object *ball = (ball_object *) start_node->obj; 
                ball->circ.render(vpos, color, model);
            } break;

          //snip code

        } 

        start_node++;
    }
}


////////////////////////////Update Fucntion//////////////////////////

void update(game_state *gamestate, mouse_state *mouse, double dt)  {
    start_node = gamestate->named_objects;
    for(int i = 0; i < gamestate->object_count; ++i) {
        switch(start_node->type) {
            case BALL: {
                ball_object *ball = (ball_object *) start_node->obj; 

                ball->circ.move_position((float)( ball->velocity*dt ));

                if(ball->circ.get_position().y < -gamestate->window_width_height.y/2.0)
                    ball->circ._direction.y = 1; 

                if(ball->circ.get_position().y > gamestate->window_width_height.y/2.0)
                    ball->circ._direction.y = -1; 

                if(ball->circ.get_position().x < -gamestate->window_width_height.x/2.0)
                    ball->circ._direction.x = 1; 

                if(ball->circ.get_position().x > gamestate->window_width_height.x/2.0)
                    ball->circ._direction.x = -1;
            } break;
        } 

        start_node++;
    }

}
Advertisement

Please post the full header and cpp of the ball class. I suspect that there's an integer type being used where a real type should be used, but there's too much omitted here to know.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

because the ball class is too fucked and contains too much, I took out the important parts of it and still the problem existed, here is the code:


vec2 data[32];
GLuint vbo;
mat4 transforms;
vec2 pos(-250,-250);
float velocity = 20;


int main(int argc, char ** argv) {
 
    glfwSetTime(0);
    double total_time = 0;
    double current_time = glfwGetTime();
    double previous_time = current_time;

    glfwSwapInterval(0);

    pos = vec2(0, 0);
    float step = 360.0/CIRCLE_SEGMENTS;

    for(int i = 0; i < CIRCLE_SEGMENTS; i++) {
        data[i] = vec2( (float)( 15*cos(i*step*torad) + pos.x ),(float) ( 15*sin(i*step*torad) + pos.y));
    }

    glGenBuffers(1, &vbo);
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glBufferData(GL_ARRAY_BUFFER, sizeof(data), data, GL_DYNAMIC_DRAW);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    transforms = mat4(1);


    while(run) {
        current_time = glfwGetTime();
        
        vec2 diff = velocity*(current_time - previous_time)*vec2(1, 1);
        pos = pos + diff;
        transforms = shatranslate(diff.x, diff.y, 0)*transforms;
        glUniformMatrix4fv(3, 1, GL_TRUE, &transforms[0][0]);

        previous_time = current_time;

        if((glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)) {
                run  = false;
                glfwSetWindowShouldClose(window, true);
        }

        glClear(GL_COLOR_BUFFER_BIT);

        glBindBuffer(GL_ARRAY_BUFFER, vbo);
        glEnableVertexAttribArray(0);
        glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);
        glDrawArrays(GL_TRIANGLE_FAN, 0, 32);

        glfwSwapBuffers(window);
        glfwPollEvents(); 
    }
    glfwDestroyWindow(window);
    return 0;
}

Thank you for taking time to answer.

This is not the ball class...

because the ball class is too fucked and contains too much

Maybe a bit of refactoring, then try again to see if the problem is still there ?

Maybe a bit of refactoring, then try again to see if the problem is still there ?

the code I posted is literally everything that happens, its the data creation, the update and rendering all in the same place, and that is my main loop, the problem still happens even with that code.

Hmmm, maybe I'm just trying to look for the (over) simple solution, but is it possibly a vsync problem? I see you have vsync switched off, i.e. glfwSwapInterval(0);

What happens if you switch it on by setting it to 1? It should limit the frame rate to 60 fps sure, but does it cure the problem? Probably not the solution but can't see any other obvious issues at first glance! The only other mystery to me is this 'shatranslate' function which I've never used before!

Is vec2 comprised of floats or ints? If it's ints, that could cause the problem (as Khatharr was mentioning).

Also, I would highly recommend you Fix Your Timestep.

Hello to all my stalkers.

Hmmm, maybe I'm just trying to look for the (over) simple solution, but is it possibly a vsync problem? I see you have vsync switched off, i.e. glfwSwapInterval(0);

What happens if you switch it on by setting it to 1? It should limit the frame rate to 60 fps sure, but does it cure the problem? Probably not the solution but can't see any other obvious issues at first glance! The only other mystery to me is this 'shatranslate' function which I've never used before!

Yeah Vsync doesn't solve anything, and shatranslate is just a making a translation matrix. just my own implementation.


//translation
mat4 shatranslate(float x, float y, float z) {
    return mat4(1.0, 0.0, 0.0, x,
                0.0, 1.0, 0.0, y,
                0.0, 0.0, 1.0, z,
                0.0, 0.0, 0.0, 1.0
               );
}

Is vec2 comprised of floats or ints? If it's ints, that could cause the problem (as Khatharr was mentioning).

Also, I would highly recommend you Fix Your Timestep.

vec2 is floats, and I have already seen and implemented that article, I was using a fixed timestep, and thought it might be the problem, so I went back to the more simpler and less complex variable timestep.

Does it also occur if you do not print the frame rate?

The skipping seems to coincide with the frame rate counter updating -- it could be directly linked, or just something else that happens every second.

I'd be tempted to just output the positions every frame to a log, and use information from there to set some conditional breakpoints.

Hello to all my stalkers.

Does it also occur if you do not print the frame rate?

The skipping seems to coincide with the frame rate counter updating -- it could be directly linked, or just something else that happens every second.

I'd be tempted to just output the positions every frame to a log, and use information from there to set some conditional breakpoints.

That framerate is related with the nVidia Overlay, even if I turn it off the problem still happens.

This topic is closed to new replies.

Advertisement