Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Understanding 2D Walking/Run Animation in Games


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
13 replies to this topic

#1 warnexus   Prime Members   -  Reputation: 1450

Like
0Likes
Like

Posted 03 May 2013 - 11:23 PM

I am teaching myself how walking animation works in 2D games.
 
*Edit* - Provided a gif I am using for mario walking left and right animation.
 
Each gif has 2 frames of animation.
 
right animation gif:
Q0X2ob.gif
 
left animation gif:
MQXuky.gif
 
 
My question: is if there is an universal time interval that is used between the current and last animation frame that makes it so appealing to the gamer's eyes?
 
I wind up looking at a youtube video of Mario walking in a Mario NES Gameplay video frame by frame a couple of times just to get an idea of how the animation works. I managed to get some insight but I am not sure if my insight is correct. It is fascinating to see how much animation was going on in 1 second of the video: 
 
My insight: There is a time interval involved between each change of animation frame of Mario. How much time interval I cannot really tell. I do know there was 3 frames of animation in Mario's movement.
 
My code shows Mario running animation using 2 frames of animation. There is a time difference of 4 between the animation0 and animation1. The running seems about right but the walking animation seems delayed. The code took some trial of error to achieve this interval difference.
 
Running animation is done by holding the right arrow key.
Walking Animation is done by tapping the right arrow key.
 
Code is in Java.
 

public Mario(double x , double y)


{

position = new Vector2D(x,y);
velocity = new Vector2D();
 
animationList = new ArrayList<Image>();
 
timeInterval = 0;
WalkAnimation0 = true;
 
 
try {
image = ImageIO.read(new File("src/MarioWalkAnimation/marioWalkAnimation0.PNG"));
animationList.add(image);
 
image = ImageIO.read(new File("src/MarioWalkAnimation/marioWalkAnimation1.PNG"));
animationList.add(image);
 
 
} 
catch (IOException e) {
e.printStackTrace();
}
Game.getInstance().addKeyListener(this);
 
}
 
 
public void draw(Graphics g) {
// Draw the ship image at our position
 
 
int walkAnimation0Limit = 2;
int walkAnimation1Limit = 6;
 
if(timeInterval <= walkAnimation0Limit)
{
 
g.drawImage(animationList.get(0),(int)position.getX(),(int)position.getY(),null);
}
 
else if(timeInterval <= walkAnimation1Limit )
{
 
g.drawImage(animationList.get(1),(int)position.getX(),(int)position.getY(),null);
 
if(timeInterval == walkAnimation1Limit)
{
timeInterval = 0;
}
 
}
 
}

 


