• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Nicholas Kong

Understanding 2D Walking/Run Animation in Games

13 posts in this topic

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:
[url=http://www.makeagif.com/Q0X2ob]Q0X2ob.gif[/URL]
 
left animation gif:
[url=http://www.makeagif.com/MQXuky]MQXuky.gif[/URL]
 
 
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: http://www.youtube.com/watch?v=2ZuhSB64quY
 
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
0

Share this post


Link to post
Share on other sites

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?

0

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.
0

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! 

0

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.

0

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.

0

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
0

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.

0

Share this post


Link to post
Share on other sites

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
1

Share this post


Link to post
Share on other sites

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?

0

Share this post


Link to post
Share on other sites

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.
0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0