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
Help Please. (Read 386 times)
Help Please.
Mar 26th, 2008, 1:38am
 
Hey i just got into processing and was wondering
how i could make the ball explode once its touched the
constrained wall.

Finding it difficult with the codes still but really keen to learn. I pretty much follwed the constrain code and trying to make it explode. any help would be much apreciated Smiley
This is what i have so far:

// Code Setup For Box And Ball Delay >>>>>>>>>>>>>>>>>>>>>>>

float mx;
float my;
float delay = 70.0;
float esize = 20.0;
int box = 30;

void setup()
{
 size(400, 400);
 
 noStroke();
 smooth();
 ellipseMode(RADIUS);  
}

void draw()
{
background(255);
// The Cordinates Of The Mouse >>>>>>>>>>>>>>>>>>>>>>>>>

// if(abs(mouseX - mx) > 0.5) {
   mx = mx + (mouseX - mx)/delay;
//  }
// if(abs(mouseY - my) > 0.5) {
   my = my + (mouseY- my)/delay;
// }
 
// Box That The Ball Is Contained  >>>>>>>>>>>>>>>>>>>>>>>>>>

 float distance = esize * 2;
 mx = constrain(mx, box+distance, width-box-distance);
 my = constrain(my, box+distance, height-box-distance);


// The Black Box >>>>>>>>>>>>>>>>>>>>>>>>>>>

 fill(0);
rect(box+esize, box+esize, box*10, box*10);
 fill(255);  
 ellipse(mx, my, esize, esize);
}


Re: Help Please.
Reply #1 - Mar 26th, 2008, 3:30pm
 
Hi Fillypo,

There are two thing you should consider:

1. You need to set a "trigger" for your explosion. In you program it's the exact moment at which the ball crosses the left, right, top or bottom border. As far as I know, using constrain() doesn't allow you to catch that moment.  I've rewritten the code, using a different approach on how to check the position of the ball.

In the following example I have one big test to determine if the ball has crossed on of the four borders. The individual tests are combined together using the logical OR ( || ) operator. The ball simulates the explosion by turning red.

Code:

// Code Setup For Box And Ball Delay >>>>>>>>>>>>>>>>>>>>>>>

float mx;
float my;
float delay = 70.0;
float esize = 20.0;
int box = 30;

void setup() {
 size(400, 400);  
 noStroke();  
 smooth();
 ellipseMode(RADIUS);  
}

void draw() {  
 background(255);
 // The Cordinates Of The Mouse >>>>>>>>>>>>>>>>>>>>>>>>>

 mx = mx + (mouseX - mx)/delay;
 my = my + (mouseY- my)/delay;

 // The Black Box >>>>>>>>>>>>>>>>>>>>>>>>>>>
 fill(0);
 rect(box+esize, box+esize, box*10, box*10);


 // The Ball position  >>>>>>>>>>>>>>>>>>>>>>>>>>
 float distance = esize * 2;

 // this checks of the mouse leaves on the left side OR the right side OR the top side OR the bottom side
 if (mx <= distance+box || mx >= width-box-distance || my <= box+distance || my >= height-box-distance) {
   fill(255,0,0); // "explode"    
 }
 else {
   fill(255);
 }

 // The Ball  >>>>>>>>>>>>>>>>>>>>>>>>>>
 ellipse(mx, my, esize, esize);
}



2. You should be more clear about how you want the "explosion" to look like. It could be abstract, like turning red, blinking or growing in size and then vanishing. Or it could be that the ellipse bursts into several smaller pieces, which fly all over the screen. This would be a much more complex solution. The good thing is, that once you've determined a trigger, it will be relatively easy to make the explosion ever more complex.

Hope this is of any help.

cheers,
Greg
Page Index Toggle Pages: 1