public void keyPressed(KeyEvent e)
{
// TODO Auto-generated method stub
// Handle the player keys
switch (e.getKeyCode()) {
 
case (KeyEvent.VK_RIGHT):
 
timeInterval++;
break;
}
 
 

Edited by warnexus, 05 May 2013 - 01:07 PM.


Sponsor:

#2 Anthony Serrano   Members   -  Reputation: 1218

Like
8Likes
Like

Posted 04 May 2013 - 03:51 AM

My question: is if there is an universal time interval that is used between the current and last animation frame that makes it so appealing to the gamer's eyes?

The short answer is no, there isn't.

For example, Mega Man's run cycle on the NES is a 4-frame loop (with the 4th frame being a repeat of the 2nd), and the frame is changed every 8 video frames (133 ms). The player character in Nija Gaiden on the NES also uses a 4-frame run cycle, but changes frames every 4 video frames (67 ms). Meanwhile, Samus's run cycle in Super Metroid is a 10-frame loop, with the frame changing ever 2 video frames (33 ms), and Mario's walk/run cycle is a 3-frame loop, with the delay between frame changes varying based on Mario's current speed (at it's fastest, I believe it's every 2 video frames).

That's four games, with 3 different run cycle frame counts ranging from 3 to 10 frames, 4 different animation timings ranging from 33 to 133 ms, and 4 different cycle durations, ranging from 100 ms to 534 ms.

As you can see, there are no reall hard and fast rules - it's more a matter of tinkering with the animation speed until it looks right, especially with walk/run cycles like Mario's that are more figurative than realistic.

#3 warnexus   Prime Members   -  Reputation: 1450

Like
0Likes
Like

Posted 04 May 2013 - 07:34 AM

My question: is if there is an universal time interval that is used between the current and last animation frame that makes it so appealing to the gamer's eyes?

The short answer is no, there isn't.

For example, Mega Man's run cycle on the NES is a 4-frame loop (with the 4th frame being a repeat of the 2nd), and the frame is changed every 8 video frames (133 ms). The player character in Nija Gaiden on the NES also uses a 4-frame run cycle, but changes frames every 4 video frames (67 ms). Meanwhile, Samus's run cycle in Super Metroid is a 10-frame loop, with the frame changing ever 2 video frames (33 ms), and Mario's walk/run cycle is a 3-frame loop, with the delay between frame changes varying based on Mario's current speed (at it's fastest, I believe it's every 2 video frames).

That's four games, with 3 different run cycle frame counts ranging from 3 to 10 frames, 4 different animation timings ranging from 33 to 133 ms, and 4 different cycle durations, ranging from 100 ms to 534 ms.

As you can see, there are no reall hard and fast rules - it's more a matter of tinkering with the animation speed until it looks right, especially with walk/run cycles like Mario's that are more figurative than realistic.

If you have a loop in your draw method, wouldn't there be problems?



#4 Anthony Serrano   Members   -  Reputation: 1218

Like
0Likes
Like

Posted 04 May 2013 - 02:32 PM

If you have a loop in your draw method, wouldn't there be problems?

"Loop" there is not talking about the programming construct - it means that the animation loops, i.e. it repeats instead of just ending.

#5 warnexus   Prime Members   -  Reputation: 1450

Like
0Likes
Like

Posted 04 May 2013 - 02:45 PM

If you have a loop in your draw method, wouldn't there be problems?

"Loop" there is not talking about the programming construct - it means that the animation loops, i.e. it repeats instead of just ending.

Oh okay! Thanks Anthony! 



#6 Azaral   Members   -  Reputation: 463

Like
5Likes
Like

Posted 04 May 2013 - 03:07 PM

The proper speed would be that the characters feet don't change their position relative to the piece of ground they are touching when running or walking. If the animation is running too fast, it will appear as though your character is sliding on the ground like running on ice. If the animation is running too slow, it will appear as though the character is also sliding on the ground, but being pushed by something from behind. That would be a technical way to look at it.



#7 warnexus   Prime Members   -  Reputation: 1450

Like
0Likes
Like

Posted 04 May 2013 - 10:00 PM

The proper speed would be that the characters feet don't change their position relative to the piece of ground they are touching when running or walking. If the animation is running too fast, it will appear as though your character is sliding on the ground like running on ice. If the animation is running too slow, it will appear as though the character is also sliding on the ground, but being pushed by something from behind. That would be a technical way to look at it.

I think it has something to do with the lack of animations frames or the time difference between each frame is either too small or too large which causes the animation to look off.

 

I am using 2 frames and the time difference between each frame is 3.



#8 Azaral   Members   -  Reputation: 463

Like
0Likes
Like

Posted 05 May 2013 - 06:59 AM

The proper speed would be that the characters feet don't change their position relative to the piece of ground they are touching when running or walking. If the animation is running too fast, it will appear as though your character is sliding on the ground like running on ice. If the animation is running too slow, it will appear as though the character is also sliding on the ground, but being pushed by something from behind. That would be a technical way to look at it.

I think it has something to do with the lack of animations frames or the time difference between each frame is either too small or too large which causes the animation to look off.

 

I am using 2 frames and the time difference between each frame is 3.

 

Make a gif showing the animation perhaps. So others could see what is going on with it and offer better advice.



#9 lithos   Members   -  Reputation: 413

Like
0Likes
Like

Posted 05 May 2013 - 10:22 AM

You don't have synced timing at all.   A key listener is going to run whenever the java thread scheduler has time to give and "decides" to, which means that it will vary from system to system and could be starved by other threads you're running(and never run or run intermediately).   It's also going to operate differently on different OSes since there are different minimum sleep times(XP 15MS, Vista 5MS, Linux 1MS, Mac usually 1MS).   Even then those minimum sleep times will change pretty randomly and you'll see 1-10MS differences(mostly from thread scheduling) even after the JVM has gotten all your code running native(rather than bytecode interpretation), and for start up and can expect utterly weird randomness in thread scheduling.

 

Game loop help:  http://www.java-gaming.org/topics/help-on-understanding-the-game-loop/29276/view.html , http://www.gameprogblog.com/generic-game-loop/ .

 

My own game loops follow a similar strategy, but I like to keep math stupid simple.   So I set a fixed time for the loop.   So I total up time that has happened since the last time the game loop went through, then subtract from it until it's 0-ish(it's important to save the remainder for the next loop) update after each subtraction.

 

__________________

 

Animation, parallel lists tend to make a lot of sense.  

 

Essentially for a single animation you have a list that has how long a frame should exist, and a second with the image.   Then you take a time and subtract from it by going through your time list, eventually getting to 0.   You take the index in time and it'll be the same index in the image list since the lists are parallel.  

 

edit: you're going to have to set a variable in the key press event that "requests movement/action", then handle requests in the game loop.

__________________

 

To look at someone else's code:  http://www.cokeandcode.com/index.html?page=tutorials


Edited by lithos, 05 May 2013 - 11:08 AM.


#10 warnexus   Prime Members   -  Reputation: 1450

Like
0Likes
Like

Posted 05 May 2013 - 01:07 PM

 

