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 › bubble up recursion
Pages: 1 2 
bubble up recursion (Read 1123 times)
bubble up recursion
Nov 11th, 2008, 8:29am
 
When I look at this image I feel it move, I would like to make it bubble and burst as if it were alive. Can this Recursion example be made to look like its bubbles were randomly fermenting, seeping toward the ends?

http://www.learningprocessing.com/examples/chapter-13/example-13-8-recursion/#comment-162
Re: bubble up recursion
Reply #1 - Nov 13th, 2008, 6:11am
 
I've been told, to achieve this I would be better off using a looping array rather than recursion for the bubble popping effect, with a variable to control the size of the circles.

// Learning Processing
// Daniel Shiffman
// http://www.learningprocessing.com

// Example 4-7: Filling variables with random values


float diam;
float x;
float y;

void setup() {
 size(200,100);
 background(255);
 smooth();    
}

void draw() {
 // Each time through draw(), new random numbers are picked for a new ellipse.
 fill(132,22,88,6);
 diam = random(80);
 x = random(width+2000);
//cutting off hanf of the process is probrably wasteful, but I don't know how to make real semi-circles
 y = 100;
 
 // Use values to draw an ellipse
   ellipse(x,y,diam,diam);


}





Its probably not the right way to do it but if I use
"x = random(width+2000);" the creation of bubbles slows down, but they eventually remain and the whole area blackens, where as I would like the bubble to pop and disappear.
Re: bubble up recursion
Reply #2 - Nov 13th, 2008, 1:59pm
 
Your ellipses has transparency, nearly transparent, and they accumulate on the drawing surface, hence the darkening as the strokes cumulate.

To make them pop up and disappear after a while, you need to keep track of the bubbles in a list (a FIFO): add a new one on each draw call, draw all the bubbles in the list, remove the oldest bubble so it isn't drawn on next iteration.

I would use ArrayDeque for this FIFO, but perhaps someone more familiar than me with Collection's would suggest something else.
Re: bubble up recursion
Reply #3 - Nov 13th, 2008, 2:29pm
 
I did not resist, as I wanted to play with this ArrayDeque...
Code:
class Bubble
{
 float diam;
 float x;
 float y;
 
 Bubble()
 {
   diam = random(80);
//    x = random(width + 2000);
   x = random(width + 20);
   y = 100;
 }
 
 void draw() // Not the global draw!
 {
   fill(132, 22, 88, 6);
   ellipse(x, y, diam, diam);
 }
}

ArrayDeque bubbles;

void setup() {
 size(200, 100);
 background(255);
 smooth();
 
 bubbles = new ArrayDeque();
}

void draw() {
 background(255);
 bubbles.add(new Bubble());
 Iterator it = bubbles.iterator();
 while (it.hasNext())
 {
   Bubble b = (Bubble) it.next();
   b.draw();
 }
 if (bubbles.size() > 20)
 {
   bubbles.removeFirst();
 }
}
Re: bubble up recursion
Reply #4 - Nov 13th, 2008, 3:41pm
 
Thanks again PhiLho, but, whe nI try to run the code Processing says "cannot find a class or type named ArrayDeque", now I checked the website and its all text, nothing to download, so I'm not sure what I have to do
Re: bubble up recursion
Reply #5 - Nov 13th, 2008, 4:37pm
 
Ah, I see ArrayDeque have been added in Java 1.6... I tested it with Processing 0154 with my JDK 6. If you have an older version, you cannot run it.

Fortunately, you can actually get a very similar result with ArrayList, perhaps with a little slowdown, but I doubt it is perceptible.

Changes:
Code:
//~ ArrayDeque bubbles;
ArrayList bubbles;

void setup() {
size(200, 100);
background(255);
smooth();

//~ bubbles = new ArrayDeque();
bubbles = new ArrayList();
}

void draw() {
background(255);
bubbles.add(new Bubble());
Iterator it = bubbles.iterator();
while (it.hasNext())
{
Bubble b = (Bubble) it.next();
b.draw();
}
if (bubbles.size() > 20)
{
bubbles.remove(0);
}
}
Re: bubble up recursion
Reply #6 - Nov 13th, 2008, 5:57pm
 
Now its complaining about "Bubble", so I did a few searches to find 1.6 for the Mac but why is it this hard and won't surfers not see the Javascript? Why is it not installed on my machine as an auto update?

