We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
Ball Spawn (Read 1041 times)
Ball Spawn
Mar 29th, 2010, 11:28am
 
Hello Processing!

Ive been having a wee bit of trouble with my code.

Currently i have Balls falling down, and when my mouse hovers over them they disappear, and more balls spawn and keep falling down.

Now instead of them falling down from the top of the screen, i have decided to make then pop up/out, in different/random areas on screen, also making the balls not overlap each other (which is whats happening now as the balls fall from the top of the screen)....

Here is my code now, i just cant figure out how to get the balls to spawn randomly on screen rather than falling down.

Thanks alot Smiley
(this code was a mixture of my own code and a little of from blindfish's code from an earlier post)

//creating an array of Spot objects
Spot foo[] = new Spot[20];

int tRadius = 50;
boolean colFlag = false;

void setup(){
 size(640,480);
 
 for(int i=0; i<foo.length; i++){
   foo[i] = new Spot((int)random(640), (-1)*tRadius, tRadius);
 }
}

//this is my collision detection
boolean collisionD(int uX, int uY, int uW, int uH, int bX, int bY, int bW, int bH){
 boolean col=false;
 if(uX+uW/2 > bX-bW/2 && uX-uW/2 < bX+bW/2 && uY+uH/2 > bY-bH/2 && uY-uH/2 < bY+bH/2){
   col = true;
 }
 return col;
}


void draw(){
 background(50);
 
 //draw mouse circle
 ellipse(mouseX,mouseY,30,30);
 
 for(int i=0;i<foo.length;i++) {
   colFlag = collisionD(mouseX,mouseY,30,30,foo[i].getX(),foo[i].getY(),tRadius,tRadius);
   if(colFlag==true || foo[i].getY() > 480){
     foo[i].setX((int)random(640));
     foo[i].setY((-1)*tRadius);
   }else{
    foo[i].setY(foo[i].getY()+1);
    foo[i].display();
   }
 }
 
}

//our Spot class, this is a Spot object.  Think of it as an actual spot with the properties X,Y,Radius(spotX,spotY,spotRadius)
class Spot {
 int spotX, spotY, spotRadius;
 
 //constructor it takes X, Y and Radius.  This gets called when you write       Spot whatever = new Spot(50,50,50);
 Spot(int theX, int theY, int theRadius){
   spotX = theX;
   spotY = theY;
   spotRadius = theRadius;
 }
 
 //sets the X value of the spot object
 void setX(int newX){
   spotX = newX;
 }
 //" y value
 void setY(int newY){
   spotY = newY;
 }
 
 //get the x value of the spot object
 int getX(){
   return spotX;
 }
 //" y value
 int getY(){
   return spotY;
 }
 //display the spot object
 void display(){
   ellipse(spotX,spotY,spotRadius,spotRadius);
 }
 void destroy(){
   spotX = 0;
   spotY = 0;
   spotRadius=0;
 }
}



//again Thanks alot Cheesy
Re: Ball Spawn
Reply #1 - Mar 29th, 2010, 7:13pm
 
Quote:
class FallingBall {
  float X, Y, dY, R;
  float maxR;
  color C;
  boolean removeMe;
  FallingBall(float _X, float _Y, float _dY, float _R){
    X=_X;
    Y=_Y;
    dY=_dY;
    maxR=_R;
    R = 0;
    C = color(random(128,255),random(128,255),random(128,255));
    removeMe = false;
  } 
  void simulate(){
    if( R < maxR && !shrink ){
      R++;
      return;
    }
    Y+=dY;
    if( Y > height + 30 ){
      Y = -30;
      shrink = true;
      mouseC = C;
    }
    if( !removeMe && (2*dist(mouseX, mouseY, X, Y) < R + mouseR )){
      removeMe = true;
      if( fallers.size() < MAX_FALLERS ){
        float R1 = random(30,50);
        float R2 = random(30,50);
        fallers.add( new FallingBall( random(0+R1,width-R1), random(0+R1,height-R1), dY+.05, R1) );
        fallers.add( new FallingBall( random(0+R2,width-R2), random(0+R2,height-R2), 1, R2) );
      }
    }
    if( shrink ){ R--; }
    if( R <= 0 ){ removeMe = true; }
  }
  void render(){
    fill(mousePressed?color(random(255),random(255),random(255)):C);
    ellipse(X,Y,R,R);
  }
  boolean alive(){
    return !removeMe;
  }
}

int MAX_FALLERS = 500;
boolean shrink = false;
ArrayList fallers;
float mouseR = 25;
color mouseC = color(random(128,255),random(128,255),random(128,255));

void setup(){
  size(400,400);
  ellipseMode(CENTER);
  noStroke();
  fallers = new ArrayList();
  fallers.add( new FallingBall( random((width/2.0)-100)+50, random((width/2.0)-100)+50, 1, 50) );
}
void draw(){
  simulate();
  render();
}
void simulate(){
  for( int i=fallers.size()-1;i>=0;i--){
    ((FallingBall) fallers.get(i)).simulate();
  }
  if( shrink && fallers.size() == 0 ){
    shrink = false;
    fallers.add( new FallingBall( random((width/2.0)-100)+50, random((width/2.0)-100)+50, 1, 50) );
  }
}
void render(){
  background(mousePressed?color(random(255),random(255),random(255)):0);
  for( int i=fallers.size()-1;i>=0;i--){
    FallingBall temp = (FallingBall) fallers.get(i);
    if( temp.alive() ){
      temp.render();
    } 
    else {
      fallers.remove(i);
    }
  }
  fill(mousePressed?color(random(255),random(255),random(255)):mouseC);
  ellipse(mouseX, mouseY, mouseR, mouseR);
}



If this code looks familiar, that's because it is! This is based on the code I posted last time you asked about this program. It was so well-written the first time that such a small change in functionality was easy to add.  Cool PS: This also has rave mode.

Having them not overlap is a different problem - and not one I first noticed when reading your thread. Hang on.
Re: Ball Spawn
Reply #2 - Mar 29th, 2010, 7:31pm
 
Quote:
class FallingBall {
  float X, Y, dY, R;
  float maxR;
  color C;
  boolean removeMe;
  FallingBall(float _X, float _Y, float _dY, float _R){
    X=_X;
    Y=_Y;
    dY=_dY;
    maxR=_R;
    R = 0;
    C = color(random(128,255),random(128,255),random(128,255));
    removeMe = false;
  } 
  void simulate(int myID){
    if( R < maxR && !shrink ){
      boolean oktogrow  = true;
      for( int i=fallers.size()-1;i>=0;i--){
        FallingBall temp = (FallingBall) fallers.get(i);
        if( i != myID && !temp.removeMe ){
          if( dist( temp.X, temp.Y, X+1, Y+1) < temp.R + R ){
            oktogrow = false;
            maxR = R;
          }  
        }
      }
      if( oktogrow ){
        R++;
      }
      return;
    }
    if( R < MIN_VISABLE_RADIUS ){
      removeMe = true;
      return;
    }
    Y+=dY;
    if( Y > height + 30 ){
      Y = -30;
      shrink = true;
      mouseC = C;
    }
    if( !removeMe && (2*dist(mouseX, mouseY, X, Y) < R + mouseR )){
      removeMe = true;
      if( fallers.size() < MAX_FALLERS ){
        float R1 = random(30,50);
        float R2 = random(30,50);
        fallers.add( new FallingBall( random(0+R1,width-R1), random(0+R1,height-R1), 1, R1) );
        fallers.add( new FallingBall( random(0+R2,width-R2), random(0+R2,height-R2), 1, R2) );
      }
    }
    if( shrink ){
      R--; 
    }
    if( R <= 0 ){
      removeMe = true;
    }
  }
  void render(){
    fill(mousePressed?color(random(255),random(255),random(255)):C);
    if( R < MIN_VISABLE_RADIUS ){ fill(0,0,0,0); }
    ellipse(X,Y,R,R);
  }
  boolean alive(){
    return !removeMe;
  }
}

int MAX_FALLERS = 100;
int MIN_VISABLE_RADIUS = 10;
boolean shrink = false;
ArrayList fallers;
float mouseR = 25;
color mouseC = color(random(128,255),random(128,255),random(128,255));

void setup(){
  size(400,400);
  ellipseMode(CENTER);
  noStroke();
  fallers = new ArrayList();
  fallers.add( new FallingBall( random((width/2.0)-100)+50, random((width/2.0)-100)+50, 1, 50) );
}
void draw(){
  simulate();
  render();
}
void simulate(){
  for( int i=fallers.size()-1;i>=0;i--){
    ((FallingBall) fallers.get(i)).simulate(i);
  }
  if( shrink && fallers.size() == 0 ){
    shrink = false;
    fallers.add( new FallingBall( random((width/2.0)-100)+50, random((width/2.0)-100)+50, 1, 50) );
  }
}
void render(){
  background(mousePressed?color(random(255),random(255),random(255)):0);
  for( int i=fallers.size()-1;i>=0;i--){
    FallingBall temp = (FallingBall) fallers.get(i);
    if( temp.alive() ){
      temp.render();
    } 
    else {
      fallers.remove(i);
    }
  }
  fill(mousePressed?color(random(255),random(255),random(255)):mouseC);
  ellipse(mouseX, mouseY, mouseR, mouseR);
}



And here's the version that has a lot less (maybe no?) overlapping circles.  Smiley
Re: Ball Spawn
Reply #3 - Mar 31st, 2010, 2:59pm
 
Guys, i cant thank you enough for this!! you guys are amazing!! thanks for the help...

Instead of getting the balls to fall down, how do i get them to simply despawn in their place if my mouse doesnt go over them...

Spawn.. Increase in size.. reach maximum size... decrease in size.. despawn..???

if that makes sense?

<3
Page Index Toggle Pages: 1