grep
grep

Reputation: 4016

framerate independent animation issues

For whatever reason, my character moves faster going left than right. It's at least twice as fast.

Here is the portion of code that checks the character movement:

if(moving)
{
    Uint32 delta = g_delta_get_ticks();
    if(!g_holding_a) 
    {
        position.x += accel * (delta / 1000.f);
        facing = right;
    }
    if(!g_holding_d) 
    {
        position.x -= accel * (delta / 1000.f);
        facing = left;
    }
}

Now, my first guess would be the timer is acting funny, but, even placing in static values like accel * (50 / 1000.f) results in the same behavior.

Now, If I just tell the positions to be += and -= 2, it works just fine. Any ideas?

Upvotes: 0

Views: 152

Answers (1)

rob mayoff
rob mayoff

Reputation: 385600

I suspect truncation is causing your trouble. When you say some_int += some_float (or some_int -= some_float), the arithmetic is done in floating-point, then truncated to an integer. So if some_int is 100 and some_float is 1.5, then after some_int += some_float, you get 101 (truncated from 101.5). But after some_int -= some_float, you get 98 (truncated from 98.5).

Since you're using GLuint for position.x, try truncating the adjustment into a GLuint before adding or subtracting it:

GLuint d = accel * (delta / 1000.f);
if (!g_holding_a) {
    position.x += d;
    facing = right;
}
if (!g_holding_d) {
    position.x -= d;
    facing = left;
}

Upvotes: 1

Related Questions