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 › Not sure what would be causing this
Page Index Toggle Pages: 1
Not sure what would be causing this (Read 933 times)
Not sure what would be causing this
May 19th, 2009, 9:34pm
 
I'm trying to make an animation similar to the header on this site, where circles drift upwards connecting with others close by. I'm using a vector to store all the circle instances and when I loop through to display everything in the vector sometimes all the circles aren't displayed. This leads to them "flickering". Any help/ideas of why this would happen are appreciated.
Thank You (sorry it's so messy right now)
Code:
//Drifting Nodes
//5.19.2009
//===== Warning: code is very messy =====
Vector pluses = new Vector();
color backGround = color(75); //Background color
//PFont font;
float connectionDist = 135;

void setup()
{
size(1280, 720);
smooth();
frameRate(30);
//font = loadFont("FreeSansBold-22.vlw");
//textFont(font, 22);
strokeWeight(2);

}
void draw()
{
background(backGround);

for (int i=0;i<pluses.size();i++)
{
plus p = (plus)pluses.elementAt(i);
/*
for(int k=0;k<pluses.size();k++)
{
plus q = (plus)pluses.elementAt(k);
if( dist(p.x,p.y,q.x,q.y) < connectionDist)
{
stroke(0, map(dist(p.x,p.y,q.x,q.y),0,connectionDist,255,0));
line(p.x,p.y,q.x,q.y);
}
}
*/
p.display();
if(p.alive == false)
{
pluses.remove(i);
}
}

if(frameCount % 8 == 0) //Speed of plus spawn (lower number yields a faster spawn rate)
{
makePlus();
}
//========== Save IMG Sequence ==========
saveFrame("DriftingNodes-####.png");
//print("Seconds running: ");
println(frameCount);
//=======================================
//fill(0);
//text("Drifting Nodes Beta 5/19/2009",10,25);
}

void makePlus()
{
color col;
//col = color(random(235, 255), random(110, 191), random(8, 41)); //Color range for pluses
col = color(random(0, 170), 220, random(0, 200)); //Green
//col = color(random(224, 255), random(80, 163), random(5, 19)); //Orange
//col = color(random(20, 50), random(77, 165), random(180, 240)); //Blue
//col = color(random(255), random(255), random(255)); //All Colors
//col = color(random(50,110));
//col = color(random(100,255), random(200,230), random(200,230)); //Pastel
pluses.addElement(new plus(random(-50, width+50), height+50, random(0.60,1.85), col, random(0.75,1.65), random(-0.25,0.25)));
}

class plus
{
boolean alive;
float x, y;
float intialScale;
color col;
float changeSpeed, changeDirection;
//float alphaFade;

plus(float xpos, float ypos, float intialScale, color col, float changeSpeed, float changeDirection)
{
x=xpos;
y=ypos;
this.intialScale=intialScale;
alive=true;
this.col=col;
this.changeSpeed = changeSpeed;
this.changeDirection = changeDirection;
}

void display()
{
int connectionsCount = 0;
for(int k = 0; k < pluses.size(); k++)
{
plus p = (plus)pluses.elementAt(k);
if( dist(x, y, p.x, p.y) < connectionDist)
{
stroke(0, map(dist(x,y,p.x,p.y),0,connectionDist,255,0));//*alphaFade/255);
line(x,y,p.x,p.y);
connectionsCount++;
}
}
stroke(0);
fill(0);
ellipse(x,y,7*this.intialScale,7*this.intialScale);
stroke(0,90);
fill(this.col, 90);
ellipse(x,y,30*this.intialScale,30*this.intialScale);

// Display number of connections each node is holding
// stroke(0);
// fill(0);
// connectionsCount-=1;
// text(connectionsCount,x+2,y-5);

// alphaFade = map(y, height+100, -100, 255, 0);//255 * (1 - (y / height));
y-=(this.intialScale/2)*this.changeSpeed;
x+=changeDirection;
if(y < -100 || x < -30 || x > width+30)
{
alive = false;
}
}
}
Re: Not sure what would be causing this
Reply #1 - May 19th, 2009, 11:31pm
 
A few quick ideas before going to work:
1) Vector is old and somehow slow (not sure it makes a difference here), you should use ArrayList
2) Look at reference (I will do later too), some collections doesn't like when you remove elements in them while iterating on them. Perhaps make a loop to display and one to remove dead nodes.
Re: Not sure what would be causing this
Reply #2 - May 20th, 2009, 6:20am
 
that's running slow for me as it's saving every frame as a png! comment out that, and the println, and it's fine.

however, your nested loop compares each pair of nodes twice. for instance it compares node 2 with node 3 and then later compares node 3 with node 2, which is twice the amount of work it needs to do. it's also comparing each node with itself which is a bit pointless.

you might also want to put a limit on the total number of nodes and only add new ones after another has died
Re: Not sure what would be causing this
Reply #3 - May 20th, 2009, 6:29am
 
ah, ok, you weren't complaining about the speed but the flicker and missing circles. the missing circles are almost certainly you failing to draw a circle because you've removed the previous one and everything's shifted up one (as Phi said)

A - B - C - D

counter = 1 = A
draw A
increment
counter = 2 = B
draw B
delete B
increment
counter = 3

BUT because you've just deleted B your list is now

A - C - D

and the counter is now pointing at D - you've skipped C

use Phi's suggestion of two loops. or draw them backwards, starting at the end and decrementing - any deletions then only renumber the things you've already drawn...
Re: Not sure what would be causing this
Reply #4 - May 20th, 2009, 12:40pm
 
Yes, separating remove and display into separate loops fixed the flickering problem, thank you (and sorry about leaving the saveFrame() in there).

I wasn't sure how to compare all the nodes without comparing each pair twice, what would be a good way of doing this?
Re: Not sure what would be causing this
Reply #5 - May 20th, 2009, 12:51pm
 
just start the inner loop after the current value of the outer loop (this is complicated in your code as you've moved the inner loop to the display method but...)

Code:

for (int i = 0 ; i < size ; i++) {
 for (int j = i + 1 ; j < size ; j++) {
   ...
 }
}


with 5 nodes:

i = 0
j = 1, 2, 3, 4
i = 1
j = 2, 3, 4
i = 2
j = 3, 4
i = 3
j = 4
i = 4
j = er...
(actually, you don't need to bother with that last one, can use (size - 1) in the i condition)

that's 10 compares vs 25...
Re: Not sure what would be causing this
Reply #6 - May 20th, 2009, 12:55pm
 
> and sorry about leaving the saveFrame() in there

ha, that's ok. and i've *just* realised that that may've been the reason i started getting those 'short of disk space' warnings. 8)
Re: Not sure what would be causing this
Reply #7 - May 21st, 2009, 2:22am
 
(was only 11M when i looked this morning)
Page Index Toggle Pages: 1