Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (603)
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  
  LWJGL Best way to handle 2D Sprite Animations?  (Read 1291 times)
0 Members and 1 Guest are viewing this topic.
Offline Clepto

Senior Newbie


Exp: 3 years



« Posted 2014-05-11 20:04:46 »

Hi, I'm wondering if there is a "recommended" way or something that would be better than what I'm currently doing.

My game currently has animations that are hundreds of frames, and there are hundreds of animations. I'm wondering if loading each of those frames as a separate image and then running those through a loop of some sort is the best way to do it.

Currently there is a spritesheet for each animation, that image is loaded as a BufferedImage, then the code splits it into individual BufferedImages for each frame, and adds them to the animation. It just seems like there is a much more efficient way to do this, perhaps some external library exists that makes animations easier.

I've entertained the idea of defining animations with XML or something like that, and loading the different limbs and bone structures of say a player, and having the code itself run through the animation rather than running through frames.

I'm not really seeing any performance issues doing this way, but it seems like there should be a better way.

Please don't say use LibGDX, I'm making it from straight LWJGL as I'm more interested in learning the core stuff than finishing a complete game.
Offline Bearded Cow

Senior Devvie


Medals: 2
Projects: 1
Exp: 1 year


¬..¬


« Reply #1 - Posted 2014-05-11 20:07:29 »

If you don't want to go fully with LibGDX I would recommend Slick2D. It is very useful.
Offline Clepto

Senior Newbie


Exp: 3 years



« Reply #2 - Posted 2014-05-11 20:09:48 »

If you don't want to go fully with LibGDX I would recommend Slick2D. It is very useful.

I have used Slick2D before, and I probably will for actually loading the images at some point, but right now I'm just wondering if there is a better way that anyone knows of to handle the actual animation other than just loading each frame as a separate image.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Online Drenius
« Reply #3 - Posted 2014-05-11 20:33:35 »

Look into skeletal animation.
Spine is a (probably) good tool for this, but looking at it at is least helpful to understand the concept.
Offline Clepto

Senior Newbie


Exp: 3 years



« Reply #4 - Posted 2014-05-11 20:58:00 »

Look into skeletal animation.
Spine is a (probably) good tool for this, but looking at it at is least helpful to understand the concept.

Would this be able to work well with a physics library? I can't think of a specific one I would use yet, but in theory would it be compatible with it(E.g. character get's blown up and falls around places with limbs freely flailing), is that how that is commonly done?
Offline Cero
« Reply #5 - Posted 2014-05-11 23:08:29 »

Spine is a different concept, its for ragdoll animations n stuff - of course you can also use it to render those frame to images

@question: usually you use a spritesheet aka textureAtlas

like so:

Click to Play

Offline Nate

« JGO Bitwise Duke »


Medals: 158
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #6 - Posted 2014-05-12 10:17:57 »

Spine would reduce your need to have so many images, as well as allow you to have many more animations, including procedural animation (eg looking at the mouse).

Online cylab

JGO Ninja


Medals: 55



« Reply #7 - Posted 2014-05-12 10:37:13 »

The OP already stated that he has a spritesheet, but splits it into multiple separate images on the "java-side".

This is not the right way to do it. Just load your complete spritesheet as one texture and change the texture coordinates of your Quad (or whatever you are using) to only contain the sprite frame you want to draw.

Take a look here: http://androidblog.reindustries.com/a-real-opengl-es-2-0-2d-tutorial-part-7-texture-atlas/ for inspiration

Mathias - I Know What [you] Did Last Summer!
Offline Clepto

Senior Newbie


Exp: 3 years



« Reply #8 - Posted 2014-05-13 23:47:54 »

Thanks for the help guys. I think I may end up switching to LibGDX and using Spine. I currently will have over 10,000 animations if I keep doing it this way, and most likely many more than that. I think from what I've been Spine will be exactly what I need. Thanks for the advice.
Offline GNecro1
« Reply #9 - Posted 2014-07-14 21:35:09 »

This is how i do it, no LWJGL!

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
import java.awt.image.BufferedImage;

public class Animation {

   private BufferedImage[] animImages = {};
   private boolean animate = false;
   private int index = 0;
   private long miliSeconds, targetTime, now;

   public Animation(int mili,BufferedImage[] images) {
      this.miliSeconds = mili;
      animImages = images;
      now = System.currentTimeMillis();
      targetTime = now + miliSeconds;
      Main.anims.add(this);
   }
   
   public void stopAnimation(){
      animate = false;
   }
   
   public void startAnimation(){
      animate = true;
   }
   
   public BufferedImage getCurrentImage(){
      if(index < 0 || index > animImages.length){
         new Exception();
      }
      return animImages[index];
   }
   
   public void disposeAnimation(){
      Main.anims.remove(this);
   }
   
   public void setIndex(int i){
      index = i;
   }
   
   public void tick(){
      if (System.currentTimeMillis() >= targetTime && animate) {
         index++;
         now = System.currentTimeMillis();
         targetTime = now + miliSeconds;
         if(index>animImages.length-1){
            index = 0;
         }
      }
   }

}

Java freak! Cheesy
Pages: [1]
  ignore  |  Print  
 
 

 

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

The first screenshot will be displayed as a thumbnail.

rwatson462 (37 views)
2014-12-15 09:26:44

Mr.CodeIt (31 views)
2014-12-14 19:50:38

BurntPizza (62 views)
2014-12-09 22:41:13

BurntPizza (99 views)
2014-12-08 04:46:31

JscottyBieshaar (60 views)
2014-12-05 12:39:02

SHC (74 views)
2014-12-03 16:27:13

CopyableCougar4 (77 views)
2014-11-29 21:32:03

toopeicgaming1999 (138 views)
2014-11-26 15:22:04

toopeicgaming1999 (127 views)
2014-11-26 15:20:36

toopeicgaming1999 (38 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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
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!