Improvements


Long overdue follow-up for the previous blog...


In the previous one, I explained that a plasma is essentially a function calculated on each pixel which will take the following value:

                  P(x,y,t) = num_colors / n_waves * \Sum_{n = 1 to n_waves} f_n(x,y,t)

with the functions f_n being

                  f_n(x,y,t) = (1 + sin(x*u_n + y*v_n + p_n*t + q_n))/2

The major issue here is that, to get the best effect, we need need to add more than one wave. For a tilemap containng N tiles (as the product of tiles per row times tiles per column) and with a number of waves n_waves, we need to calculate N x n_waves functions. The number of tiles is hardly something we can control (we may increase the size of the tiles to reduce N but we would do this at the expense of the plasma quality which would become more blocky). We however may want to increase n_waves so that we get better effects (I find that you get very good effects with n_waves between 10 and 20, but higher number can lead interesting results). This can affect performance, but it is something that can in effect easily control.

Some more maths

We use here trigonometric functions and there is a good reason for this. The sine function has the following property: 

                        sin(a+b) = sin(a)*cos(b) + cos(a)*sin(b)

Applying this to the wave functions above, we get

             P(x,y,t) = num_colors / n_waves * \Sum_{n = 1 to n_waves} (1 + sin(x*u_n + y*v_n + q_n)*cos(p_n*t) + cos(x*u_n + y*v_n + q_n)*sin(p_n*t) )/2

Now remember that I explained in the previous post that I chose to use a single value p_n = p for all waves. Then we end up with

             P(x,y,t) = num_colors / n_waves * (n_waves + A(x,y)*cos(p*t) + B(x,y)*sin(p*t) )/2

with 

   A(x,y) = \Sum_{n = 1 to n_waves} sin(x*u_n + y*v_n + q_n) 

and 

   B(x,y) = \Sum_{n = 1 to n_waves} cos(x*u_n + y*v_n + q_n)

The interesting results here is that these function A and B only depends on the position (x,y) and can be precalculated. Effectively this means that there are now only N calculations (one per tile) to generate a plasma with an arbitrary number of waves, with no effect on performance.

Leave a comment

Log in with itch.io to leave a comment.