Posts Tagged ‘prototype’

Modifying textures using libGDX Pixmap in runtime - Explained

Wednesday, January 4th, 2012

We have previously shown a bit how we were using LibGDX Pixmap to modify textures in runtime here and here for a game prototype we were doing. In this post I want to share more detail of how we do that. The objective was to make destructible terrain like in Worms 2.

Introduction

When you work with OpenGL textures, you can't directly modify their pixels whenever you want since they are on OpenGL context. To modify them you have to upload an array of bytes using glTexImage2D or glTexSubImage2D. The problem is you have to maintain on the application side an array of bytes representing the modifications you want to do.

To simplify working with byte arrays representing images, LibGDX provides a useful class named Pixmap which is a map of pixels kept in local memory with some methods to interact with a native library to perform all modifications with better performance.

Moving data from Pixmap to OpenGL Texture

In our prototypes, we wanted to remove part of the terrain whenever a missile touches it, like a Worms 2 explosion. That means we need some way to detect the collisions between the missile and the terrain and then a way to remove pixels from a texture.

We simplified the first problem by getting the color of the pixel only for the missile's position and checking if it was transparent or not. A more correct solution could be using a bitmap mask to check collisions between pixels but we wanted to simplify the work for now.

For the second problem, given a radius of explosion of the missile, we used the pixmap fillCircle method by previously setting the color to (0,0,0,0) (fully transparent) and disabled Pixmap blending to override those pixels.

But that only modified the pixmap data, now we needed to modify the OpenGL texture. To do that, we called OpenGL glTexImage2D using the bytes of the pixmap as the new texture data and that worked correctly.

Transforming from world coordinates to Pixmap coordinates

One problem when working with pixmaps is we have to map world coordinates (the position of the missile for example) to coordinates inside the Pixmap.


This image shows the coordinate system of the Pixmap, it goes from 0 to width in x and 0 to height in y.


This image shows how we normally need to move, rotate and resize the Pixmap in a game.

To solve this, we are using a LibGDX Sprite to maintain the Pixmap transformation, so we can easily move, rotate and scale it. Then, we can use that information to project a world coordinate to Pixmap coordinate by applying the inverse transform, here is the code:

	public void project(Vector2 position, float x, float y) {
		position.set(x, y);

		float centerX = sprite.getX() + sprite.getOriginX();
		float centerY = sprite.getY() + sprite.getOriginY();

		position.add(-centerX, -centerY);

		position.rotate(-sprite.getRotation());

		float scaleX = pixmap.getWidth() / sprite.getWidth();
		float scaleY = pixmap.getHeight() / sprite.getHeight();

		position.x *= scaleX;
		position.y *= scaleY;

		position.add( //
				pixmap.getWidth() * 0.5f, //
				-pixmap.getHeight() * 0.5f //
		);

		position.y *= -1f;
	}

(note: it is the first version at least, it could have bugs and could be improved also)

To simplify our work with all this stuff, we created a class named PixmapHelper which manage a Pixmap, a Texture and a Sprite, so we could move the Sprite wherever we wanted to and if we modify the pixmap through the PixmapHelper then the Texture was automatically updated and hence the Sprite (since it uses internally the Texture).

The next video shows how we tested the previous work in a prototype were we simulated cluster bombs (similar to Worms 2):

Some adjustments to improve performance

Instead of always working with a full size Pixmap by modifying it and then moved to the OpenGL texture, we created smaller Pixmaps of fixed sizes: 32x32, 64x64, etc. Then, each time we needed to make an explosion, we used the best Pixmap for that explosion and then we called glTexSubImage2D instead glTexImage2D to avoid updating untouched pixels. One limitation of this modification is we have to create and maintain several fixed size pixmaps depending on the modification size. Our current greater quad is 256x256 (almost never used).

Then, we changed to store each modification instead performing them in the moment the PixmapHelper erase method was called, and we added an update method which performs all modifications together. This improvement allow us to call Pixmap update method when we wanted, maybe one in three game updates or things like that.

Conclusion

Despite using LibGDX Pixmap for better performance, moving data to and from OpenGL context is not a cheap operation, on Android devices this could means some pauses when refreshing the modified textures with the new data. However, there is a lot of space for performance improvement, some ideas are to work only with pixmap of less bits instead RGBA8888 and use that one as the collisions context and as the mask of the real image (even using shaders), between other ideas.

Finally, the technique looks really nice and we believe that it could be used without problems for a simple game but it is not ready yet to manage a greater game like Worms 2.

Hope you like the technique and if you use it, maybe even also share your findings.

P.S.: In case you were wondering: yes, I love Worms 2.

VN:F [1.9.22_1171]
Rating: 4.0/5 (4 votes cast)

Detecting collisions using libGDX Pixmap

Monday, October 10th, 2011

I keep making prototypes about what I talk in the previous post, in this case I made an example of how to interact between the game entities (the bombs in the video) and the Pixmap data (the platform of the video) by adding some kind of basic collision detection checking the Pixmap's pixels with alpha value different from zero. I only wanted to share the a video showing the experiment:

As always, hope you like it. Probably more information on next posts.

VN:F [1.9.22_1171]
Rating: 4.6/5 (10 votes cast)

Modifying textures using libGDX Pixmap in runtime

Thursday, October 6th, 2011

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)

Super Flying Thing - Update 09

Wednesday, October 5th, 2011

