Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (567)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Best Method to Implementing Smooth Animation  (Read 1144 times)
0 Members and 1 Guest are viewing this topic.
Offline ghostsoldier23

Junior Member


Medals: 1



« Posted 2013-01-23 19:56:49 »

What is the best method to programming more advanced 2D animations in Java, like skeletal movement and such?

For example, in a 2D game, a character walking animation with fluid leg movement as opposed to flipping between 3-4 animation sprites.

Is it best to just make a TON of sprites?  Or is there a better way?
Offline ClickerMonkey

JGO Coder


Medals: 20


Game Engineer


« Reply #1 - Posted 2013-01-23 20:00:15 »

You could use http://www.java-gaming.org/topics/spine-2d-skeletal-animation/27914/view.html


Offline ghostsoldier23

Junior Member


Medals: 1



« Reply #2 - Posted 2013-01-23 20:12:06 »


I did notice that, and I might give it a try, but I'm more interested in first trying to just build something decent into the engine from scratch.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ClickerMonkey

JGO Coder


Medals: 20


Game Engineer


« Reply #3 - Posted 2013-01-23 20:36:38 »

There is no "best" way to do this. This is done in 3D applications by creating a skeleton, which is a tree of bones. When you move one bone all of it's children move. Once a skeleton is created you can create animations in frames, which is the state of each bone relative to its parent (the distance and angle for simple skeletons). To animate the model you attach each vertex (or a sprite in your case) to a bone (or multiple if you're crazy).

Here's one way of doing it:

1  
2  
3  
4  
5  
6  
7  
public class Bone {
    public Bone parent;
    public Bone[] children = {};
    public Vector position = new Vector();
    public float angle;
    public float distance;
}


The update code
1  
2  
3  
4  
5  
6  
7  
8  
if ( parent != null ) {
  position.set( parent.position );
}
position.x += Math.cos(angle) * distance;
position.y += Math.sin(angle) * distance;
for (int i = 0; i < children.length; i++) {
   children[i].update();
}


So the root nodes should have the position of the actual sprite.

There are MANY ways of doing this.. so there are several strengths (simple) and weaknesses (angles) to this approach.

Now to animation, I imagine you could create a skeleton like this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
public class Skeleton {
   public Bone[] bones;
   public Vector position;
   public void update() {
      for ( int i = 0; i < bones.length; i++ ) {
        if ( bones[i].parent == null ) {
          bones[i].position.set( position );
          bones[i].update();
        }
      }
   }
}


And a frame would have a snapshot of the bones angles and distances:

1  
2  
3  
4  
5  
public class Frame {
   public float[] angles;
   public float[] distances;
   public float duration;
}


And an animation has a set of frames:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
public class Animation {
    public int index;
    public float time;
    public Skeleton skeleton;
    public Frame[] frames;
    public void update(float dt) {
       Frame f = frames[currentFrame];
       time += dt;
       if ( time >= f.duration ) {
          currentFrame = (currentFrame + 1) % frames.length; // loop through frames
         time -= f.duration;
       }
       // update skeleton based on current animation
      Frame f0 = frames[currentFrame];
       Frame f1 = frames[(currentFrame + 1) % frames.length];
       float delta = time / f0.duration;
       for (int i = 0; i < skeleton.bones.length; i++) {
         skeleton.bones[i].angle = (f1.angles[i] - f0.angles[i]) * delta + f0.angles[i];
         skeleton.bones[i].distance = (f1.distances[i] - f0.distances[i]) * delta + f0.distances[i];
       }
    }
}


With all of this I'm sure you can figure out the rest, you have a Model which has a Skeleton and a list of Animations it can play. You could add animation queueing, transitioning, reversing, etc.

To attach sprites (i like to call them Limbs) you just need to know your bone, offset, and whether you rotate with the bone

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
public class Limb {
   public Bone bone;
   public Sprite sprite;
   public Vector offset;
   public boolean rotatesWith;
   public boolean sharesAngle;
   public float angleOffset;
   public void update() {
     sprite.position.set( bone.position );
     if ( rotatesWith ) {
         // rotate offset by bone.angle and add to sprite.position
    } else {
         sprite.position.add( offset );
     }
     sprite.angle = (sharesAngle ? bone.angle + angleOffset : angleOffset);
   }
}


Something like that...

Hope it helps!

Offline ghostsoldier23

Junior Member


Medals: 1



« Reply #4 - Posted 2013-01-23 20:57:04 »

Very well written answer thank you!  I'll give this model a try.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Pippogeek (39 views)
2014-09-24 16:13:29

Pippogeek (30 views)
2014-09-24 16:12:22

Pippogeek (19 views)
2014-09-24 16:12:06

Grunnt (45 views)
2014-09-23 14:38:19

radar3301 (27 views)
2014-09-21 23:33:17

BurntPizza (63 views)
2014-09-21 02:42:18

BurntPizza (33 views)
2014-09-21 01:30:30

moogie (41 views)
2014-09-21 00:26:15

UprightPath (50 views)
2014-09-20 20:14:06

BurntPizza (54 views)
2014-09-19 03:14:18
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!