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.
IndexProgramming Questions & HelpPrograms › Multiple Ball Drops!
Page Index Toggle Pages: 1
Multiple Ball Drops! (Read 733 times)
Multiple Ball Drops!
Mar 23rd, 2010, 5:57pm
 
Hey all,

Im working on a project that will eventually have motion capture/detection... but for now, i will simulate the mouse to be the User.
(final project will consist of balls falling, and a user popping them with his hand before they reach the ground) for now, i want to get the program working with the mouse.

I looked at other threads and saw (processing.org/discourse/yabb2/num_1261940312__the_raining_circles_thread___it_will_be_somewhat_similar_to_that__except_i_don.html't think i want that many balls, no overlaping/crossing over each other.

Right now, i have 1 ball dropping down, and as soon as the my mouse goes over that ball (ellipse) it re-spawns... i want it to get destroyed, and another ball spawns and falls down in its place.. then eventually have multiple balls come down at the same time..

Here is my code so far (i hope what i typed above made some kind of sense :S)



int y=0;
int x=0;

void setup(){
 size(640,480);
 x=width/2;
}

//this is my collision detection (u being user, b, being ball)

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);
 ellipse(mouseX,mouseY,30,30);
 
 ellipse(x,y,50,50);
 y++;
 
 boolean colFlag = collisionD(mouseX,mouseY,30,30,x,y,50,50);
 
 if (colFlag == true){
   y=-60;  //this is where the ball respawns back at top.. but i want it to get destoyed and another ball to fall down
 }
}

//thanks guys! excited to get this to fully work Cheesy:D
Re: Multiple Ball Drops!
Reply #1 - Mar 23rd, 2010, 6:44pm
 
Quote:
class FallingBall {
  float X, Y, dY, R;
  color C;
  boolean removeMe;
  FallingBall(float _X, float _Y, float _dY, float _R){
    X=_X;
    Y=_Y;
    dY=_dY;
    R=_R;
    C = color(random(128,255),random(128,255),random(128,255));
    removeMe = false;
  } 
  void simulate(){
    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), -30, dY+.05, R1) );
        fallers.add( new FallingBall( random(0+R2,width-R2), -30, 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( width/2.0, -30, 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( width/2.0, -30, 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);
}


Cool HOLD DOWN THE MOUSE BUTTON TO ACTIVATE RAVE MODE. Cheesy
Re: Multiple Ball Drops!
Reply #2 - Mar 23rd, 2010, 7:19pm
 
Hahahhahahah!! thanks alot!! the rave mode is great Cheesy

so, i sort of merged a bit of code together (using raining circles) (Thanks blindfish)

//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;
 }
}



Now i have what  i kinda want working a bit, but im passing out.. i think now i need fix some variables inside..

But ill post an update in the next few days Smiley
Page Index Toggle Pages: 1