# How to program destructibles?

I'm thinking about programming a 2d simulation of a spaceship that takes damage depending on where the projectile came from (e.g. from behind - engine, from the front - lasers, sides - hull, etc.). When a given part is below a certain durability factor it explodes into physics simulated particles. What is a good way of storing such particles (ship built out of parts, parts built out of particles, e.g. 6px triangles) taking into consideration the following:

• different parts have different shapes,
• it might be needed to add more parts in the future,
• possibility of avoiding a pixel-perfect projectile-to-part collision detection,
• the spaceship should be affected by physics but particles shouldn't unless exploding.

A rational approach might be to simplify your problem. This is a game, so most of the time there is no need for 100% accurate physics.

Instead of tracking every single component that makes your ship, you can instead prepare several damaged ship models and swap then on the fly as it gets more and more damage.

When you swap the models, you could spawn some particles that reflect what probably got damaged at that life level of the ship.

By just loading a model, you save the extra time it would have taken per frame to calculate collisions of each piece of the ship with each other (among other things for all the objects), you also have less code to deal with. This will provide less opportunities for bugs and from a gameplay perspective might give you similar output like what you originally wanted.

Note that if your gameplay totally depends on accurate simulation of destruction, then of course this approach will not work.

• Thank you for your answer, it has been helpful although I already did a project similar to what you described in the past and now am trying to enrich it with simple (not 100% accurate) physics. It's just a learning exercise so I don't mind bugs that much. I did realise, though, that I cannot tackle this all at once and decided to program few particles simulations first. Thanks again. – cprn May 28 '16 at 19:40

I ended up not "keeping the particles together" as I originally wanted because it made literally no sense computation-wise but using a bitmap of a white silhouette on a black background as a mask for each of the spaceship modules instead. A whole ship would be made of an array of those bitmaps stuck together. Whenever a module got hit I'd render an irregular black star of rough radius = blastRadius on that bitmap to mark an explosion (effectively removing a blackened part of the module on next frame render) and then just made a tiny simulation of some particles flying away to make explosions look as intended in the opening question. Every 500ms or so I make a calculation of the pixels left in each bitmap and when white pixels are below 60% of the original amount I "explode" the whole part. It took some time to figure out but was fun to code and works great, even on my old mobile.