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 › confusion with loadPixels()
Page Index Toggle Pages: 1
confusion with loadPixels() (Read 1620 times)
confusion with loadPixels()
May 15th, 2010, 8:51pm
 
Hi All:

I'm trying to display an image in two layers: first, I display an ever-growing trail of pheromones secreted by individual 'Ant' objects, then I display the ants on top of it all.

I thought that by storing the pixel array prior to drawing the ants themselves, I could effectively make it look as though the ants are moving around, but their pheromone trails stay behind. For some reason, my ants are trailing too.

Thanks in advance for your help!

Code:
void draw() {  
if (!firstLoop) {
loadPixels(); pixels = pheromoneMap; updatePixels();
}

for (int i=0; i<AntFarm.size()-1; i++) {
Ant thisAnt = (Ant) AntFarm.get(i);
thisAnt.walk();
thisAnt.squirt();
}

loadPixels(); pheromoneMap = pixels; updatePixels();

for (int i=0; i<AntFarm.size()-1; i++) {
Ant thisAnt = (Ant) AntFarm.get(i);
thisAnt.display();
}

firstLoop = false;
}
Re: confusion with loadPixels()
Reply #1 - May 15th, 2010, 8:53pm
 
Ah, and by way of explanation, the "squirt" method secretes the pheromone, the "display" method displays the ant itself. Since the "display" method comes after I store the pixels, I thought the ants would be drawn fresh each frame. Unfortunately that doesn't seem to be the case. Ideas?
Re: confusion with loadPixels()
Reply #2 - May 16th, 2010, 12:40am
 
By default draw() does not clear the screen before drawing the next frame - you need to do that explicitly e.g. by placing background(255); before the rest of your draw() code...
Re: confusion with loadPixels()
Reply #3 - May 16th, 2010, 2:27am
 
blindfish is right, of course, but I will add that some calls are not necessary: no need to loadPixels before overwriting them, no need to updatePixels if you don't change them (but change the map):
Code:
void draw() {  
background(255);
if (!firstLoop) {
pixels = pheromoneMap; updatePixels();
}

for (int i=0; i<AntFarm.size()-1; i++) {
Ant thisAnt = (Ant) AntFarm.get(i);
thisAnt.walk();
thisAnt.squirt();
}

loadPixels(); pheromoneMap = pixels;

for (int i=0; i<AntFarm.size()-1; i++) {
Ant thisAnt = (Ant) AntFarm.get(i);
thisAnt.display();
}

firstLoop = false;
}

But pheromoneMap = pixels; won't do what you expect: it says the map will point to the pixels of the sketch, but (perhaps depending on chosen display mode) it might be updated as you update the display, ie. it can be the displayed ants.
You should use get() instead, making a copy of the pixels.
Re: confusion with loadPixels()
Reply #4 - May 16th, 2010, 8:13am
 
Thanks guys! That's a big help. Here's what ended up working for me. Is using a for-loop to update the pheromoneMap variable an excessively slow way to go?

Code:
void draw() {  
clearDisplay();

if (!firstLoop) {
pixels = pheromoneMap; updatePixels();
}

for (int i=0; i<AntFarm.size()-1; i++) {
Ant thisAnt = (Ant) AntFarm.get(i);
thisAnt.walk();
thisAnt.squirt();
}

loadPixels();
for(int i=0; i<pheromoneMap.length; i++) { pheromoneMap[i] = pixels[i]; }

for (int i=0; i<AntFarm.size()-1; i++) {
Ant thisAnt = (Ant) AntFarm.get(i);
thisAnt.display();
}

firstLoop = false;
}

void clearDisplay() {
background(255);
noFill(); stroke(230); strokeWeight(1);
ellipse(width/2,height/2,20,20);
ellipse(width/2,height/2,350,350);
}
Re: confusion with loadPixels()
Reply #5 - May 16th, 2010, 10:27am
 
You might use arrayCopy() to do the pixel copy, it might be faster (or not, but at least, it is simpler to write!).
Re: confusion with loadPixels()
Reply #6 - May 17th, 2010, 9:23am
 
Perfect. Cheers!
Page Index Toggle Pages: 1