Sprite animation/timing methods

Anything related in any way to game development as a whole is welcome here. Tell us about your game, grace us with your project, show us your new YouTube video, etc.

Moderator: PC Supremacists

Post Reply
User avatar
0x0000000
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 42
Joined: Mon Feb 11, 2013 11:46 pm
Current Project: Pokemans
Favorite Gaming Platforms: PC, Gameboy Advance SP, Mac, Xbox360
Programming Language of Choice: C/++
Location: Memory Address 0x0000000 (in UTF-9 notation)
Contact:

Sprite animation/timing methods

Post by 0x0000000 »

Hello world.

I have a question: how does one time and animate a sprite?
Do I use a timer class and check if a certain amount of milliseconds have passed?
Or do I keep a counter(an integer) that increments each iteration until a certain point is reached?

I know how to clip the sprites, I just need to time the clipping correctly.

Any help and ideas will be appreciated :)
Don't ask me about my username.
> viewtopic.php?f=4&t=8520&start=999999#p85581
K-Bal
ES Beta Backer
ES Beta Backer
Posts: 701
Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:

Re: Sprite animation/timing methods

Post by K-Bal »

I'd prefer the first method. Libraries like SFML have classes for this but I'd probably just use a float variable and increment it by the frametime in every update cycle.
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Sprite animation/timing methods

Post by dandymcgee »

I calculate a time delta since the last animation frame change and check if it exceeds the desired delta each update. If it does, go to the next animation frame and reset the deltaTime to 0 subtract the expected duration from the actual duration to minimize error (as per K-Bal's suggestion). This results in a smaller margin of error than basing it solely on frame count without the asynchronicity that a timer introduces.

Psuedocode in case wording was unclear:
struct Frame {
    int duration;
    //...
}

class Sprite {
    int frameCount;
    Frame *frames;
    int frame;
    int ticks;
	
    void Update();
    //...
};

void Sprite::Update(int deltaTime)
{
    ticks += deltaTime;
    if(ticks >= frames[frame].duration) //milliseconds
    {
        ticks -= frames[frame].duration;

        //assumes 1-directional looping order (e.g. 1, 2, 3, 1, 2, 3)
        //in my actual code this is wrapped in a switch(frameOrder) where enum FrameOrder { None, Loop, Oscillation, ReverseLoop }
        frame = ++frame % frameCount;
    }
}
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
K-Bal
ES Beta Backer
ES Beta Backer
Posts: 701
Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:

Re: Sprite animation/timing methods

Post by K-Bal »

I wouldn't set it to 0 but subtract the keyframe time. This way you're introducing less error.
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Sprite animation/timing methods

Post by dandymcgee »

K-Bal wrote:I wouldn't set it to 0 but subtract the keyframe time. This way you're introducing less error.
Good idea, hadn't thought of that. Edited my pseudocode, can you double-check the logic?
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
Post Reply