Modifying textures using libGDX Pixmap in runtime

Since the beginning of Super Flying Thing, I wanted the game to behave in some aspects similar to Worms (and other games). One of those aspects is the destructible terrain, I love that feature and that is why I am prototyping some stuff to see if we can add that feature to the game or not.

The next video shows how I am using the Pixmap class of libGDX library to modify textures dynamically.

In the previous video there are two textures, they start with the same pixmap data. Then I start to paint and erase pixels from each texture to show how the pixmap operations works over the two pixmaps. Finally, I start rotating both textures and then paint and erase stuff again, to show how I am transforming from game world coordinates to each pixmap coordinates.

Pros

Destructible terrain. If that works, then we could add some new game play options, like throwing missiles (weapons!!) or killing yourself in order to destroy some part of the map and open paths. Or some power like a shield or something where you move through the terrain destroying it.

We could create some kind of in game level editor for people to make their own levels (and possibly share their stuff). We could make that now using tiles maybe, but I believe making the levels from textures could be easily used by any player (maybe kids). I feel like an in game level editor similar to Inkscape (what I am using right now) is advanced stuff for a typical player.

We could also make more interesting random levels (using some other techniques).

Cons

Having to handle all this texture modification information implies we have to use more memory since we can't reuse textures, and that could be a problem in limited devices.

Another problem is that textures created with Pixmaps are not being managed by libGDX, that means if the game is paused (you had a phone call for example) and the textures were disposed, then when the games continues the texture data will not be automatically loaded by the library, so we need to manage the texture ourselves.

Also, we will probably lose Box2D collisions calculations as our world is currently made by Box2D polygons, and we will have to create our own bitmap collision calculations.

Conclusion

Working the terrain as textures is really interesting but also harder and we have to prototype more to know if this is possible or not. However, I will keep dreaming with this feature.

Hope you the post, and if you know some techniques that could help or want to make another contribution, feel free to comment, we will appreciate it.

VN:F [1.9.22_1171]
Rating: 3.7/5 (6 votes cast)
Modifying textures using libGDX Pixmap in runtime, 3.7 out of 5 based on 6 ratings

Tags: , , ,

  • http://www.ziggysgames.com Ziggy

    Could you constrain the ways in which the terrain can be modified? Then you might be able to continue using box2d, splitting the bodies / fixtures along constrained paths. Something like the 'object slicing' games that some people have written.

  • arielsan

    I thought about splitting bodies/fixtures but for what I want for the game I don't feel box2d is the right answer in this case or at least I don't know how to make it in order to keep the same level of freedom the "texture" method has.

  • Pingback: Detecting collisions using libGDX Pixmap « Gemserk

  • Mario Zechner

    For the texture managment issue you can implement your own TextureData based on what PixmapTextureData does. Just don't dispose the Pixmap you return (TextureData.disposePixmap() should return false).

  • arielsan

    When you create a Texture with a Pixmap the disposePixmap is set to false by default, so the Pixmap is not disposed and that is what we want. The real problem is the corresponding texture is being "disposed" (the texture handle) when the OpenGL context is released when the application is "minimized", so when it is resumed the same Texture instance has no data and has to be reloaded with the Pixmap data (never disposed) to make all the Sprites (and other TextureRegion) to work correctly.

    We tried making our own PixmapDataFormat to return true on the isManagerd() method but that fails when you want to draw data over a managed texture, and I need that method to update the texture when the pixmap is modified (after an explosion for example).

    Right now, we are doing this, when the application is resumed, we call texture.load(new PixmapTextureData(pixmap, pixmap.getFormat(), false, false)); and that seems to be working.

    Thanks for the comments Mario :D

  • Pingback: Modifying textures using libGDX Pixmap in runtime – Explained « Gemserk