Create bugs inside of an ellipse

So, i have an array of “bugs” and want to make them appear inside of an ellipse and constrain them there until i release them (mouse click, probably).

My struggle right now is to make them appear inside of the ellipse.

Main Code:

// Importar Bibliotecas
import ddf.minim.*;
import processing.video.*;


PImage imgBug;
// Array de Bugs
Bug[] bugs = new Bug[5];

float raioCircunferencia;
float x;
float y;


Minim minim;
AudioInput in;

Capture cam;

void setup() {
  size(2000, 2000);
  smooth();
  raioCircunferencia = 1000;
  
  background(255);
  
  for (int i = 0; i < bugs.length; i++) {
    float a = random(TWO_PI);
    
    x = width/2+cos(a)*random(raioCircunferencia);
    x = height/2+sin(a)*random(raioCircunferencia);
    
    float r = random(50,200);
    color c = color(int(random(255)),int(random(255)),int(random(255)));
    
    bugs[i] = new Bug(c, x, y, r);
  }
  
  
  // SOM 
  minim = new Minim(this);
  minim.debugOn();
  in = minim.getLineIn(Minim.STEREO, 2048);
  
}

void draw() {
  
  stroke(1);
  fill(255);
  ellipse(width/2,height/2,raioCircunferencia,raioCircunferencia);
  
  float som = in.mix.level();
  
  fill(255,30);
  rect(0,0,width,height);
  
  
  for (int i = 0; i < bugs.length; i++) {
    bugs[i].move(som);
    bugs[i].display();
  }
}

void stop()
{
  // always close Minim audio classes when you are done with them
  in.close();
  minim.stop();
  super.stop();
}

Class Bug

class Bug {

  color c;
  color bugColor;
  float x;
  float y;
  float r;
  float speed = 2.50;

  Bug (color cor, float posX, float posY, float raio) {

    c = cor;
    x = posX;
    y = posY;
    r = raio;
  }

  void move(float audio) {
    float tx, ty;
    
    tx = x;
    ty = y;
    
    speed = map(audio, 0.00001, 1, 10, 30);
    
    tx += random(-speed, speed);
    ty += random(-speed, speed);
    
    if(tx <= 0 || tx >= width || ty <= 0 || ty >= height){
      tx = x;
      ty = y;
    }
    
    x = tx;
    y = ty;
    
  }

  void display() {
    if (x <= width/2) {
      bugColor = color(255, 0, 0);
    } 
    else {
      bugColor = color(0, 0, 255);
    }
    
    fill(bugColor);
    noStroke();
    ellipse(x, y, r, r);
  }
}

It doesn’t work ?

for (int i = 0; i < bugs.length; i++) {
    float a = random(TWO_PI);
    
    x = width/2+cos(a)*random(raioCircunferencia);
    x = height/2+sin(a)*random(raioCircunferencia);
    
    float r = random(50,200);
    color c = color(int(random(255)),int(random(255)),int(random(255)));
    
    bugs[i] = new Bug(c, x, y, r);
  }

By the way you cloud say :

for(Bug b : bugs){
  b.move(som);
  b.display();
}

instead of :

for (int i = 0; i < bugs.length; i++) {
    bugs[i].move(som);
    bugs[i].display();
}

That’s my code, right? It should, shouln’t it?

Do bugs appear inside of the ellipse?

Nop… :confused:
Maybe it’s something wrong with the Bug Class?

Maybe this ? :

// Importar Bibliotecas
import ddf.minim.*;
import processing.video.*;


PImage imgBug;
// Array de Bugs
Bug[] bugs = new Bug[5];

float raioCircunferencia = 500;
float x;
float y;


Minim minim;
AudioInput in;

Capture cam;