Is it just me, the 'dev', that needs the new Java kit, once compiled it will work for everybody? Or will a user need to download something?


http://images.spelpunt.nl/javase6release1dp6.dmg  

this is the one I have found and it only installs on OS X 10.4! I'm on 10.5.5 Leopard

Thanks
Re: bubble up recursion
Reply #7 - Nov 15th, 2008, 9:23am
 
I gave only the changed code, the Bubble class is given above and unchanged...

I don't understand the relation with JavaScript.

And indeed, I have heard that Java 1.6 is late or unstable on Mac OS (because Apple what to do it themselves, IIRC). I must remember to avoid 1.6 specific tricks...
I see in the release notes: "ABOUT REV 0150 - Updated Java to release 1.6.0_07 on Windows and Linux." and later, problems with Java 1.6 on Mac are reported: it might exist but it isn't very good.
Oh, and perhaps check Apple site for the update, if you want to upgrade.
Re: bubble up recursion
Reply #8 - Nov 15th, 2008, 10:21am
 
Bingo, sorry about that, I thought it was separate code. It works fine, really well.

Thanks again!
Re: bubble up recursion
Reply #9 - Nov 18th, 2008, 1:11am
 
Placing this on a white background makes it look seemless, except for the edges, that is I decided to have the bubble popping as a whole also, not just halves fizzing along the bottom. But I found that I had a window (with no visible edges) in my webpage and when the bubbles touched the end of the window you could tell where the edges were.

So easily enough I changed the y float to half of the height, which meant the bubbles would never touch the edges bu to change the x I would then have to give it a margin of 50 each side (the diameter being 100) but trying to add in "<50" it gave me a boolean error, I think it is this bit that needs to be changed:

   x = random(width + 20000);

Somehing there to stop it from touching the edges either side at half the diameter would keep the illusion.
Re: bubble up recursion
Reply #10 - Nov 18th, 2008, 4:55pm
 
Not sure what is the current state of your code, but in mine, I disabled the "+ 20000" line as I didn't understood it...
Re: bubble up recursion
Reply #11 - Nov 18th, 2008, 5:59pm
 
Sorry I took the code as you posted it and just changed it so the bubbles are in the middle, that's just it, the bubbles do not overlap the top or bottom but as for the side edges I don't know?



class Bubble
{
 float diam;
 float x;
 float y;
 
 Bubble()
 {
   diam = random(80);
//    x = random(width + 2000);
   x = random(width + 20);
   y = 50;
 }
 
 void draw() // Not the global draw!
 {
   fill(132, 22, 88, 6);
   ellipse(x, y, diam, diam);
 }
}

//~ ArrayDeque bubbles;
ArrayList bubbles;

void setup() {
 size(200, 100);
 background(255);
 smooth();

//~   bubbles = new ArrayDeque();
 bubbles = new ArrayList();
}

void draw() {
 background(255);
 bubbles.add(new Bubble());
 Iterator it = bubbles.iterator();
 while (it.hasNext())
 {
   Bubble b = (Bubble) it.next();
   b.draw();
 }
 if (bubbles.size() > 20)
 {
   bubbles.remove(0);
 }
}
Re: bubble up recursion
Reply #12 - Nov 18th, 2008, 9:01pm
 
Ah, OK, just change the constructor of the class:
Code:
  Bubble()
{
diam = random(80);
x = diam / 2 + random(width - diam);
y = height / 2;
}

I reduce the range of the horizontal position by twice the radius, and add a radius to ensure a margin.
Re: bubble up recursion
Reply #13 - Nov 18th, 2008, 9:23pm
 
Hurray, another victory! Thanks as always! Any change chance of making it go a bit s l o w e r
Re: bubble up recursion
Reply #14 - Nov 18th, 2008, 11:07pm
 
Yes, I found it too fast too...
Two ways:
- Just change the frameRate!
- Or add new bubble every n frames (allows to do more stuff meantime...
Code:

void draw() {
background(255);
if (frameCount % 4 == 0)
{
bubbles.add(new Bubble());
}
Iterator it = bubbles.iterator();
while (it.hasNext())
{
Bubble b = (Bubble) it.next();
b.draw();
}
if (bubbles.size() > 10 && frameCount % 4 == 0)
{
bubbles.remove(0);
}
}

Pages: 1 2