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 & HelpSyntax Questions › Question about making the eruption effect
Page Index Toggle Pages: 1
Question about making the eruption effect (Read 136 times)
Question about making the eruption effect
Dec 9th, 2008, 6:52am
 
Hi. I want to make the eruption effect, that if the ball of y location goes to the ground, to split into half and bounce. I can make the ball to get smaller and bounce, but when i tried to make another ball to make it split, it goes out of array index. But I don't know how to solve. Could you give any help or idea? Thank you.

I will post the code. For the part that keep falling out of array, i put // so that it won't show any error if you played it. You should click mouse for make balls.




int numballs=10000;
float[] locx=new float[numballs];
float[] locy=new float[numballs];
float[] ma=new float[numballs];
float[] speedx=new float[numballs];
float[] speedy=new float[numballs];
int ball_length;
int ball_lengthp;
Balls[] ball=new Balls[numballs];

boolean play=false;
 float t,tt;
 
void setup()
{
 t=0;
 tt=0.2;
 size(500,500);
 background(0);
}

void draw(){
t+=tt;
 fill(0,20);
 rectMode(CORNERS);
 rect(width,height,0,0);
 if(play){
  ball_length++;
  ball[ball_length] = new Balls(mouseX,mouseY,random(-.5,.5),1,10);
 }

 for (int i = 1; i < ball_length+1+ball_lengthp; i++) {
   
  if(ball[i].y>height-100){
  ball[i] = new Balls(ball[i].x,ball[i].y,ball[i].xspeed/2,ball[i].yspeed*-1,ball[i].ma/2);
    //ball_lengthp++;
    //ball[ball_length+ball_lengthp] = new Balls(ball[i].x,ball[i].y,-ball[i].xspeed/2,ball[i].yspeed*-1,ball[i].ma/2);  
 }
    ball[i].move();
   ball[i].display();
 }
 }


void mousePressed(){
  play=true;  
}

 void mouseReleased(){
   play=false;
 }
 

class Balls{
 float x=250;
 float y=250;

 float ma;
 float xspeed;
 float yspeed;

 Balls(float xloc,float yloc,float speedx,float speedy,float mass) {
   x=xloc;
   y =yloc;
   xspeed = speedx;
   yspeed = speedy;
   ma=mass;
 }


 void move() {

   yspeed+=1;
    x+=xspeed;
   y +=yspeed;

   if(x>width-100){
    xspeed*=-1;    
  }  
 }

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

Re: Question about making the eruption effect
Reply #1 - Dec 9th, 2008, 12:47pm
 
Frankly, I am not too sure of what is going on, but I didn't want to rewrite completely the code... Smiley
Here is a small fix, which has still some rough edges, but it should put you on the right track.
One of the biggest problem was to have too counters on the array, it was confusing.
Code:
int numballs=10000;
int ball_length;
Balls[] ball=new Balls[numballs + 1];

boolean play=false;
 float t,tt;
 
void setup()
{
 size(500,500);
 t=0;
 tt=0.2;
 rectMode(CORNERS);
 background(0);
}



void draw(){
 t+=tt;
 fill(0,20);
 rect(width,height,0,0);
 if(play && ball_length < numballs){
  ball[ball_length++] = new Balls(mouseX,mouseY,random(-.5,.5),1,10);
 }
 for (int i = 0; i < ball_length; i++) {
  if(ball[i].y > height - 100 && ball[i].yspeed > 0){
    ball[i].update(0.5, -1, 0.5);
    if (ball_length < numballs)
    {
      ball[ball_length++] = new Balls(
          ball[i].x,
          ball[i].y,
          -ball[i].xspeed/2,
          ball[i].yspeed*-1,
          ball[i].ma);    
    }
    break;
  }
  ball[i].move();
  ball[i].display();
 }
}


void mousePressed(){
  play=true;  
}

 void mouseReleased(){
   play=false;
 }
 

class Balls{
 float x=250;
 float y=250;

 float ma;
 float xspeed;
 float yspeed;

 Balls(float xloc,float yloc,float speedx,float speedy,float mass) {
   x=xloc;
   y =yloc;
   xspeed = speedx;
   yspeed = speedy;  
   ma=mass;
 }

 void update(float speedfx,float speedfy,float massf) {
   xspeed *= speedfx;
   yspeed *= speedfy;  
   ma *= massf;
 }
 
 void move() {

   yspeed+=1;
    x+=xspeed;
   y +=yspeed;

   if(x>width-100){
    xspeed*=-1;      
  }  
 }

 void display() {
   fill(255);
   noStroke();
   ellipse(x,y,ma,ma);
 }
}
Page Index Toggle Pages: 1