FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Syntax
(Moderators: fry, REAS)
   Particles
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: Particles  (Read 572 times)
kevin

WWW
Particles
« on: Aug 21st, 2003, 5:43pm »

Hey,
 
Many of the work here contains particles, and have a class for each particle, and they react to the mouse or some other input.
 
They're all a bit over my head, since i'm not very familiar with OOP.
 
Would anyone be able to post up a very basic example of a few particles reacting to something to get me on my way?
 
Thanks,
 
- Kevin
 
mKoser

WWW Email
Re: Particles
« Reply #1 on: Aug 21st, 2003, 7:01pm »

...just hacked a quick example, I hope the explenations make sense, otherwise, just ask
 
Code:

// particle object example
// by mikkel crone koser
// www.beyondthree.com
// aug. 2003
 
// creating variables of type 'particle' (the name of
// your object-class) they will each hold and object
// of type particle. You can create as many of these
// objects as you want, or create an array of the objects
// to simplify your code.
particle pOne, pTwo;
 
void setup(){
  size(200, 200);
  background(#444444);
  ellipseMode(CENTER_RADIUS);
  noStroke();
  
  // create objects
  // since the constrcutor class expects two
  // integers to be passed in when creating an
  // object, you have to create it like this:
  // objectVariableName = new objectClassName(integer, integer)
  pOne = new particle((int)random(width), (int)random(height));
  pTwo = new particle((int)random(width), (int)random(height));
}
 
void loop(){
  // by calling the 'move' method in each
  // object, their position is recalculated
  // and the objects are drawn on the canvas
  pOne.move();
  pTwo.move();
}
 
class particle{
  // these are object variables, each
  // object will have it's own set of
  // these variables, you can read and
  // change these varibales from outside
  // the object by calling:
  // objectName.varriableName = newValue;
  int x;
  int y;
  float speedX = random(1,5);
  float speedY = random(1,5);
 
  // this is the constructor method,
  // it will only be called when you
  // create an object.
  particle(int xx, int yy){
    x = xx;
    y = yy;
  }
  
  // this is a custom medthos, it can
  // be called repeatedly as often as
  // you wish. You can call it like this:
  // objectName.methodName();
  void move(){  
    // constraining X movement to boundries of screen
    if(x > width){
      speedX = -speedX;
    }else if(x < 0){
       speedX = -speedX;
    }
    
    // constrain Y movement to boundries of screen
    if(y > height){
      speedY = -speedY;
    }else if(y < 0){
      speedY = -speedY;
    }
    
    // assigning X snd Y speed to current X and Y position
    x = (int)(x + speedX);
    y = (int)(y + speedY);
    
   // drawing the object
    ellipse(x, y, 5, 5);
  }
}

 
...you can also look at barragan's example that comes installed with Processing goto Open > Example > Structure > Objects.
 
Good luck, and please post the results here
Mikkel
 
UPDATE:
whoops, i just realized that i complete misunderstood you question, you are looking for PARTICLES, not OBJECTS ... damn, I read it too fast, and jumped straigt into writing the code! *shit* ...anyway, I guess objects are halfway towards creating particles, whats missing is not the behavior of the objects! Duh!
« Last Edit: Aug 21st, 2003, 7:09pm by mKoser »  

mikkel crone koser | www.beyondthree.com | http://processing.beyondthree.com
benelek

35160983516098 WWW Email
Re: Particles
« Reply #2 on: Aug 23rd, 2003, 4:29pm »

the key to OOP (object-oriented-particles ) is quantasizing any forces, so they have a cumulative effect on each turn of the loop().
 
if you want a solid but long road to follow, have a look through most high school math text books - especially the interesting stuff in upper-high school that verges on physics. it's easy to come up with formulas for certain forces, if you know the algebraic descriptions.
 
putting particle systems into an object-oriented structure really creates benefits when you want to use things like acceleration on each particle, because that requires the storage of position and velocity values for each axis on each particle. so most of the operations can be self-contained within the particle, making it easier to keep track of things.
 
here's an extract from one of my first particle-based sketches, domain_rules. it describes a magnet class (centres of forces), a particle class (which react to the magnets), and a domain class (which makes one talk to the other). in general, there're two main methods defined for each particle: update the particle's position, and draw it. sometimes people hard-code in the position/role of influences (magnets) on the particles, but i prefer to put em in objects themselves (for added flexibility and multiplicity).
 
if you have any questions, lemme know. if the code breaks, it's because it was written for an earlier version of Processing. it's also not very efficient in terms of process-power. plus, you might want to copy and paste it into Processing, to take advantage of color-coding.
 
Code:

// domain rules
// by benelek.
 
dom[] domains = new dom[1];
particle[] particles = new particle[10000];
 
void setup() {
  size(700,400);
  background(255);
  ellipseMode(CENTER_DIAMETER);
   
  domains[0]=new dom();
   
  int i=0;
  for(int ix=0; ix<100; ix++) {
    for(int iy=0; iy<10; iy++) {
 for(int iz=0; iz<10; iz++) {
   particles[i]=new particle(ix*10-50,iy*10-50,iz*10-50); //give position on a regular grid.
   particles[i].particleCol = color((int)(ix*2.5f),25*iy,25*iz); //color particle based on initial position.
   i++;
 }
    }
  }
}
 
void loop() {
  translate(width/2,height/2,0);
  domains[0].draw(); //draw magnets
  for(int f=0; f<particles.length; f++) {
    particles[f].update(); //do quantasized ooperation on each particle. since they don't interact with each-other, the order makes no difference.
    particles[f].draw(); //draw the particles.
  }
}
 
 
//----------------------universal domain class-----------------------
class dom {
  magnet[] influences = new magnet[2];
  dom () {
    influences[0]=new magnet(-1,-10,-10,-10);
    influences[1]=new magnet(-1,10,10,10);
  }
  void draw() {  //for drawing the influences.
    for(int i=0; i<influences.length; i++) {
 influences[i].draw();
    }
  }
}
 
//---magnet class---point-based attractors or repelors.
class magnet {
  float xPos=0; // coordinates of magnet.
  float yPos=0;
  float zPos=0;
  float x; // distances to particle, not coordinates.
  float y;
  float z;
  float xnynz;
  int constant = 1;
  // constant:
  //  1 = repel.
  //  -1 = attract.
   
  magnet (int Iconst, int x, int y, int z) {
    constant=Iconst;
    xPos=x;
    yPos=y;
    zPos=z;
  }
  magnet() {
  }
  float accelX() {
    //NOTE: run updateXY() first.
    return(constant*x/xnynz);
  }
  float accelY() {
    //NOTE: run updateXY() first.
    return(constant*y/xnynz);
  }
  float accelZ() {
    //NOTE: run updateXY() first.
    return(constant*z/xnynz);
  }
  void updateXY(float objX, float objY, float objZ) {
   //update x and y values for use in accel calc.
   x=objX-this.xPos;
   y=objY-this.yPos;
   z=objZ-this.zPos;
   xnynz=sq(x)+sq(y)+sq(z);
   if(xnynz==0) { xnynz=0.00001f; }
  }
   
  void draw() {
    fill(0);
    push();
 translate(0,0,zPos);
 ellipse(xPos,yPos,10,10);
    pop();
  }
}
 
//--------------------universal particle class---------------------
 
class particle {
  // NOTE: particles are given their own velocities, for the purpose of
  // individual innertia.
  // pos[] and vel[] indexes:
  //  0 is x.
  //  1 is y.
  //  2 is z.
  float[] pos = new float[3];
  float[] vel = { 0,0,0 };
  dom domain = domains[0];
  int particleCol=0;
   
  particle(float ix, float iy, float iz) {
    pos[0]=ix;
    pos[1]=iy;
    pos[2]=iz;
  }
  void update() {
    for(int i=0; i<domain.influences.length; i++) {
 domain.influences[i].updateXY(pos[0],pos[1],pos[2]);
 vel[0] += domain.influences[i].accelX(); //accelerate.
 vel[1] += domain.influences[i].accelY(); //accelerate.
 vel[2] += domain.influences[i].accelZ(); //accelerate.
    }
    pos[0] += vel[0];
    pos[1] += vel[1];
    pos[2] += vel[2];
  }
  void draw() {
 translate(0,0,pos[2]);
 stroke(particleCol);
 point(pos[0],pos[1]);
 translate(0,0,-pos[2]);
  }
}

 
kevin

WWW
Re: Particles
« Reply #3 on: Aug 25th, 2003, 2:44pm »

Thanks, that helps me on my way,
 
From my previous work with Flash I have some notion of how it works, but this has put me in the right direction.
 
benelek, thankfully I studied a little bit of Linear Algebra in college, so that should help me, thought it is a bit rusty.  
 
Mikkel, what you posted was very helpful too, asking objects was really what I meant, but forgot my OO terms.
 
thank you both, i'm off to play now....
 
kevin

WWW
Re: Particles - nesting?
« Reply #4 on: Aug 27th, 2003, 11:24am »

Hi again, still playing with this stuff. Got two followup question that maybe you can help me with.
 
How can I create a 'chain' of objects.
 
I see benelek's example where he created an array of new objects, but I can't get a basic example to work for some reason.
 
Also, is it possible to nest objects recursively? Thanks.
 
benelek

35160983516098 WWW Email
Re: Particles - nesting?
« Reply #5 on: Aug 28th, 2003, 3:12am »

my example didn't run it should... feel free to work with my code and modify whatever you want to.
 
on Aug 27th, 2003, 11:24am, p wrote:
How can I create a 'chain' of objects.

 
i'm assuming you mean a group of particles that follow each-other, right what kind of relationship do you want to create that would link the objects into a chain
 
if it's just a group of one particle following the position of another exactly, you don't need objects - you can simply have an array of positions, with the positions being bumped up the array on each cycle of loop(). you'd decide where the new values come from (eg, mouseX and mouseY). then you'd draw a particle at every 10th array value or so. if you do this, though, you'd probably want to slow the frameRate down a bit, so that the values don't cycle too quickly.
 
or you could play a game of 'tag', where you define the 'update' method of each particle (object) as being attracted to another particle (object). you wouldn't need a domain class as in my example, and you can copy the update functions out of that class to be used by the individual particles... when you initialise the array of particles, you would specify that each particle look at the position values of the particle previous in the array, with the first object on the array being pointed to whatever you want the line to follow.
 
 
if you want me to make the code up for you, lemme know - but it'll have to wait a few days
 
kevin

WWW
Re: Particles
« Reply #6 on: Aug 28th, 2003, 4:19pm »

Hi,
 
Thanks again for the reply. You're code worked fine actually (and looked cool too!)
 
When I meant a chain I simply meant I wanted to create an array of objects, so i could reference them just as you did.
 
I took a look at it this afternoon though, and it's working. I must have just missed something yesterday.
 
Here's the code for anyone needing it in future.
 
Code:

// Small example of an array of objects
 
myObject[] myObjectArray = new myObject[2];
 
void setup() {
   
  myObjectArray[0] = new myObject();
  myObjectArray[1] = new myObject();
   
  size(300,300);  
  background(230);
  fill(255,234,78);
}
 
void draw() {
  myObjectArray[0].drawObj(5,6);
  myObjectArray[1].drawObj(55,55);
}
 
// Small Example Class
class myObject {
 
  // Draw Object on screen method
  void drawObj(int x, int y){
    ellipse(x,y,10,10);
  }
}

 
 
Pages: 1 

« Previous topic | Next topic »