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 › [newbie] - adapting code to 0090
Page Index Toggle Pages: 1
[newbie] - adapting code to 0090 (Read 1610 times)
[newbie] - adapting code to 0090
May 11th, 2005, 1:18am
 

hi there,

i'm a newbie using proce55ing and currently trying to convert an old code, written for a beta version older than 0070, to beta 0090. its a particle system with attractors. i've already made some changes, i.e. BImage to PImage, etc. But I don't really know how to change imageMode(CENTER_DIAMETER). And the script does not work... does anybody have an idea...??!?

code:

int NUM_PARTICLES  = 10000;
int NUM_ATTRACTORS = 8;

int SPRITE_SIZE = 2;

Particle[] particle;
Attractor[] attractor;

float damp = 0.0002;
float accel = 10000;

PImage part;

class Particle {
 float x,y,vx,vy;
 Particle() {
   x = random(width);
   y = random(height);
   vx = random(-accel/2,accel/2);
   vy = random(-accel/2,accel/2);
 }
 void step() {
   for (int i = 0; i < attractor.length; i++) {
     float d2 = sq(attractor[i].x-x) + sq(attractor[i].y-y);
     if (d2 > 0.1) {
       vx += accel * (attractor[i].x-x) / d2;
       vy += accel * (attractor[i].y-y) / d2;
     }
   }
   x += vx;
   y += vy;
   vx *= damp;
   vy *= damp;
 }
}

class Attractor {
 float x,y;
 Attractor() {
   x = random(width);
   y = random(height);
 }
}

void setup() {
 size(800,600);
 part = new PImage(SPRITE_SIZE,SPRITE_SIZE);
 attractor = new Attractor[NUM_ATTRACTORS];
 particle = new Particle[NUM_PARTICLES];
 scatter();
 float md = 0.5*sqrt(sq(part.width/2)+sq(part.height/2));
 for (int x = 0; x < part.width; x++) {
   for (int y = 0; y < part.height; y++) {
     float d = sqrt(sq(x-part.width/2)+sq(y-part.height/2));
     float r = 255-(255*d/md);
     float g = 128-(128*d/md);
     float b = 32-(32*d/md);
     part.set(x,y,color(r,g,b,8-8*d/md));
   }  
 }
 imageMode(CENTER_DIAMETER);
}

void loop() {
 for (int j = 0; j < particle.length; j++) {
   particle[j].step();
   blend(part,0,0,part.width,part.height,(int)particle[j].x-part.width/2,(int)particle[j].y-part.height/2,(int)particle[j].x+part.width/2,(int)particle[j].y+part.height/2,ADD);
 }
}

void scatter() {
 background(0);
 for (int i = 0; i < attractor.length; i++) {
   attractor[i] = new Attractor();
 }
 for (int i = 0; i < particle.length; i++) {
   particle[i] = new Particle();
 }
}

void mousePressed() {
 scatter();
}
Re: [newbie] - adapting code to 0090
Reply #1 - May 11th, 2005, 1:27am
 
change "void loop()" to be "void draw()" and imageMode(CENTRE_DIAMETER) is unfortunately not around, so you may have to just subtrace image.width/2 and image.height/2 from the co-ordinates of call to draw the image.
Re: [newbie] - adapting code to 0090
Reply #2 - May 11th, 2005, 10:28am
 
thx for your answer Smiley
but can you please specify what you wrote about the imageMode(). im did not really found out yet whats the task of this part... its my first time with processing and i try to understand how this particle/attractor system works and wanna adapt in to 0090. but i don't understand what loadImage is doing here... does it place the start point of the process in the center of the image?!? can you help me?

tia!
peter
Re: [newbie] - adapting code to 0090
Reply #3 - May 11th, 2005, 2:58pm
 
imageMode() changes the way blend() works ...

http://processing.org/reference/blend_.html
http://processing.org/reference/imageMode_.html

( blend() does not seem to work with the default java2d renderer ... so i switched to P3D in size() )
Code:

int NUM_PARTICLES = 1000;
int NUM_ATTRACTORS = 8;

int SPRITE_SIZE = 2;

Particle[] particle;
Attractor[] attractor;

float damp = 0.0002;
float accel = 10000;

PImage part;

class Particle {
float x,y,vx,vy;
Particle() {
x = random(width);
y = random(height);
vx = random(-accel/2,accel/2);
vy = random(-accel/2,accel/2);
}
void step() {
for (int i = 0; i < attractor.length; i++) {
float d2 = sq(attractor[i].x-x) + sq(attractor[i].y-y);
if (d2 > 0.1) {
vx += accel * (attractor[i].x-x) / d2;
vy += accel * (attractor[i].y-y) / d2;
}
}
x += vx;
y += vy;
vx *= damp;
vy *= damp;
}
}

class Attractor {
float x,y;
Attractor() {
x = random(width);
y = random(height);
}
}

void setup() {
size(800,600, P3D);
part = new PImage(SPRITE_SIZE,SPRITE_SIZE);
attractor = new Attractor[NUM_ATTRACTORS];
particle = new Particle[NUM_PARTICLES];
scatter();
float md = 0.5*sqrt(sq(part.width/2)+sq(part.height/2));
for (int x = 0; x < part.width; x++) {
for (int y = 0; y < part.height; y++) {
float d = sqrt(sq(x-part.width/2)+sq(y-part.height/2));
float r = 255-(255*d/md);
float g = 128-(128*d/md);
float b = 32-(32*d/md);
part.set(x,y,color(r,g,b,8-8*d/md));
}
}
imageMode(CORNER);
}

void draw() {
for (int j = 0; j < particle.length; j++) {
particle[j].step();
blend( part, 0,0, part.width, part.height ,
(int)particle[j].x-part.width/2, (int)particle[j].y-part.height/2,
(int)particle[j].x+part.width/2,(int)particle[j].y+part.height/2, ADD );
}
}

void scatter() {
background(0);
for (int i = 0; i < attractor.length; i++) {
attractor[i] = new Attractor();
}
for (int i = 0; i < particle.length; i++) {
particle[i] = new Particle();
}
}

void mousePressed() {
scatter();
}
Re: [newbie] - adapting code to 0090
Reply #4 - May 17th, 2005, 4:50pm
 

ahhh, ok....
thx fjen!
Re: [newbie] - adapting code to 0090
Reply #5 - May 17th, 2005, 4:51pm
 
thanks a lot - now i understand (a little bit more... *g*)

i tried to adapt this one next - it doesn't work...
changed loop() to draw() but nothing happens...
do you have an idea for this one??

i know, i'm an annoying newbie... Wink
but its my first time with processing and i'm not very familiar with programming.
i'm trying to learn - learning by doing...
i can retrace the parts of the code, but i do not
find the specific point which is responsible for
the problem in beta 0090...

can anybody please help me?!?!

thx Smiley


code:

int NUM_PARTICLES  = 10000;
int NUM_ATTRACTORS = 6;

Particle[] particle;
Attractor[] attractor;

float damp = 0.00002;
float accel = 10000;

class Particle {
 float x,y,vx,vy;
 Particle() {
   x = random(width);
   y = random(height);
   vx = random(-accel/2,accel/2);
   vy = random(-accel/2,accel/2);
 }
 void step() {
   for (int i = 0; i < attractor.length; i++) {
     float d2 = sq(attractor[i].x-x) + sq(attractor[i].y-y);
     if (d2 > 0.1) {
       vx += accel * (attractor[i].x-x) / d2;
       vy += accel * (attractor[i].y-y) / d2;
     }
   }
   x += vx;
   y += vy;
   vx *= damp;
   vy *= damp;
   line(x,y,x,y);
 }
}

class Attractor {
 float x,y;
 Attractor() {
   x = random(width);
   y = random(height);
 }
}

void setup() {
 size(800,600);
 attractor = new Attractor[NUM_ATTRACTORS];
 particle = new Particle[NUM_PARTICLES];
 scatter();
}

void loop() {
 for (int j = 0; j < particle.length; j++) {
   particle[j].step();
 }
}

void scatter() {
 background(255);
 stroke(0,0,0,8);
 for (int i = 0; i < attractor.length; i++) {
   attractor[i] = new Attractor();
 }
 for (int i = 0; i < particle.length; i++) {
   particle[i] = new Particle();
 }
}

void mousePressed() {
 scatter();
}

Re: [newbie] - adapting code to 0090
Reply #6 - May 17th, 2005, 6:13pm
 
Ah, the problem here is that loop() should now be draw() and again that the renderer should be switched using size(800,600,P3D).

By the way - wouldn't it be nice to at least mention where you found this code?

There's a BETA-compatible release of lots of my attractor code in the works right now as it happens. If you'd asked me you could have found that out for yourself and saved yourself some trouble!

Thanks anyway everyone for helping to update my code Wink
Page Index Toggle Pages: 1