Quantcast
Channel: Library Questions - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 2896

Arrays. Getting separate arrays of bubbles to emit at x,y coordinates of array of moving objects.

$
0
0

I am trying to get an array of rising bubbles to emit from two bouncing moving orb objects. In the past I was able to get it to work, but it only worked for one of the orbs no matter what I tried.

I have simplified the code so that bubbles emits at mouseX, mouseY coordinates. And I have reversed mouse to show a second bubble does actually emerge at the skewed coordinates.

Any idea where I'm going wrong here. I'd appreciate the tips. Been trying this all day. Many thanks.

int numberOfOrbs = 2;
Orb_Class[] many_Orbs = new Orb_Class[numberOfOrbs];

int numberOfBubbles = 30;
Bubble_Class[] many_Bubbles = new Bubble_Class[numberOfBubbles];

int timer;


void setup()
{
  size(600, 900);
  noStroke();
  smooth(); 

  for (int i=0; i<many_Bubbles.length; i++) 
  {
    many_Bubbles[i] = new Bubble_Class(random(width), height, 10, 1);
  }


  for (int i=0; i<many_Orbs.length; i++) 
  {
    many_Orbs[i] = new Orb_Class(64, random(4, 6), random(4, 6)); 
  }
}


void draw()
{
  background( 255 );  // erase the window to black  
  stroke( 255 );      // draw using a white stroke 

  if (millis() - timer >= 500) 
  {
    for (int i=0; i<many_Bubbles.length; i++) 
    {
      many_Bubbles[0] = new Bubble_Class(mouseX, mouseY, 10, 1);
      many_Bubbles[1] = new Bubble_Class(mouseY, mouseX, 10, 1);    
    }
    timer = millis();
  }

  for (int j=0; j<many_Orbs.length; j++) 
  {
    many_Orbs[j].display();
    many_Orbs[j].move(j);  
  }


  for (int i=0; i<many_Bubbles.length; i++) 
  {
    many_Bubbles[i].ascend();
    many_Bubbles[i].display();
  }
}



class Orb_Class
{  
  float radius;   // radius
  float x; 
  float y = random(300, 600); // location
  float xspeed, yspeed; // speed  
  float xtime;  
  float ytime;

  // Constructor
  Orb_Class(float tempR, float tempXt, float tempYt) 
  {
    radius = tempR;
    xspeed = tempXt;
    yspeed = tempYt;   
  }

  void move(int arrayIndex)    // Unused for the moment
  { 
    //xspeed = incomingKickOSC;
    x += xspeed; // Increment x
    y += yspeed; // Increment y

    // Check horizontal edges 
    if (x > width || x < 0) {
      //popsound[arrayIndex].rewind();
      //popsound[arrayIndex].play();      
      xspeed *= -1;
    }
    //Check vertical edges
    if (y > height || y < 0) {
      //popsound[arrayIndex].rewind();
      //popsound[arrayIndex].play();
      yspeed *= -1;
    }    
  }

  void display()
  {  
    fill(255, 0, 0);
    noStroke();
    ellipse(x, y, radius, radius);  
  }
}



class Bubble_Class 
{
  float diameter;
  float riseSpeed;
  boolean popped = false;
  float x, y;

  Bubble_Class(float tempX, float tempY, float tempD, float tempRs) {
    x = tempX;
    y = tempY;
    diameter = tempD;
    riseSpeed = tempRs;
  }

  void ascend() {
    y = y - riseSpeed;
    x = x + random(-2, 2);
  }

  void display() {
    if (!popped) {
      stroke(0);
      fill(127, 100);
      ellipse(x, y, diameter, diameter);
    }
  }
}

I have also tried to move code within the Orb_Class and failed. I think I need to be doing something like this but my efforts are failing:

  if (millis() - timer >= 500) 
  {
    for (int i=0; i<many_Bubbles.length; i++) 
    {
      for (int j=0; j<many_Orbs.length; j++)
      {    
        many_Bubbles[0] = new Bubble_Class(somehow_Access_Array_For_Orb_PosX[j], somehow_Access_Array_For_Orb_PosY[j], 10, 1);
      } 
    }
    timer = millis();
  }

Viewing all articles
Browse latest Browse all 2896

Trending Articles