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 › ira greenberg 'particel wave example problem'
Page Index Toggle Pages: 1
ira greenberg 'particel wave example problem' (Read 480 times)
ira greenberg 'particel wave example problem'
Jul 11th, 2008, 3:58pm
 
Hi

just recently picked up a copy of Ira Greenberg book 'Processing'. Been working through some of the code examples.
Tried to type out Particle Wave - but it doesn't seem to work no animation just a still image. I will keep trying to de-bug it but if there is anybody out there who can help that would be great.
thanks
mp
my copied code is below -
/*
Particlewave*/

void setup (){
 size (600, 400);
 background(0);
 smooth();
 
 /* lots of stuff to manipulate*/
 /* increase strokeWt if you don't see output*/
 float strokeWt = 1.5;
 float strokeCol= 255;
 float[]strokeStyle = { strokeWt, strokeCol };

 //particlae limits
int timeLimit = 1000;
int particleCount = 100;
int[]particleLimits = { timeLimit, particleCount };

// particle dynamics
float amplitudeMin = .5;
float amplitudeMax = 4.0;
float frequencyMin = 4.0;
float frequencyMax = 40.0;
float materialMin = .25; // lead
float materialMax = .99; //rubber
float []particleDynamics = { amplitudeMin, amplitudeMax,
frequencyMin, frequencyMax, materialMin, materialMax};

// speed limits
float accelMin = .005;
float accelMax = 0.2;
float xSpeedMin = -2.0;
float xSpeedMax = 2.0;
float[]speedLimits = { xSpeedMin, xSpeedMax,
accelMin, accelMax };

float gravity = .85;
// position of particle emitter
float emitterX = width/2;
float emitterY = 0;
float[]world = { gravity, emitterX, emitterY };

// start partile engine
setParticles(strokeStyle, particleLimits,
particleDynamics, speedLimits, world);
}

// main particle engine - fully parameterized
void setParticles(float[]strokeStyle, int[]particleLimits,
float[]particleDynamics, float[]speedLimits, float[]world){
// create arrays
float []xSpeed = new float[particleLimits[1]];
float []ySpeed = new float[particleLimits[1]];
float []accel = new float[particleLimits[1]];
float []x = new float[particleLimits[1]];
float []y = new float[particleLimits[1]];
float []amplitude = new float[particleLimits[1]];
float []frequency = new float[particleLimits[1]];
float []material = new float[particleLimits[1]];
// particle style
strokeWeight(strokeStyle[0]);
stroke(strokeStyle[1]);
// angle used as segment fro makeWaves function call
float angle = 0;
// fill arrays
for (int i=0; i<particleLimits[1]; i++){
xSpeed[i] = random(speedLimits[0], speedLimits[1]);
accel[1] = random(speedLimits[2], speedLimits[3]);
amplitude[i] = random(particleDynamics[0], particleDynamics[1]);
frequency[i] = random(particleDynamics[2], particleDynamics[3]);
material[i] = random(particleDynamics[4], particleDynamics[5]);
// emitter initial position
x[i] = world[1];
y[i] = world[2];
}
// dynamics engine
for (int i=0; i<particleLimits[1]; i++){
for (int j=0; j<particleLimits[0]; j++){
x[i]+=xSpeed[i];
// add waviness to call to makeWaves function
x[i]=makeWaves(x[i], angle+=frequency[i], amplitude[i]);
ySpeed[i]+=accel[i];
y[i]+=ySpeed[i];
// plot function just calls point () method
plot (new float[]{
 x[i], y[i] }
 );
 
 // ground detection
 if (y[i]>=height){
   ySpeed[i]*=1*material[i];
   ySpeed[i]*=world[0];
   y[i] = height;
 }
 // wall detection
 if (x[i]>=width || x[i]<=0){
   xSpeed[i] *=-1;
 }
}
}
}
// generate wave
float makeWaves(float x, float angle, float amplitude){
 x+=sin(radians(angle))*amplitude;
 return x;
}
// draw points
void plot(float[]pt){
 point(pt[0], pt[1]);
}



Re: ira greenberg 'particel wave example problem'
Reply #1 - Jul 12th, 2008, 12:01am
 
I don't think this example is animated.
Here's a very quick hack to make it "move"

Please note: I just got back home and fixed code a bit - still not pretty though (sorry).

Code:
 
