Sign in to follow this  

Faster velocity when greater FPS [solved]

This topic is 2951 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi. I guess you know what this topic is about. Well, My little 2d game experiment is very simple. Just moving a centered car in a translated floor. The problem is: when the framerate is greater, the car moves faster. Take a look:
// this is check control function. When a certein key is pressed, its state is true.

VOID CheckControl()
{
    if (control.up)
    {   
        if (floor.velocity > -0.03) floor.velocity -= 0.01*fps.countd/fps.freq;
    }
    else if (floor.velocity < 0.0) floor.velocity += 0.005*fps.countd/fps.freq;
        
    if (control.down)
    {   
        if (floor.velocity < 0.01) floor.velocity += 0.01*fps.countd/fps.freq;
    }
    else if (floor.velocity > 0.0) floor.velocity -= 0.005*fps.countd/fps.freq;
        
    if (control.left)
    {       
        floor.direction -= 2*floor.velocity;
        car.direction = floor.direction*RADTODEGREES;
    }
        
    if (control.right)
    {   
        floor.direction += 2*floor.velocity;
        car.direction = floor.direction*RADTODEGREES;
    }
}

...

// this is render function

VOID Render()
{
    CloseFPS();
    OpenFPS();
    
    CheckControl();
    
    glClear(GL_COLOR_BUFFER_BIT);
    
    RenderObjects();
}

...

// and this is how I calculate the FPS:

VOID OpenFPS()
{
    QueryPerformanceFrequency((LARGE_INTEGER*) &fps.freq);
    QueryPerformanceCounter((LARGE_INTEGER*) &fps.count1);
}

VOID CloseFPS()
{
    QueryPerformanceCounter((LARGE_INTEGER*) &fps.count2);
    
    fps.countd = fps.count2 - fps.count1;
    fps.fps = (GLuint) fps.freq / fps.countd;
    sprintf(fps.text, "FPS: %0.3d, Delay: %d", fps.fps, fps.countd);
}


PS: fps.freq is not the real FPS, but the Windows PerformanceCounter frequency. [Edited by - pescador on November 12, 2009 8:13:37 AM]

Share this post


Link to post
Share on other sites
You also need to multiply with the frametime in floor.direction += 2*floor.velocity;. The velocity increases with acceleration, which depends on time, and position increases with velocity, which also depends on time. So you have time^2 in there, and therefore need to divide with FPS in both steps.

Share this post


Link to post
Share on other sites
Sorry. Here is the problem:


glTranslatef(cos(object.direction) * object.velocity, sin(object.direction) * object.velocity, 0);

So the modelview matrix was "refreshed" more times when greater FPS, then the car was moving faster.
Just chagend the translate function to:
glTranslatef(object.xpos, object.ypos, 0);

Thankyou all.

Share this post


Link to post
Share on other sites

This topic is 2951 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this