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

Sprite animation/timing methods

Postby 0x0000000 on Sun Oct 13, 2013 4:08 am

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 :)
User avatar
0x0000000
Chaos Rift Newbie
Chaos Rift Newbie
 
Posts: 42
Joined: Tue Feb 12, 2013 1:46 am
Location: Memory Address 0x0000000 (in UTF-9 notation)

Re: Sprite animation/timing methods

Postby K-Bal on Sun Oct 13, 2013 8:37 am

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
K-Bal
ES Beta Backer
ES Beta Backer
 
Posts: 719
Joined: Sun Mar 15, 2009 4:21 pm
Location: Germany, Aachen

Re: Sprite animation/timing methods

Postby dandymcgee on Sun Oct 13, 2013 11:56 am

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:
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
 
Posts: 4911
Joined: Tue Apr 29, 2008 4:24 pm
Location: New Hampshire

Re: Sprite animation/timing methods

Postby K-Bal on Sun Oct 13, 2013 11:59 am

I wouldn't set it to 0 but subtract the keyframe time. This way you're introducing less error.
User avatar
K-Bal
ES Beta Backer
ES Beta Backer
 
Posts: 719
Joined: Sun Mar 15, 2009 4:21 pm
Location: Germany, Aachen

Re: Sprite animation/timing methods

Postby dandymcgee on Sun Oct 13, 2013 12:07 pm

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:
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
 
Posts: 4911
Joined: Tue Apr 29, 2008 4:24 pm
Location: New Hampshire


Return to Game Development

Who is online

Users browsing this forum: No registered users and 3 guests

cron