The Stack Overflow podcast is back! Listen to an interview with our new CEO.
1 of 4

How to program destructibles?

Sorry for asking four questions at once but these are all on the subject of "how do I do this and what are the pitfalls".

I'm building a space ship!

I was wondering about the simplest way to make a little space ship fighter that takes damage based on a hit location (e.g. engine, thrusters, laser, hull) and can fall apart.

I decided I have to "build" a ship as a collection of separate parts and build each part as a collection of "particles" (e.g. little triangles, squares or circles) but I lack experience in procedural animations and can't decide on the following...

1. Are triangles a good choice for "particles"?

At first I thought squares would be easier because of collision detection but if I rotate a square 45 degrees I'll be facing exactly same problems as with triangles so I thought why not to go for less edges. Will this bite me later? Am I wrong thinking it's the same level of complication?

2. How do I store a shape build from "particles"?

For squares it would simply be a 2d array. But I wish I could store triangles. Do I add one more dimension for triangle rotation? I never wrote an algorithm that would approximate a shape described by 2d array and fill it with triangles but it sounds as interesting alternative to 3d array. What would be the steps to do so? Will this bite my behind later?

3. How do I avoid pixel-perfect collision detection?

Each part has to be aware of whether it has been hit so it could indicate damage and finally animate explosion. I can't think of any good hitbox approximation unless I decide all parts are square shaped. How do I approximate hitboxes for shapes made out of triangles? Assuming there are many ships on the screen at the same time, should I drop this idea and switch to detection based on the angle of the projectile or obstacle that hit the ship (e.g. ship gets hit from behind - engine takes damage, from the side - generator, etc.). Is there some obvious solution I'm missing?

4. How do I "glue" parts together (physics)?

Currently I have a simple Physics class that makes all descendants behave according to applied forces (velocity, acceleration, resistance). I store a list of Part objects on the Ship object and list of Particle objects on each Part object. How do I "disable" Physics inheritance on particles and parts that aren't currently animated so that they would appear "glued" together instead of falling apart?

P.s. Thanks for your patience and hopefully it is an interesting subject for you as well.