Processing Webcam Game

Hello Everyone :slight_smile:

My idea was to create a webcam game where different colored bubbles appear, grow and are burst once a bright area (e.g. a phone torch) is shined over them. I have been struggling with the code and hope that someone can help me fix it.

Here is my main code:

import processing.video.*;

Capture myCaptureDevice;

int imageWidth = 640;
int imageHeight = 480;

Bubble bubble1;

void setup()
{
  size(640, 480, P2D);
  myCaptureDevice = new Capture(this, imageWidth, imageHeight);
  background(0);
  smooth();

  bubble1 = new Bubble();
}

void draw()
{
  myCaptureDevice.read();
  myCaptureDevice.loadPixels();
  float brightestValue = 0;
  int brightestX = 0;
  int brightestY = 0;
  int[] pixels = myCaptureDevice.pixels;

  int index = 0;
  for (int y = 0; y < myCaptureDevice.height; y++) {
    for (int x = 0; x < myCaptureDevice.width; x++) {
      int pixelValue = myCaptureDevice.pixels[index];
      float pixelBrightness = brightness(pixelValue);
      if (pixelBrightness > brightestValue) {
        brightestValue = pixelBrightness;
        brightestY = y;
        brightestX = x;
      }
      index++;
    }
  }
  for (int i = 0; i < 100; i++) {
    if (brightestX == xArray[i] && brightnessY == yArray[i]) {
      println("Dick");
    }
  }

  *for (int i=0; i<pixels.length ;i++) {
   int pixelValue = myCaptureDevice.pixels[i];
   float pixelBrightness = brightness(pixelValue);
   if (pixelBrightness > brightestVal) {
   brightestVal = pixelBrightness;
   brightestY = y;
   brightestX = i;
   if (brightness(pixels[i]) > 220) {
   
   }
   }*/

  image(myCaptureDevice, 0, 0, imageWidth, imageHeight);

  bubble1.drawBubble();
    }


class:


public class Bubble
{
  float bubbleIncrement[] = new float[100];
  float bubbleSize[] = new float[100];
  float r[] = new float[100];
  float g[] = new float[100];
  float b[] = new float[100];
  int currentBubbles = 0;

  float[] xArray = new float[(imageWidth*imageHeight)];
  float[] yArray = new float[(imageWidth*imageHeight)];

  public Bubble()
  {
  }

  void drawBubble()
  {
    if (currentBubbles < 100)
    {

      for (int i=0; i<100; i++)
      {    
        xArray[i] = (random(imageWidth));
        yArray[i] = (random(imageHeight));
        bubbleIncrement[i] = random(50);

        r[i] = random(255);
        g[i] = random(255);
        b[i] = random(255);

        ellipse(xArray[i], yArray[i], bubbleIncrement[i]*bubbleSize[i], bubbleIncrement[i]*bubbleSize[i]);

        currentBubbles++;
      }
    }

    for (int j=0; j<10; j++) {
      if (bubbleSize[j] < 100.0) {
        bubbleSize[j] = bubbleSize[j] + 0.005;
      }

      noStroke();        
      fill(r[j], g[j], b[j], 150);

      ellipse(xArray[j], yArray[j], bubbleIncrement[j]*bubbleSize[j], bubbleIncrement[j]*bubbleSize[j]);
    }
  }
}

Sounds like an interesting project but it’s a little hard to understand your problem. Can you try to create a MCVE? And please format your code using ctrl + t and the </> button in the text box.

@staff

FORMAT PROPERLY.

Please say something more – we don’t know your goals, or where you are stuck. What isn’t working? What did you expect to happen, and what happens instead?

Is there a specific part you haven’t written yet, or code you borrowed that you don’t understand? Did you try something that didn’t work?