void setup() {
  fullScreen();
  smooth();
  
  background(255);
  
  for (int i = 0; i < bugs.length; i++) {
    float a = random(TWO_PI);
    
    x = cos(a)*random(raioCircunferencia);
    y = sin(a)*random(raioCircunferencia);
    
    float r = random(50,200);
    color c = color(int(random(255)),int(random(255)),int(random(255)));
    
    bugs[i] = new Bug(c, x, y, r);
  }
  
  
  // SOM 
  minim = new Minim(this);
  minim.debugOn();
  in = minim.getLineIn(Minim.STEREO, 2048);
  
}

void draw() {
  background(255,30);
  translate(width/2,height/2);
  stroke(1);
  fill(255);
  ellipse(0,0,raioCircunferencia*2,raioCircunferencia*2);
  
  float som = in.mix.level();
  
  
  for (Bug b : bugs) {
    b.move(som);
    b.display();
  }
}

void stop()
{
  // always close Minim audio classes when you are done with them
  in.close();
  minim.stop();
  super.stop();
}


class Bug {

  color c;
  color bugColor;
  float x;
  float y;
  float r;
  float speed = 2.50;

  Bug (color cor, float posX, float posY, float raio) {

    c = cor;
    x = posX;
    y = posY;
    r = raio;
  }

  void move(float audio) {
    float tx, ty;
    
    tx = x;
    ty = y;
    
    speed = map(audio, 0.00001, 1, 10, 30);
    
    tx += random(-speed, speed);
    ty += random(-speed, speed);
    
    if(tx <= -raioCircunferencia || tx >= raioCircunferencia || ty <= -raioCircunferencia || ty >= raioCircunferencia){
      tx = x;
      ty = y;
    }
    
    x = tx;
    y = ty;
    
  }

  void display() {
    if (x <= 0) {
      bugColor = color(255, 0, 0);
    } 
    else {
      bugColor = color(0, 0, 255);
    }
    
    fill(bugColor);
    noStroke();
    ellipse(x, y, r, r);
  }
}

I added translate(width/2,height/2); and raioCircunferencia in the parameters.

It kinda worked, but i change the values like this it stops working, now the next step is constraining the bugs inside the ellipse. Can i do that with the dist function on the b.move()?

I changed the fullscreen for size(1000,1000); and made radius of width/2

// Importar Bibliotecas
import ddf.minim.*;
import processing.video.*;


PImage imgBug;
// Array de Bugs
Bug[] bugs = new Bug[5];

float raioCircunferencia;
float x;
float y;


Minim minim;
AudioInput in;

Capture cam;

void setup() {
  size(1000,1000);
  smooth();
  raioCircunferencia = width/2;
  background(255);
  
  for (int i = 0; i < bugs.length; i++) {
    float a = random(TWO_PI);
    
    x = cos(a)*random(raioCircunferencia);
    y = sin(a)*random(raioCircunferencia);
    
    float r = random(50,200);
    color c = color(int(random(255)),int(random(255)),int(random(255)));
    
    bugs[i] = new Bug(c, x, y, r);
  }
  
  
  // SOM 
  minim = new Minim(this);
  minim.debugOn();
  in = minim.getLineIn(Minim.STEREO, 2048);
  
}

void draw() {
  background(255,30);
  translate(width/2,height/2);
  stroke(1);
  fill(255);
  ellipse(0,0,raioCircunferencia,raioCircunferencia);
  
  float som = in.mix.level();
  
  
  for (Bug b : bugs) {
    b.move(som);
    b.display();
  }
}

void stop()
{
  // always close Minim audio classes when you are done with them
  in.close();
  minim.stop();
  super.stop();
}


class Bug {

  color c;
  color bugColor;
  float x;
  float y;
  float r;
  float speed = 2.50;

  Bug (color cor, float posX, float posY, float raio) {

    c = cor;
    x = posX;
    y = posY;
    r = raio;
  }