/*
Particle Wave
Ira Greenberg, December 5, 2005
- hacked animated version 7/11/08
*/

/*lots of stuff to manipulate. I packed
the arguments into arrays just to make the
setParticles function argument/parameter
lists less unwieldy*/
//particle style
float strokeWt = 1.5; // increase if you don’t see any output
float strokeCol= 255;
float[]strokeStyle = {  
 strokeWt, strokeCol};

//particle limits
int timeLimit = 1000;
int particleCount = 100;
int[]particleLimits = {  
 timeLimit, particleCount};

//particle dynamics
float amplitudeMin = .5;
float amplitudeMax = 4.0;
float frequencyMin = 4.0;
float frequencyMax = 40.0;
float materialMin = .25; // lead
float materialMax = .99; // rubber
float[]particleDynamics = {  
 amplitudeMin, amplitudeMax, frequencyMin,
 frequencyMax, materialMin, materialMax   };

// speed limits
float ySpeedMin = .002;
float ySpeedMax = .1;
float xSpeedMin = -2.0;
float xSpeedMax = 2.0;
float[]speedLimits = {  
 xSpeedMin, xSpeedMax, ySpeedMin, ySpeedMax   };

float gravity = .85;
//postion of particle emitter
float emitterX;
float emitterY = 0;
float[]world = new float[3];

float[]xSpeed;
float[]ySpeed;
float[]accel;
float[]x;
float[]y;
float[]amplitude;
float[]frequency;
float[]material;
float angle = 0;

// for increental update
float particleCounter = 0;
float particleFlow = .05;
float waveCounter = 0;
float waveFlow = 1.0;



void setup(){
 size(800, 400);
 background(0);
 frameRate(30);
 smooth();
 emitterX = width/2;
 world[0] = gravity;
 world[1] = emitterX;
 world[2] = emitterY;

 //start particle engine
 setParticles(strokeStyle, particleLimits, particleDynamics, speedLimits, world);
}

// main particle engine-fully parameterized
void setParticles(float[]strokeStyle, int[]particleLimits, float[]particleDynamics,
float[]speedLimits,  float[]world){
 //create arrays
 xSpeed = new float[particleLimits[1]];
 ySpeed = new float[particleLimits[1]];
 accel = new float[particleLimits[1]];
 x = new float[particleLimits[1]];
 y = new float[particleLimits[1]];
 amplitude = new float[particleLimits[1]];
 frequency = new float[particleLimits[1]];
 material = new float[particleLimits[1]];

 // particle style
 strokeWeight(strokeStyle[0]);
 stroke(strokeStyle[1]);


 //fill arrays
 for (int i = 0; i< particleLimits[1]; i++){
   xSpeed[i] = random(speedLimits[0], speedLimits[1]);
   ySpeed[i] = random(speedLimits[2], speedLimits[3]);
   amplitude[i] = random(particleDynamics[0], particleDynamics[1]);
   frequency[i] = random(particleDynamics[2], particleDynamics[3]);
   material[i] = random(particleDynamics[4], particleDynamics[5]);
   //emitter initial position
   x[i] = world[1];
   y[i] = world[2];
 }
}

//generate wave
float makeWaves(float x, float angle, float amplitude){
 x+=sin(radians(angle))*amplitude;
 return x;
}

// draw points
void plot(float[]pt){
 point(pt[0], pt[1]);
}

void draw(){
 fill(0, 20);
 rect(-1, -1, width+1, height+1);
// println(particleCounter);
 // dynamics engine
 for (int i=0; i<waveCounter; i++){
   for (int j= 0; j<particleCounter; j++){
x[i]+=xSpeed[i];
// add waviness to call to makeWaves function
x[i]=makeWaves(x[i], angle+=frequency[i], amplitude[i]);
accel[i]+=ySpeed[i];
y[i]+=accel[i];
// plot function just calls point() method
plot(new float[]{  
  x[i], y[i]          }
);

//ground detection
if (y[i]>=height){
  accel[i]*=-1*material[i];
  accel[i]*=world[0];
  y[i] = height;
}
//wall detection
if (x[i]>=width || x[i]<=0){
  xSpeed[i] *=-1;
}
   }

 }
 if (waveCounter<particleLimits[1]-1){
   waveCounter+=waveFlow;

 }
 if (particleCounter<particleLimits[0]-1){
   particleCounter+=particleFlow;

 }
}
Page Index Toggle Pages: 1