There is a new version of Super Flying Thing available, here is the change log:

  • Changed to count the best time to end a level
  • Modified random level generation and changed random mode to be named training
  • Removed practice mode, go to training now
  • Removed Exit button, you can exit the game pressing back button on main menu
  • Added sound for the explosions
  • Added about us screen with links to blog and more games

Remember, you can play it right now, just follow the next links:

PLAY ON PC or PLAY ON ANDROID:

Play on Android

Hope you enjoy it.

VN:F [1.9.22_1171]
Rating: 0.0/5 (0 votes cast)

A game prototype : Mad Jetpack

Tuesday, September 27th, 2011

The previous weekend I started a game prototype named Mad Jetpack, it is a Jetpac remake with Box2D physics and probably more features.

Jetpac is an arcade game where the main idea was to recover parts of your ship and assemble them to fix your ship while different monsters (from each planet you visit I suppose) attack you.

Game prototype is uploaded and it has already some features implemented. As I don't know if I will continue with it or not I want to share the current state with you.

Objective: pick the small circles(ship parts or fuel) and bring them to the greater circle (your ship), avoid the moving circles (enemies).

Instructions: move the character with WASD, aim with the mouse and shoot with the left button, press 1 to restart the game.

PLAY with your imagination

Hope you enjoy it.

VN:F [1.9.22_1171]
Rating: 0.0/5 (0 votes cast)

Super Flying Thing - Update 08

Friday, September 2nd, 2011

There is a new version of Super Flying Thing available, here is the change log:

  • Added replays to show how you died a lot of times :P, could be disabled if you don't want to watch it.
  • Changed the Splash screen to load all stuff once to improve performance during the game and to show loading progress.
  • Changed screen buttons, moved them up to avoid touching the ads when trying to touch a button.
  • Changed to avoid showing a title bar when the game started.
  • Changed Settings screen, moved controllers stuff to another screen.
  • More performance improvement.
  • Fixed (probably) the bug of OutOfMemoryException when the game was left open for a long time.

Remember, you can play it right now, just follow the next links:

PLAY ON PC or PLAY ON ANDROID:

Play on Android

Hope you enjoy it.

VN:F [1.9.22_1171]
Rating: 0.0/5 (0 votes cast)

Ludum Dare 21, Here we go...

Saturday, August 20th, 2011

As we did for the previous fourth Ludum Dare (or at least we tried), we joined Ludum Dare 21 (page is down right now). The theme in this case is Escape.

Based on my lack of imagination I am creating a game similar to Canabalt where you play as a Vampire escaping from some vampire hunters named Vampire Runner. I have almost nothing right now but I am making the effort. If you want to follow my progress, click here.

Ruben is also joining, if you want to follow his progress, click here. His game is a platformer game trying to add some interesting behavior with physics and gravity initially the idea is that you can turn the room 90 degrees, but could be interesting to add levels without gravity, with gravity inversion like VVVVVV and stuff like that.

Hope we finish something in time.

VN:F [1.9.22_1171]
Rating: 0.0/5 (0 votes cast)

Super Flying Thing - Update 07

Friday, August 19th, 2011

Hi everyone, we found a little tiny bug that made our game run different in different phones, so in some phones the ship would move way faster than in others, and that make the game a little too challenging (impossible to control, always dying, lots of fun).

The other big changes in this updates are some stuff trying to get the game to run better on phones that aren't the latest and greatest. For now the background will start disabled but you can enable it by going to the settings page (it looks really nice :-) but it is expensive to show in some phones, in the future we will try to test the phone or start with the background enabled and detect if it is an issue and suggest to the user that he can disable it to get better performance).

As always, a change log:

  • Fixed a bug making the game run different on different devices, some times almost not playable
  • Added settings to enable/disable background to improve game performance (disabled by default)
  • Added new controller named TargetController which allows you to specify the point the ship should go
  • Added a second background which moves with the camera giving a basic idea of parallax
  • Changed ship animation to have more frames to be smoother
  • Changed internal stuff to improve performance.
  • Changed the graphics (background, walls, color of portals)

Also, we have a new gameplay video:

Remember, you can play it right now, just follow the next links:

PLAY ON PC or PLAY ON ANDROID:

Play on Android

Hope you enjoy it.

VN:F [1.9.22_1171]
Rating: 4.0/5 (1 vote cast)

Super Flying Thing - Update 06

Friday, August 12th, 2011

Readers, I know there are some, there is a new update of the game, should be available on Android Market already.
We added different controllers and we are testing how they feel and what method of control people like best.

As always, a change log:

  • Added more levels and fixed previous ones.
  • Added different controllers to test.
  • Added level preview on level selection screen.
  • Added settings screen which lets you choose different controllers and test them.
  • Added message to show which level is being previewed
  • Added animation to visited planet to differentiate easily between start and destination planets.
  • Changed ship to receive damage instead killing it instantly, so you now could survive in some cases.
    Fixed a bug with lasers.

Remember, you can play it right now, just follow the next links:

PLAY ON PC or PLAY ON ANDROID:

Play on Android

Hope you enjoy it.

VN:F [1.9.22_1171]
Rating: 5.0/5 (2 votes cast)

Some funny stuff on Super Flying Thing

Monday, August 8th, 2011

I was testing some code about hierarchical structures for the game entities and I tried adding a laser gun to the ship to see how it looks like and here are the results:

Testing random stuff on the game to test code gives me some new gameplay ideas, for example, shooting lasers to some targets to destroy them while avoiding killing some good targets.

VN:F [1.9.22_1171]
Rating: 4.0/5 (1 vote cast)