# Making up everything from single pixels

I recently saw a game which looked a bit like minecraft, but it was a RPG. The enemies in this game consist completely of voxels and if they die, they collaps and every single voxels falls down, splattering on the floor.

That looked great and I think it could also look good in a 2D Shooter. But I cannot think about an approach to handle such an amount of pixels. What I want to achieve is: The enemies (and also the player) should consist of pixels, but not in a way that let them look like a pixel mess. They should have certain shapes etc. But everything should be loose. So for example, if there is an endboss with some tentacles or big weapons, it should be possible to destroy these bit by bit. And everytime a bit is destroyed, the single, now loose pixels fly away in all directions.

How is this usually done for voxels? Will this even be possible with pixels (I cannot see why not)? How could be an approach to implement such an idea?

• I could see a particle system working well for this. Paired with skeletal animation for your sprites and a rendering system that allowed fine control over which parts of the sprite to draw. Mar 27 '13 at 19:16
• The game in question, for reference's sake, is most likely 3d Dot Game Heroes; see, for instance, youtube.com/watch?v=Bhuhdw_9Hnk Mar 28 '13 at 8:10
• This is not the game I saw, but it illusterstes the Ideal very well. Sorry for giving no example, I could not remember the name. Mar 28 '13 at 8:21

In one of my voxel engines, the voxel block explosion was done by creating a defined amount of minature blocks at the explosion point, and applying physical properties to them, eg velocity and gravity. Then, set them at random directions, and draw them. So, the actual voxel model wasnt really being disintegrated, but the voxel particles gave that impression.

That is one approach.

Another approach is to actually break up the voxel model. To achieve that under testing, was to detach the smaller voxels from the model, and apply the same techique for the previously mentioned explosion. No new voxels were created.

In relation to pixels, I would imagine it possible, yes.

An approach might be to look at using a rendertarget for the sprite, and where the texture pixel coords match the explosion points, store those coloured pixels in a dictionary, with position and colour values. Then, the rendertarget would form the source of the sprite without those pixels present. Use finally some type of custom particle manager to explode the pixels from the dictionary, using a technique similar to the explosion of the voxel model that I mention in the second part of this response.

Edit:

As requested in the comment, here is an example of using a rendertarget to regenerate a sprite:

    GraphicsDevice _graphicsDevice;
RenderTarget2D _renderTarget;
SpriteBatch _spriteBatch;
Texture2D texture;
Texture2D _spriteTexture ;
Rectangle sourceRectangle;

protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
_spriteBatch = new SpriteBatch(GraphicsDevice);
}


And then, in a new method, something similar to:

        _graphicsDevice.Clear(Color.White);
_renderTarget = new RenderTarget2D(_graphicsDevice, 16, 16, false, _graphicsDevice.DisplayMode.Format, DepthFormat.Depth24);

_graphicsDevice.SetRenderTarget(_renderTarget);
_spriteBatch.Begin();
sourceRectangle = new Rectangle(0, 0, 16, 16);
_spriteBatch.Draw(_spriteTexture, new Vector2(0, 0), sourceRectangle,
Color.White, 0, new Vector2(0, 0), 1f, SpriteEffects.None, 1);
_spriteBatch.End();
_graphicsDevice.SetRenderTarget(null);

texture = _renderTarget;


texture becomes the output sprite, as a Texture2D. The code isnt really doing a change here to the spriteTexture. But it illustrates how the spriteTexture is rendered to a new RenderTarget. The actual change would happen just prior to the spriteBatch.Draw method call, where some effect, or modification to the spriteTexture would be done, eg, removing some pixels because of the explosion.

• Could you please explain what you mean by rendertarget? I only heard about it when playing around with XNA and I understood it as a shape for images. Is this correct? Mar 28 '13 at 8:24
• Thanks for the code and the further explanation. "or modification to the spriteTexture would be done, eg, removing some pixels because of the explosion." This was actually the last bit of information I needed to understand your answer. Your approach sounds very good. I will try to make it this way. Mar 28 '13 at 10:15