Archived

This topic is now archived and is closed to further replies.

Ronin Magus

Having trouble w/ linear interp algorithm

Recommended Posts

I'm having some trouble with this linear interpolation algorithm (at least that's what I think it is.. hehe) Nothing on my screen moves!! I'm using SDL, BTW.
          

// move 32 pixels per second if vector value is 1

// if vector value is .5, then move 16 per second,

// etc etc etc

const int PIXEL_MOVE = 32;

struct Position
{
    int x, y;
};

struct FloatPosition
{
    float x, y;
};

struct object
{
    Position pos;
    FloatPosition vector;
    ...
    void Move();
}

void object::Move()
{
    static Uint32 lasttime = 0;
    Uint32 newtime = SDL_GetTicks();

    // log the times for debug purposes   

    LogFile << newtime;
    LogFile << lasttime;

    pos.x += int((newtime - lasttime) / float(1000)) * (vector.x * PIXEL_MOVE));
    pox.y += int((newtime - lasttime) / float(1000)) * (vector.x * PIXEL_MOVE));

    //update the time

    lasttime = newtime;
}

          
In looking at the log file I see that newttime and lasttime are almost always within 1ms of each other. So my algorithm would always return something like .001 and when typecasted to int that is just rounded to 0. [edit] Even when I force it it wait at least 200ms, still nothing moves. I think there's a problem w/ my algorithm But I also notice that there is not a 200ms difference between newtime and lasttime, even when I force it to wait, there's still just 1ms. I wonder why? [/edit] Any suggestions? [edited by - Ronin Magus on November 2, 2002 10:31:48 PM]

Share this post


Link to post
Share on other sites
hey,

um it''d help if i knew what that SDL_GetTicks() did?

so fill me in on that and i can help..

also..
the code

pos.x += int((newtime - lasttime) / float(1000)) * (vector.x * PIXEL_MOVE));
pox.y += int((newtime - lasttime) / float(1000)) * (vector.x * PIXEL_MOVE));

shouldn''t ^
*the second pos.. not pox .. be:
be vector.y * PIXEL_MOVE

another thing.. does position have to be int?

cheers,
Fonz

Share this post


Link to post
Share on other sites