Sign in to follow this  
pescador

Faster velocity when greater FPS [solved]

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
The movement is preaty good, but I know there is a little speed difference when running in greater FPS (or slower one).

Erik Rufelt thanks, but it didn't work.

Share this post


Link to post
Share on other sites
How are you updating the car position?
Something like the following should work:

acceleration = not dependant on time

velocity += acceleration * dt
position += velocity * dt

Where dt is the frametime, (double)count/freq.

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

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