  void move(float audio) {
    float tx, ty;
    
    tx = x;
    ty = y;
    
    speed = map(audio, 0.00001, 1, 10, 30);
    
    tx += random(-speed, speed);
    ty += random(-speed, speed);
    
    if(tx <= -raioCircunferencia || tx >= raioCircunferencia || ty <= -raioCircunferencia || ty >= raioCircunferencia){
      tx = x;
      ty = y;
    }
    
    x = tx;
    y = ty;
    
  }

  void display() {
    if (x <= 0) {
      bugColor = color(255, 0, 0);
    } 
    else {
      bugColor = color(0, 0, 255);
    }
    
    fill(bugColor);
    noStroke();
    ellipse(x, y, r, r);
  }
}

Yes you can do (in the function move) :

    float distance = dist(tx,ty,0,0);
    
    if(distance >= raioCircunferencia){
      tx = x;
      ty = y;
    }

And you have to say :

ellipse(0,0,raioCircunferencia*2,raioCircunferencia*2);

So i got this running and working! Thanks!
Another question, can i make so if i press the mouse and make the ellipse appear, the bugs that are outside pop and a new one is created?

// Importar Bibliotecas
import ddf.minim.*;
import processing.video.*;


PImage imgBug;
// Array de Bugs
Bug[] bugs = new Bug[50];

float raioCircunferencia;
float meioRaio;
float x;
float y;
int click = 0;


Minim minim;
AudioInput in;

Capture cam;

void setup() {
  size(2000, 2000);
  smooth();
  raioCircunferencia = 1000;
  meioRaio = raioCircunferencia/2;
  
  background(255);
  
  for (int i = 0; i < bugs.length; i++) {
    float a = random(TWO_PI);
    
    x = width/2+cos(a)*random(meioRaio);
    y = height/2+sin(a)*random(meioRaio);
    
    float r = random(50,200);
    color c = color(int(random(255)),int(random(255)),int(random(255)));
    
    bugs[i] = new Bug(c, x, y, r);
  }
  
  
  // SOM 
  minim = new Minim(this);
  minim.debugOn();
  in = minim.getLineIn(Minim.STEREO, 2048);
  
}

void draw() {
  
  if(click == 0){
    stroke(0);
    strokeWeight(10);
    fill(255,30);
    ellipse(width/2,height/2,raioCircunferencia,raioCircunferencia);
  }
  
  float som = in.mix.level();
  
  fill(255,30);
  rect(0,0,width,height);
  
  
  for (int i = 0; i < bugs.length; i++) {
    bugs[i].move(som);
    bugs[i].display();
  }
}

void stop()
{
  // always close Minim audio classes when you are done with them
  in.close();
  minim.stop();
  super.stop();
}

class Bug {

  color c;
  color bugColor =  color(random(int(255)),random(int(255)),random(int(255)));
  float x;
  float y;
  float r;
  float speed = 2.50;

  Bug (color cor, float posX, float posY, float raio) {

    c = cor;
    x = posX;
    y = posY;
    r = raio;
  }

  void move(float audio) {
    float tx, ty;
    
    tx = x;
    ty = y;
    
    speed = map(audio, 0.00001, 1, 10, 30);
    
    tx += random(-speed, speed);
    ty += random(-speed, speed);
    
    float distance = dist(tx,ty,width/2,height/2);
    
    if (click == 0){
      if(distance >= meioRaio){
        tx = x;
        ty = y;
      }
    }
    
    
    if(tx <= 0 || tx >= width || ty <= 0 || ty >= height){
      tx = x;
      ty = y;
    }
    
    x = tx;
    y = ty;
    
  }

  void display() {
    //if (x <= width/2) {
    //  bugColor = color(255, 0, 0);
    //} 
    //else {
    //  bugColor = color(0, 0, 255);
    //}
    
    //fill(bugColor);
    
    fill(bugColor);
    noStroke();
    ellipse(x, y, r, r);
  }
}

void mousePressed(){
  click ++;
  if(click > 1) click = 0;
}

Yes, you should use : Reference / Processing.org
And do :

bugs.remove(i);

if the distance is higher than raiocircunferencia when the mouse is pressed.

