Jump to content
  • Advertisement
Sign in to follow this  
Nicholas Kong

Understanding 2D Walking/Run Animation in Games

This topic is 1958 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement

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?

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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! 

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

 

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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!