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 › bouncing blocks, getting rid of tail effect
Page Index Toggle Pages: 1
bouncing blocks, getting rid of tail effect (Read 605 times)
bouncing blocks, getting rid of tail effect
Oct 24th, 2009, 10:06am
 
Hi there,

I'm having trouble getting rid of the tail effect on the blocks. Please can you help me. I'm enclosing the code below.
Thanks,
MJ

int numBlocks = 10; // declare the number of blocks to ten
Block[] blocks = new Block[numBlocks]; // declare and create array
ChangeColors c = new ChangeColors();

void setup() {
 size(200,200);
 smooth();
 noStroke();
 
 for(int i = 0; i < blocks.length; i++) {
   float x = 10 + i*16;
   float rate = 0.5 + i*0.05;
   blocks[i] = new Block(x,50,16,rate); // create each objects
 }
}

void draw() {
 fill(0, 15); // color and opacity
 rect(0, 0, width, height);
 fill(255);
 for(int i = 0; i < blocks.length; i++) {
   blocks[i].move();
   blocks[i].display();
   c.changeColors();  // changes the color of the blocks
 }
}

class Block {
 float x,y,sz; //for rect
 float sp; //speed
 int pos=1; //position (direction)
 
 Block(float xpos, float ypos, float area, float speed) {
   x=xpos;
   y=ypos;
   sz=area;
   sp=speed;
 }

 void move() {
   y+=(sp*pos);
   if ((y > (height - sz/2)) || (y < sz/2)) {
     pos *= -1;
   }
 }
 
 void display() {
   rect(x,y,sz,sz);
 }
}

class ChangeColors {
 
 ChangeColors() {}
 void changeColors() {
   fill(random(0,255),random(0,255),random(0,255),random(0,255));
 }
}
Re: bouncing blocks, getting rid of tail effect
Reply #1 - Oct 24th, 2009, 10:25am
 
You start each frame by drawing a semi-transparent black panel:
fill(0, 15); // color and opacity
rect(0, 0, width, height);

Of course, this is not making everything from the last frame disappear completely, thus leaving trails.

Try setting a non-transparent black:
fill(0, 255);
Or, since the rectangle fills the whole display anyway, replace both lines with:
background(0);
Re: bouncing blocks, getting rid of tail effect
Reply #2 - Oct 25th, 2009, 1:42am
 
When I asked to remove the duplicate post in the other thread, I meant the one without answers... Smiley

I will remove my orphan answer to remove all trace of the old thread now, for the record I answered something similar to Christian's answer (which is more detailed).
"Replace your background with transparency (the fill+rect) lines, with a good old background() call."
Page Index Toggle Pages: 1