Ok, got that part working…

Getting the IndexOutOfBoundsException, i’m making the loop backwards so i don’t skip any index.

// Importar Bibliotecas
import ddf.minim.*;
import processing.video.*;


PImage imgBug;
// Array de Bugs
ArrayList<Bug> arrayBugs;

float raioCircunferencia;
float meioRaio;
float x;
float y;
int numBugs = 100;
int click = 0;


Minim minim;
AudioInput in;

Capture cam;

void setup() {
  size(2000, 2000);

  smooth();

  raioCircunferencia = 1000;
  meioRaio = raioCircunferencia/2;

  arrayBugs = new ArrayList<Bug>();

  background(255);

  for (int i = numBugs-1; i >= 0; i--) {
    float a = random(TWO_PI);

    x = width/2+cos(a)*random(meioRaio);
    y = height/2+sin(a)*random(meioRaio);

    float r = random(50, 200);
    color c = color(int(random(255)), int(random(255)), int(random(255)));

    arrayBugs.add(new Bug(c, x, y, r));
  }


  // SOM 
  minim = new Minim(this);
  minim.debugOn();
  in = minim.getLineIn(Minim.STEREO, 2048);
}

void draw() {

  if (click == 0) {
    stroke(0);
    strokeWeight(10);
    fill(255, 30);
    ellipse(width/2, height/2, raioCircunferencia, raioCircunferencia);
  }

  float som = in.mix.level();
  noStroke();
  fill(255, 30);
  rect(0, 0, width, height);


  for (int i = numBugs-1; i >= 0; i--) {
    Bug b = arrayBugs.get(i);
    b.move(som);
    b.display();
    if(b.morre()){
      arrayBugs.remove(i);
    }
  }
}

void stop()
{
  // always close Minim audio classes when you are done with them
  in.close();
  minim.stop();
  super.stop();
}

class Bug {

  color c;
  color bugColor =  color(random(int(255)), random(int(255)), random(int(255)));
  float x;
  float y;
  float r;
  float speed = 2.50;
  int opacidade = 100;

  Bug (color cor, float posX, float posY, float raio) {

    c = cor;
    x = posX;
    y = posY;
    r = raio;
  }

  void move(float audio) {
    float tx, ty;

    tx = x;
    ty = y;

    float distance = dist(tx, ty, width/2, height/2);

    speed = map(audio, 0.00001, 1, 10, 30);

    tx += random(-speed, speed);
    ty += random(-speed, speed);

    if (click == 0) {
      if (distance > meioRaio) {
        tx = x;
        ty = y;
      }
    }

    if (tx <= 0 || tx >= width || ty <= 0 || ty >= height) {
      tx = x;
      ty = y;
    }

    x = tx;
    y = ty;
  }

  void display() {
    //if (x <= width/2) {
    //  bugColor = color(255, 0, 0);
    //} 
    //else {
    //  bugColor = color(0, 0, 255);
    //}

    //fill(bugColor);
    float tx, ty;

    tx = x;
    ty = y;

    float distance = dist(tx, ty, width/2, height/2);
    if (distance > meioRaio && click == 0) {
      fill(bugColor, opacidade);
      opacidade--;
    } else {
      fill(bugColor, 100);
    }

    noStroke();
    ellipse(x, y, r, r);
  }
  
  boolean morre() {
    if(opacidade < 0){
      return true;
    }
    else {
      return false;
    }
  }
}

void mousePressed() {
  click ++;
  if (click > 1) click = 0;
}

It is because you have to use arrayBugs.size() in the for loops not numBugs :

For example

for (int i = arrayBugs.size()-1; i >= 0; i--) {
    Bug b = arrayBugs.get(i);
    b.move(som);
    b.display();
    if(b.morre()){
      arrayBugs.remove(i);
    }
  }

But you use numBugs when you initialize your array of bugs.

1 Like

Oh, ok! Thank you so very much!:v:

1 Like