Step 68PC Software: Effect 3, fireworks
To make this effect, we really had to sit down and think about how fireworks work, and which forces influence the firework particles.
We came up with a theoretical model of how fireworks work:
1) A rocket is shot up to a random position, origin_x, origin_y, origin_z.
2) The rocket explodes and throws burning particles out in random directions at random velocities.
3) The particles are slowed down by air resistance and pulled towards the ground by gravity.
With this model in mind we created a fireworks effect with a pretty convincing result. Here is how it works:
1) A random origin position is chosen. (within certain limits, x and y between 2 and 5 to keep the fireworks more or less in the center of the cube. z between 5 and 6. Fireworks exploding near the ground can be dangerous! :p)
2) The rocket, in this case a single voxel is moved up the Z axis at the x and y coordinates until it reaches origin_z.
3) An array of n particles is created. Each particle has an x, y and z coordinate as well as a velocity for each axis, dx, dy and dz.
4) We for() loop through 25 particle animation steps:
5) A slowrate is calculated, this is the air resistance. The slowrate is calculated using tan() which will return an exponentially increasing number, slowing the particles faster and faster.
6) A gravity variable is calculated. Also using tan(). The effect of gravity is also exponential. This probably isn't the mathematically correct way of calculating gravity's effect on an object, but it looks good.
7) For each particle, the x y and z coordinates are incremented by their dx, dy and dz velocities divided by the slowrate. This will make the particles move slower and slower.
8) The z coordinate is decreased by the gravity variable.
9) The particle is drawn on the cube.
10) Delay for a while, then do the next iteration of the explosion animation.
We are quite pleased with the result.
| « Previous Step | Download PDFView All Steps | Next Step » |































































































![Hard Wired LED Cube: [No Programming]](http://img.instructables.com/files/deriv/FTD/SF1T/GO8DC8KW/FTDSF1TGO8DC8KW.SQUARE.jpg)















(Forgive my pythonisque syntax.)
Storing a starting number between 0 and 1, say 0.9.
multiplier = 0.9
Initialise a variable, e.g. named store, to the same value:
store = multiplier
For each step, multiply the store by the multiplier:
store = store * multiplier
And return a value calculated from the store (assuming that the bottom of the cube is 0, and the top is 7):
return floor(8*store)
The reason I suggest this approach, is because ATMega's have a multiply instruction - you may be able to squeeze this approach onto the chip instead of running it through the computer. :)