The proper speed would be that the characters feet don't change their position relative to the piece of ground they are touching when running or walking. If the animation is running too fast, it will appear as though your character is sliding on the ground like running on ice. If the animation is running too slow, it will appear as though the character is also sliding on the ground, but being pushed by something from behind. That would be a technical way to look at it.

I think it has something to do with the lack of animations frames or the time difference between each frame is either too small or too large which causes the animation to look off.

 

I am using 2 frames and the time difference between each frame is 3.

 

Make a gif showing the animation perhaps. So others could see what is going on with it and offer better advice.

Updated the thread. Thread has the gifs now.



#11 Jompe_71   Members   -  Reputation: 155

Like
1Likes
Like

Posted 06 May 2013 - 02:05 AM

I would start by separating animation logic from the draw method into an update method. Secondly I would the make the animation logic framerate independant. As it is now it's written in a frame dependant way which causes the animation to look different depending on the speed of the machine.

 

I'll try to do some pseudo-code below to show how I do animation logic myself below.

 

// Animation frames ( can be used for longer intervalls of certain frames as shown using the frame 4 more than the others ).
private int[] animationSequence = { 1, 1, 2, 2, 3, 3, 4, 4, 4, 5, 5, 4, 4, 4, 3, 3, 2, 2 };

// Animation framerate.
private float animationFramerate = animationSequence.length / 1000F;
private int currentFrame = 0;
 
 
public void update( long ms ) {
 
    currentFrame += ms * animationFramerate; 
    if( currentFrame > animationSequence.length ) 
    {
        currentFrame %= animationSequence.length;
    }
}
 

 

Thats pretty much it for the lazy-mans version. There are some fixes which could be made for even better synchronization. Thats by adding logic which can handle "half" frames. You can then use a variable for time spillover, i.e. when you show frame N and you still have som time left on that frame before moving into the next one. 

 

In the above code, there are two ways to tweek your animation speed. One is to change the value of 1000F to either a higher value (slows down the animation) or vice versa for a faster one OR you can alter the animation sequence to have more frames of the same index in a row. When it comes to the rendering part you have two options as well. Either treat the index value in the sequence as an Image in your array or my preferred choice an offset in a sprite-sheet.


Edited by Jompe_71, 06 May 2013 - 06:00 AM.


#12 warnexus   Prime Members   -  Reputation: 1450

Like
0Likes
Like

Posted 09 May 2013 - 08:20 AM

I would start by separating animation logic from the draw method into an update method. Secondly I would the make the animation logic framerate independant. As it is now it's written in a frame dependant way which causes the animation to look different depending on the speed of the machine.

 

I'll try to do some pseudo-code below to show how I do animation logic myself below.

 

// Animation frames ( can be used for longer intervalls of certain frames as shown using the frame 4 more than the others ).
private int[] animationSequence = { 1, 1, 2, 2, 3, 3, 4, 4, 4, 5, 5, 4, 4, 4, 3, 3, 2, 2 };

// Animation framerate.
private float animationFramerate = animationSequence.length / 1000F;
private int currentFrame = 0;
 
 
public void update( long ms ) {
 
    currentFrame += ms * animationFramerate; 
    if( currentFrame > animationSequence.length ) 
    {
        currentFrame %= animationSequence.length;
    }
}
 

 

Thats pretty much it for the lazy-mans version. There are some fixes which could be made for even better synchronization. Thats by adding logic which can handle "half" frames. You can then use a variable for time spillover, i.e. when you show frame N and you still have som time left on that frame before moving into the next one. 

 

In the above code, there are two ways to tweek your animation speed. One is to change the value of 1000F to either a higher value (slows down the animation) or vice versa for a faster one OR you can alter the animation sequence to have more frames of the same index in a row. When it comes to the rendering part you have two options as well. Either treat the index value in the sequence as an Image in your array or my preferred choice an offset in a sprite-sheet.

So you are saying I need to use the long millisecond to make it frame rate independent?



#13 Satharis   Members   -  Reputation: 1031

Like
0Likes
Like

Posted 09 May 2013 - 06:37 PM

So you are saying I need to use the long millisecond to make it frame rate independent?

He's saying that regardless of your timing method you should get some kind of delta time between updates called on your object and use that alongside the concept of say, a second, to time how often the animation frame changes. The code above is just an example of one way to do it.

The goal is that even if the update only runs 10 times a second it will change to the proper frame of the animation(even skipping frames if need be) and that framerate won't make it run at a different timing.

#14 warnexus   Prime Members   -  Reputation: 1450

Like
0Likes
Like

Posted 09 May 2013 - 09:45 PM

So you are saying I need to use the long millisecond to make it frame rate independent?

He's saying that regardless of your timing method you should get some kind of delta time between updates called on your object and use that alongside the concept of say, a second, to time how often the animation frame changes. The code above is just an example of one way to do it.

The goal is that even if the update only runs 10 times a second it will change to the proper frame of the animation(even skipping frames if need be) and that framerate won't make it run at a different timing.

Interesting. Thanks I will use this idea as a leverage.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS