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 › problem running attractors_colur2 on pde0102 java
Page Index Toggle Pages: 1
problem running attractors_colur2 on pde0102 java (Read 657 times)
problem running attractors_colur2 on pde0102 java
Jan 27th, 2006, 2:26pm
 
Hi,

I`ve just found out about processing. I downloaded some source  from Tom cardens site called attractors_colour2. I try to run it form the pde, but I get errors. I haven`t changed the source at all. Here`s the error I`m getting...

java.lang.NullPointerException

java.lang.NullPointerException


at Temporary_7300_3813.scatter(Temporary_7300_3813.java:163)


at Temporary_7300_3813.setup(Temporary_7300_3813.java:107)


at processing.core.PApplet.display(PApplet.java:1259)


at processing.core.PGraphics.requestDisplay(PGraphics.java:520)


at processing.core.PApplet.run(PApplet.java:1142)


at java.lang.Thread.run(Unknown Source)



at Temporary_7300_3813.scatter(Temporary_7300_3813.java:163)

at Temporary_7300_3813.setup(Temporary_7300_3813.java:107)

at processing.core.PApplet.display(PApplet.java:1259)

at processing.core.PGraphics.requestDisplay(PGraphics.java:520)

at processing.core.PApplet.run(PApplet.java:1142)

at java.lang.Thread.run(Unknown Source)


Any help would be appreciated. I really like the effect this makes and would like to study how it works, but if I can`t build it I can`t see it.

Cheers,


tsd
Re: problem running attractors_colur2 on pde0102 j
Reply #1 - Jan 27th, 2006, 5:21pm
 
If you link to the code in question, it might help others who aren't so familiar with my site Smiley

http://www.tom-carden.co.uk/p5/attractors_colour2/applet/index.html

Also feel free to ask me questions on my blog, that's what it's for:

http://www.tom-carden.co.uk/p5/2004/05/attractors-and-particles.php

The code was written for Processing 0069 so it won't work with the latest versions.

Basically, you need to change loop to draw, use loadPixels and updatePixels in draw, and don't rely on pixels.length to be correct inside scatter (use width*height instead).  You might also try setting the renderer to the old P2D renderer with size(600,600,P2D), since that's what Processing 0069 used.

Note that I would probably do things differently now.  I never quite got the antialiased pixels right - I would probably attempt to implement something like Wu pixels now instead.

http://freespace.virgin.net/hugo.elias/graphics/x_wupixl.htm

Let me know how you get on,

Tom.
Re: problem running attractors_colur2 on pde0102 j
Reply #2 - Jan 27th, 2006, 5:29pm
 
Here's a version for 102+, without the unreliable antialiased points and with a few more comments.  Note that the main difference between this and the black and white versions is that the colour hue is set according to the velocities of the pixels and each point.

Code:


int NUM_PARTICLES = 8192;
int NUM_ATTRACTORS = 8;

Particle[] particle;
Attractor[] attractor;

// how much speed to keep for particles, between frames
float damp = 0;
// how attractive the attractors are
float accel = 5000;

float buffer[]; // count particle locations for each pixel
float vxbuffer[]; // count particle x speed for each pixel
float vybuffer[]; // count particle y speed for each pixel

// for scaling the colour values (use mouse to set contrast)
float power = 0.4;

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;
deposit(x,y,vx,vy);
vx *= damp;
vy *= damp;
}
}

void deposit(float x, float y, float vx, float vy) {
if (x >= 0 && x < width && y >= 0 && y < height) {
int index = (int)x+((int)y*width);
buffer[index]++;
vxbuffer[index] += vx;
vybuffer[index] += vy;
}
}

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

void setup() {

size(600,600,P2D);

colorMode(HSB);

attractor = new Attractor[NUM_ATTRACTORS];
particle = new Particle[NUM_PARTICLES];

scatter();

// cut and pasted a favourite
attractor[0] = new Attractor(398.7855, 260.12);
attractor[1] = new Attractor(430.83215, 433.03412);
attractor[2] = new Attractor(257.5647, 411.41632);
attractor[3] = new Attractor(231.67877, 426.55072);
attractor[4] = new Attractor(127.697815, 317.10776);
attractor[5] = new Attractor(165.1979, 245.52707);
attractor[6] = new Attractor(332.07306, 104.05903);
attractor[7] = new Attractor(465.398, 162.21806);

}

void draw() {
for (int j = 0; j < particle.length; j++) {
particle[j].step();
}
set(frameCount%32,frameCount/32,#d0d0d0);
if (frameCount % 32 == 0) {
background(255);
float maxbuf = 0;
float maxspeed = 0.0;
for (int i = 0; i < buffer.length; i++) {
maxbuf = max(maxbuf,buffer[i]);
maxspeed = max(maxspeed,sqrt(sq(vybuffer[i])+sq(vxbuffer[i])));
}
loadPixels();
for (int i = 0; i < pixels.length; i++) {
if (buffer[i] > 0) {
float value = buffer[i]/maxbuf;
float b = 255.0-(255.0*pow(value,power));
float speed = sqrt(sq(vybuffer[i])+sq(vxbuffer[i]))/maxspeed;
float s = (255.0*pow(speed,power));
float angle = PI+atan2(vybuffer[i],vxbuffer[i]);
float h = 255.0-(angle*255.0/TWO_PI);
pixels[i] = color(h,s,b);
}
}
updatePixels();
}
}

void scatter() {
background(255);
println("attractors:");
for (int i = 0; i < attractor.length; i++) {
attractor[i] = new Attractor();
attractor[i].x = (width*0.25*cos(TWO_PI*(float)i/(float)attractor.length)) + (width/2) + random(-width*0.1,width*0.1);
attractor[i].y = (height*0.25*sin(TWO_PI*(float)i/(float)attractor.length)) + (height/2) + random(-height*0.1,height*0.1);
println("attractor["+i+"] = new Attractor("+attractor[i].x+", "+attractor[i].y+");");
}
println();
for (int i = 0; i < particle.length; i++) {
particle[i] = new Particle();
}
buffer = new float[width*height];
vxbuffer = new float[width*height];
vybuffer = new float[width*height];
}

void keyPressed() {
scatter();
}

void mousePressed() {
power = float(mouseX)/float(width);
}

Page Index Toggle Pages: 1