How to animate 2D sprites/textures

Hi! I started a 2D game using the same method than in the game sample Asteroid.

So I have a “3D” flat square mesh on which the texture is my 2D sprite/image.

I want to animate it. What is the best way to achieve this?

I am thinking of creating a task which will change the texture after a short delay, but that seem really inefficient. Is there any better way to achieve this?

And while rendering the texture on a flat square is working to get a 2D like game, is this the best way to make a 2D game in panda3D? But I am only wondering, since it is already working.

Thanks again!

You might try making use of SequenceNode–see the API here, and a description of one approach here. In short, a SequenceNode should automatically “flip through” a given sequence of geometry, complete with control over the speed at which the flipping occurs. In your case this geometry would presumably be cards textured with frames of an animation, producing simple frame-by-frame animation, I believe.

With a bit of work one might use SequenceNodes to put together a “Sprite” class that handles multiple animations for a single object (such as animations for walking in various directions); indeed, it might be worth searching the forum to see whether someone has already done so.

Note: I don’t claim that the above is the best way of doing this; it’s simply a way that I’m aware of.

If the animation is not too long and the frame not too large, you can make a texture atlas and put all of the frames of the animations in one file, then move the uv on your quad. Some time ago I made a class that makes exactly that: Animated texture via atlas (fake particles)

You can also go a step further and do the scrolling in a shader, that should work even faster. You can also try to use an array texture I think they are available in 1.9.x (but I’m not 100% sure), if your animations are hundreds of frames long this might be the best option.

You can also make a 2d polygon mesh for animations and use plain old joints/bones just as it would be a 3d animated character, the concept is surprisingly simple and from what I can tell extremely popular on mobile platforms.
Something like this:


esotericsoftware.com/spine-in-depth

The fastest way, if your animation is simply a sequence of frames, is to load your animation frames as a texture array or a 3D texture, and then in your shader (or using setTexPos) you cycle through the frames. With a 3D texture you can get inter-frame-blending that way for free.

Thank you all!

Time to work the the solutions you provided!!!