前回爆発音を作成しましたが、今回はそれを利用した爆発エフェクトの作成です。

 Processingを用いた爆破エフェクトの作成はGoogleで検索するといくつか出てきますが、今回はシンプルに円を拡大することにしました。
 特に難しいことはしていないので下の動画を見ればどのような表現をしているかわかると思います。


 中身としては、"爆発モード"に入る際に更新情報としてそのタイミングでの時間を取得し、そこからの経過時間で一番大きな円の大きさを変えています。小さい円は、ある程度ランダムな場所にある程度ランダムな最大サイズの円を設定し、半径0の円から最大サイズになるまで描画しています。小さい円は5個描画され、一つの描画が終わると次の円が表示されるようになっています。

 以下コードです。ある程度コメントで説明してあります。
  1. import java.util.Random;
  2. import ddf.minim.*;
  3. import ddf.minim.analysis.*; 
  4. import ddf.minim.effects.*;
  5. import ddf.minim.signals.*;
  6. import ddf.minim.spi.*;
  7. import ddf.minim.ugens.*;

  8. int[][] circle = new int[4][5];//x,y,r,t0,t
  9. char[] mode = new char[2];
  10. int[] bubble = new int[4];
  11. int[] noisevolmode = new int[2];

  12. Minim minim; 
  13. AudioOutput out;
  14. Random random = new Random();
  15. Noise[][] noisevol = new Noise[8800][2];

  16. void setup() {
  17.   size(1920, 1080);
  18.   colorMode(HSB, 360, 255, 255, 255);
  19.   strokeWeight(5);
  20.   background(0);
  21.   
  22.   minim = new Minim(this);
  23.   out = minim.getLineOut(Minim.STEREO);
  24.   for (int i = 0; i < 8800; i++) {
  25.     noisevol[i][0] = new Noise(1.0 + cos(map(i, 0, 8800, 0, PI)), Noise.Tint.BROWN);
  26.   }
  27.   for (int i = 0; i < 8800; i++) {
  28.     noisevol[i][1] = new Noise(1.0 + cos(map(i, 0, 8800, 0, PI)), Noise.Tint.RED);
  29.   }
  30. }
  31.   
  32. void draw() {//ループ処理
  33.   background(0);
  34.   if (mode[1] != mode[0]) {//更新処理の判定
  35.     update();
  36.     mode[1] = mode[0];
  37.   }
  38.   switch(mode[0]) {//現在のモードの実効
  39.   case 'b':
  40.     bubble();
  41.     break;
  42.   default:
  43.     break;
  44.   }
  45. }

  46. void update() {//更新処理
  47.   switch(mode[1]) {//終了処理
  48.   case 'b':
  49.     noisevol[noisevolmode[1]][0].unpatch(out);
  50.     noisevol[noisevolmode[1]][1].unpatch(out);
  51.     break;
  52.   default:
  53.     break;
  54.   }
  55.   switch(mode[0]) {//開始処理
  56.   case 'b':
  57.     if (mode[1] != mode[0]) {
  58.       bubble[0] = millis();
  59.     }
  60.     break;
  61.   default:
  62.     break;
  63.   }
  64. }

  65. void keyPressed() {//キーボード入力の割り込み
  66.   mode[0] = key;
  67. }

  68. void bubble() {//円と音の出力
  69.   background(0);
  70.   bubble[1] = millis() - bubble[0];
  71.   bubble[2] = (millis() - bubble[0]) * 10;
  72.   bubble[3] = bubble[2] % 2200;
  73.   
  74.   if (bubble[2] >= 4 * 2200) {//終了判定
  75.     mode[0] = 's';
  76.     return;
  77.   }
  78.   circle[0][4] = (millis() - circle[0][3]) * 2;
  79.   if (circle[0][2] == 0 || circle[0][2] < circle[0][4]) {
  80.     circle[0][4] = 0;
  81.     circle[0][3] = millis();
  82.     circle[0][2] = random.nextInt(500);
  83.     circle[0][1] = height / 4 + random.nextInt(height /2);
  84.     circle[0][0] = width / 4 + random.nextInt(width /2);
  85.   }
  86.   circle[1][4] = (millis() - circle[1][3]) * 2;
  87.   if (circle[1][2] == 0 || circle[1][2] < circle[1][4]) {
  88.     circle[1][4] = 0;
  89.     circle[1][3] = millis();
  90.     circle[1][2] = random.nextInt(500);
  91.     circle[1][1] = height / 4 + random.nextInt(height /2);
  92.     circle[1][0] = width / 4 + random.nextInt(width /2);
  93.   }
  94.   circle[2][4] = (millis() - circle[2][3]) * 2;
  95.   if (circle[2][2] == 0 || circle[2][2] < circle[2][4]) {
  96.     circle[2][4] = 0;
  97.     circle[2][3] = millis();
  98.     circle[2][2] = random.nextInt(500);
  99.     circle[2][1] = height / 4 + random.nextInt(height /2);
  100.     circle[2][0] = width / 4 + random.nextInt(width /2);
  101.   }
  102.   circle[3][4] = (millis() - circle[3][3]) * 2;
  103.   if (circle[3][2] == 0 || circle[3][2] < circle[3][4]) {
  104.     circle[3][4] = 0;
  105.     circle[3][3] = millis();
  106.     circle[3][2] = random.nextInt(500);
  107.     circle[3][1] = height / 4 + random.nextInt(height /2);
  108.     circle[3][0] = width / 4 + random.nextInt(width /2);
  109.   }

  110.   noisevolmode[0] = bubble[2];
  111.   noisevol[noisevolmode[1]][0].unpatch(out);
  112.   noisevol[noisevolmode[0]][0].patch(out);
  113.   noisevol[noisevolmode[1]][1].unpatch(out);
  114.   noisevol[noisevolmode[0]][1].patch(out);
  115.   noisevolmode[1] = noisevolmode[0];

  116.   fill(0, 0, 255, 255);
  117.   circle(width / 2, height / 2, bubble[3]);
  118.   circle(circle[0][0], circle[0][1], circle[0][4]);
  119.   circle(circle[1][0], circle[1][1], circle[1][4]);
  120.   circle(circle[2][0], circle[2][1], circle[2][4]);
  121.   circle(circle[3][0], circle[3][1], circle[3][4]);
  122. }