Animation with Images?

I want to display an animation from a set of images, labeled like "Img0.png", "Img1.png", etc. Is there any way to do this in jme already? Or is this a totally wrong approach?

Depends on what you want to do, but swapping textures in and out is going to be a poor approach whatever your goal. Typically, if you are doing something like 2D Sprite animations, you will have a sprite sheet that contains all the frames of the animation and edit texture coordinates to apply sections of the texture.

Thanks. Yes, thats exactly what my goal was, too.  :smiley: Ill try that.

Great response speed…

          -Gibi }:-@

Hi,



I wrote some code for animating a single texture (a sprite sheet as mojo called it).  I can post the code if you're interested?



Note:  The code is pretty poor and generally untested, so maybe you'll be able to help me improve it  :stuck_out_tongue:




  • Chris

Sure! That would be great! :slight_smile:

Ok cool, I wrote it a couple of months ago so I'll just check it still compiles agaisnt the latest from CVS.



I'll have it posted up asap!




  • Chris

Here is the test class.



/**
 * HelloSpriteSheet class.
 *
 * @author  Christopher Martin
 * @version 1.0
 */
public class HelloSpriteSheet extends SimpleGame {
   
    public static void main(String[] args) {
        HelloSpriteSheet app = new HelloSpriteSheet();
        app.setDialogBehaviour(AbstractGame.ALWAYS_SHOW_PROPS_DIALOG);
        app.start();
    }

   
   
   
   
    protected void simpleInitGame() {
        // Create a box to add the animated texture too
        Box b = new Box("TestBox", new Vector3f(), 1f, 1f, 1f);
       
        // Create a texture state
        TextureState ts = display.getRenderer().createTextureState();
       
        // Load the texture
        Texture sheet = TextureManager.loadTexture(
                HelloSpriteSheet.class.getClassLoader().getResource("location of the image to load"),
                Texture.MM_LINEAR,
                Texture.FM_LINEAR);
       
        // Add the texture to the texture state
        ts.setTexture(sheet);
       
        // Create the animated texture
        AnimatedSingleTexture ast = new AnimatedSingleTexture(sheet, 1, 1, 1f); // Check the class javadoc and set these to the correct values!
        ast.setActive(true);
       
        // Add the animated texture as a controller so it gets updated each frame
        b.addController(ast);
        // Add the texture state to the box so we can actually see the texture
        b.setRenderState(ts);       
       
        // Attach the box to the scene
        rootNode.attachChild(b);
    }
   
}




- Chris

Hi,



Features.



  • Variable speed

  • Supports all the controller repeat types

  • Animates the texture directly and hence doesn't care what type of geometry the texture is applied too



There are a few limitations of the class as it stands.


  • Animation frames must all be the same size (not really a problem IMO as why would they not need to be?)

  • The first frame is always at the top left of the texture

  • Animates the texture directly so each animation of the same texture must work on a clone (only of the texture! The image data does not need to be cloned!)




- Chris

Thanks! Though not exactly what i needed, it helped me create that. Great code!

        -Gibi }:-@

is there an easy way to make a certain color in an Image transparent?I know the text class uses a black background which is transparent, but what if the background is bright green? I know i can easily change it in an editing program like the gimp or photoshop , but i was wondering about doing it in jme. I have some strip animation images with different colored backrounds i want to animate with background rendered transparent .

You might be able to use glblendcolor… I'm not sure if we fully support that though.

I was able to make colors transparent but I loop through the byte buffer and if rgb=certain color then alpha=0 else alpha=255 but after I finished writing it I wondered if I wasnt doing things the hard way since it has to run through thousands of bytes for each image it loads.

Hi,


The Gibi said:

Thanks! Though not exactly what i needed, it helped me create that. Great code!


Thanks!  :)

If anyone else can give feedback that would also be awesome.

Also, would this in any way be a useful addition to jME?  I think it could be (obviously, as I wrote the class so of course I think it's needed!) as it could be used not only for 2D games but for effects in 3D games.  Mainly thinking of things like explosions or weather effects (lightning, clouds) and perhaps the coolest useage would be for animated particle effects!

I dunno, whats everyone else think?


- Chris

Good work,



I think this is very usefull.  I also hope we can do other things to enhance JME to make it more 2d development friendly.  Although, I am all for rewriting 2d games to use 3d models/objects as well.