class parameters

edited July 2016 in Kinect

hello i have taken the standar particle system with the texture,and i want to give the location of the mouse in draw function rather inside the class because i want to connect it with kinect,tried it but the result was strange,i tried to move the acceleration and the velocity,the result is better but it is still strange.i tried to move also the the lifespan parameter but it is also strange. Im thinking that is something with the add particle function and the iterator,but im not very good with tha concept,to figure out the problem.I will be very thankful if you can help me.Thank you in advance

here is the code:

import java.util.Iterator;
ParticleSystem ps;
PVector location;
PVector acceleration;
PVector velocity;
float lifesp = 255;
float _lifesp = 2.0;

void setup(){
 size(640,360,P2D);
PImage image = loadImage("texture.png");
ps = new ParticleSystem(image);


}

void draw(){
 background(0);
 //ps.applyForce();
 acceleration = new PVector(0, 0.05);
    velocity = new PVector(random(-1, 1), random(-2, 0));
 PVector location = new PVector (mouseX,mouseY);
 lifesp -= _lifesp;
ps.run(location, acceleration, velocity,lifesp );
ps.addParticle();


}

the particle class:

class Particle {


  PVector loc;
  PVector location;

  float lifespan;
PImage image;




  Particle(PImage img) {



    image = img;
  }

  void run(PVector location ,PVector _acceleration, PVector _velocity,float _lifespan) {

    update(location,_acceleration,_velocity);
    display(location,_lifespan);
  }

  void update(PVector _location, PVector _acceleration, PVector _velocity) {

    velocity.add(_acceleration);
    _location.add(_velocity); 

  }


  void display(PVector _location, float _lifespan) {

    stroke(0, lifespan);
    imageMode(CENTER);
    tint(255,lifespan);
    image(image,_location.x,_location.y);
    //fill(175, lifespan);
    //ellipse(location.x, location.y, 8, 8);
  }

  boolean isDead(float _lifespan) {
    lifespan = _lifespan;
    if (_lifespan < 0.0) { 
      return true;
    } else {
      return false;
    }
  }
}

and the particle system class:

class ParticleSystem{

 ArrayList<Particle> particles;
PVector origin;

//PVector _loc;

PImage image;

ParticleSystem(PImage _img){

  particles = new ArrayList<Particle>();
  image = _img;

} 

  void addParticle(){
   particles.add(new Particle(image)); 


  }

  void run(PVector location, PVector acceleration, PVector velocity,float _lifespan){
   Iterator<Particle> it = particles.iterator();
  while (it.hasNext()){

   Particle p = it.next();
   p.run(location,acceleration,velocity, _lifespan);
   if(p.isDead(_lifespan)){
     println("khgkhk");
    it.remove(); 
   }
  } 

  }


}`

Answers

  • Correct me if I'm wrong, but I understand that you want to:

    1. Receive mouse-position from kinekt
    2. Display the particle-systen at that location

    But you are having trouble because the information you receive from the kinekt is not usable in a class.

    Is this correct? If so, wouldn't it be easier to pass the received information on to another var and then accessing that from within the class? Instead of rewriting the whole sketch? And it's usually a good idea to keep all the variables the class needs inside the class.

    Is it possible to see the original sketch? That way it's easier to see what you've done and perhaps where something went wrong?

  • i need to take the location of the particles from draw function,because there i can call the kinect calibration,now i m working with mouseX mouseY but if i find this i could make the connection,except if i import the library in the class and instantiate the kinect object and call all the functions inside the class,i tried it but it seemed more complicated

  • here is the original

    import java.util.Iterator;
    ParticleSystem ps;
    
    
    void setup(){
     size(640,360,P2D);
    PImage image = loadImage("texture.png");
    ps = new ParticleSystem(image);
    
    
    }
    
    void draw(){
     background(0);
     //ps.applyForce();
    ps.run();
    ps.addParticle();
    
    
    }
    

    the particle class:

    class Particle {
    
      PVector location;
      PVector velocity;
      PVector acceleration;
      float lifespan;
    PImage image;
    
      Particle(PImage img) {
        location = new PVector(mouseX,mouseY);
        acceleration = new PVector(0, 0.05);
        velocity = new PVector(random(-1, 1), random(-2, 0)); 
        lifespan = 255;
        image = img;
      }
    
      void run() {
        update();
        display();
      }
    
      void update() {
        velocity.add(acceleration);
        location.add(velocity); 
        lifespan -= 2.0;
      }
    
    
      void display() {
        stroke(0, lifespan);
        imageMode(CENTER);
        tint(255,lifespan);
        image(image,location.x,location.y);
        //fill(175, lifespan);
        //ellipse(location.x, location.y, 8, 8);
      }
    
      boolean isDead() {
        if (lifespan < 0.0) { 
          return true;
        } else {
          return false;
        }
      }
    }
    

    the particle system class:

        class ParticleSystem{
     ArrayList<Particle> particles;
    PVector origin;
    
    PImage image;
    
    ParticleSystem(PImage _img){
    
      particles = new ArrayList<Particle>();
    image = _img;
    
    } 
    
      void addParticle(){
       particles.add(new Particle(image)); 
    
      }
    
      void run(){
       Iterator<Particle> it = particles.iterator();
      while (it.hasNext()){
       Particle p = it.next();
       p.run();
       if(p.isDead()){
         println("khgkhk");
        it.remove(); 
       }
      } 
    
      }
    
    
    }
    
  • Ok, so mouseX replaces the x-coordinate you receive from the kinekt. And let's say you can't access that information from within the class, or that it's too complicated. What you can do is to make a new variable called xMouse. We declare it before setup and in draw we say xMouse = mouseX;

    The information received is now stored in a global variable that is no different from any other variable. And we do the same with mouseY

    In the class we instantiate the location PVector by accessing the global variable xMouse like this: location = new PVector(xMouse, yMouse);

  • the global variable that i declare before the setup can be accessed from everywhere?also inside the class panel?is any difference if the classes are in seperate panels or in the same?the vector of the kinect is declared inside an if statement. the vector of the kinect from the right hand is the location of the ellipse

    import SimpleOpenNI.*;
    SimpleOpenNI kinect;
    
    
    void setup() {
      size(640, 480);
    
      kinect = new SimpleOpenNI(this);
      kinect.enableDepth();
      ///_SKEl_doesnt work
      kinect.enableUser();
    
      size(640, 480);
      fill(255, 0, 0);
    }
    
    void draw() {
      kinect.update();
      PImage depth = kinect.depthImage();
    
      image(depth, 0, 0);
    
    
      IntVector userList = new IntVector();
    
      kinect.getUsers(userList);
    
      if (userList.size() > 0) {
    
        int userId = userList.get(0);
    
        if ( kinect.isTrackingSkeleton(userId)) {
    
          PVector rightHand = new PVector();
          kinect.getJointPositionSkeleton(userId, SimpleOpenNI.SKEL_LEFT_HAND, rightHand);
    
          PVector convertedRightHand = new PVector();
          kinect.convertRealWorldToProjective(rightHand, convertedRightHand);
    
    ellipse(convertedRightHand.x, convertedRightHand.y, 10, 10);
    
    
        }
      }
    }
    
    
    
    
    void onNewUser(SimpleOpenNI kinect, int userID) {
      println("Start skeleton tracking");
      kinect.startTrackingSkeleton(userID);
    }
    
    //startTracking is alterd 
    void onEndCalibration(int userId, boolean successful) {
      if (successful) {
        println(" User calibrated !!!");
        kinect.startTrackingSkeleton(userId);
      } else {
        println("  Failed to calibrate user !!!");
        kinect.startTrackingSkeleton(userId);
      }
    }
    
    //There is aproblem here,deleted the pose
    void onStartPose( int userId) {
      println("Started pose for user");
      kinect.stopTrackingSkeleton(userId);
    }
    
  • A global variable is available from anywhere. No matter you are calling it from a class or a function or somewhere else.

    When you declare a variable inside a class it is private. It is only usable by the class unless you specify that it's global. And if you declare a variable in a function, it's only available in that function. That's why cars usually are declared before setup().

    I have never worked with kinekt, but from what I can understand, you problem will be solved if you do this:

    1. Before setup: PVector convertedRightHand
    2. In draw(line37): convertedRightHand = new PVector();
    3. In the class that displays the particle system: location.x = convertedRightHand.x; location.y = convertedRightHand.y;

    But I might be wrong. If this doesn't work @Chrisir might be of better help, or maybe @GoToLoop has some neat solution.

  • ok thank you i will try it

  • it gives null pointer exception.what about .get()?

  • and i put print("location "+location) and gives location null.the get() fuction doesnt work

  • try

    PVector location = new PVector(); 
    
    location.x = convertedRightHand.x; 
    location.y = convertedRightHand.y;
    
  • edited July 2016

    If both location & convertedRightHand are PVector objects, in order to assign the latter to the former, it's easier to just call method set() rather than individually assign their fields to each other:
    location.set(convertedRightHand);

    https://Processing.org/reference/PVector_set_.html

  • I am not sure what is his Problem anyway

  • my problem is that i want the declared convertedRightHand Pvector inside the if statement in draw to be called in the location PVector of the particle class,so to make the kinect move the particles

  • And does what I posted work?

  • I dint try it yet,i m doing an other job now,but should i declare it inside particle class?The processing sketch works until start tracking the skeleton,but then prints location null and then null pointer exception.I will try both tours and GoToLoop solutions

  • edited July 2016

    Neither worked.here is the sketch with the particle system inside kinect:

    import java.util.Iterator;
    ParticleSystem ps;
    
    import SimpleOpenNI.*;
    SimpleOpenNI kinect;
    
    PVector convertedRightHand;
    PVector location;
    
    void setup() {
      size(640, 480, P3D);
      PImage image = loadImage("texture.png");
      ps = new ParticleSystem(image);
    
      kinect = new SimpleOpenNI(this);
      kinect.enableDepth();
      ///_SKEl_doesnt work
      kinect.enableUser();
    }
    
    void draw() {
      background(0);
      //ps.applyForce();
    
    
      kinect.update();
      //PImage depth = kinect.depthImage();
    
      //image(depth, 0, 0);
    
      IntVector userList = new IntVector();
    
      kinect.getUsers(userList);
    
      if (userList.size() > 0) {
    
        int userId = userList.get(0);
    
        if ( kinect.isTrackingSkeleton(userId)) {
    
          PVector rightHand = new PVector();
          kinect.getJointPositionSkeleton(userId, SimpleOpenNI.SKEL_LEFT_HAND, rightHand);
    
          PVector convertedRightHand = new PVector();
    
          //I have to pass this PVector to the particle class
    
          kinect.convertRealWorldToProjective(rightHand, convertedRightHand);
          //location.set(convertedRightHand);
          //ellipse(convertedRightHand.x, convertedRightHand.y, 10, 10);
          ps.run();
          ps.addParticle();
        }
      }
    }
    
    void onNewUser(SimpleOpenNI kinect, int userID) {
      println("Start skeleton tracking");
      kinect.startTrackingSkeleton(userID);
    }
    
    //startTracking is alterd 
    void onEndCalibration(int userId, boolean successful) {
      if (successful) {
        println(" User calibrated !!!");
        kinect.startTrackingSkeleton(userId);
      } else {
        println("  Failed to calibrate user !!!");
        kinect.startTrackingSkeleton(userId);
      }
    }
    
    //There is aproblem here,deleted the pose
    void onStartPose( int userId) {
      println("Started pose for user");
      kinect.stopTrackingSkeleton(userId);
    }
    
    
    // the particle class:
    
    class Particle {
    
      PVector velocity;
      PVector acceleration;
      float lifespan;
      PImage image;
    
      Particle(PImage img) {
    
        PVector location = new PVector();
    
    
     //here is the place i have to pass it
    
        location.set(convertedRightHand);
        acceleration = new PVector(0, 0.05);
        velocity = new PVector(random(-1, 1), random(-2, 0)); 
        lifespan = 255;
        image = img;
        print("location "+location);
      }
    
      void run() {
        update();
        display();
      }
    
      void update() {
        velocity.add(acceleration);
        location.add(velocity); 
        lifespan -= 2.0;
      }
    
      void display() {
        stroke(0, lifespan);
        imageMode(CENTER);
        tint(255,lifespan);
        image(image,location.x,location.y);
        //fill(175, lifespan);
        //ellipse(location.x, location.y, 8, 8);
      }
    
      boolean isDead() {
        if (lifespan < 0.0) { 
          return true;
        } else {
          return false;
        }
      }
    }
    
    // the particle system:
    
    class ParticleSystem{
      ArrayList<Particle> particles;
      PVector origin;
    
      PImage image;
    
      ParticleSystem(PImage _img){
    
        particles = new ArrayList<Particle>();
        image = _img;
      } 
    
      void addParticle(){
        particles.add(new Particle(image)); 
      }
    
      void run(){
        Iterator<Particle> it = particles.iterator();
        while (it.hasNext()){
          Particle p = it.next();
          p.run();
          if(p.isDead()){
            println("khgkhk");
            it.remove(); 
          }
        } 
      }
    }
    

    is it possible?If i pass it as an argument to particle system class and then to particle class?

  • except maybe i pass it in ps.run() function as argument

  • yes.

    these are the original lines in draw()

     PVector location = new PVector (mouseX,mouseY);
    
     ps.run(location, acceleration, velocity,lifesp );
    

    now just modify them like

    PVector location = new PVector(); 
    
    location.x = convertedRightHand.x; 
    location.y = convertedRightHand.y;
    
    
         ps.run(location, acceleration, velocity,lifesp );
    
  • i have to pass all the arguments?

  • I tried it and it gives strange results

    here i disable the kinect PVector and i put mouseX,mouseY

    import java.util.Iterator;
    ParticleSystem ps;
    
    import SimpleOpenNI.*;
    SimpleOpenNI kinect;
    
    //PVector convertedRightHand;
    //PVector location;
    
    
    
    
    void setup() {
      size(640, 480, P3D);
      PImage image = loadImage("texture.png");
      ps = new ParticleSystem(image);
    
      kinect = new SimpleOpenNI(this);
      kinect.enableDepth();
      ///_SKEl_doesnt work
      kinect.enableUser();
    }
    
    void draw() {
      background(0);
      //ps.applyForce();
    
    
      kinect.update();
      //PImage depth = kinect.depthImage();
    
      //image(depth, 0, 0);
    
    
      //IntVector userList = new IntVector();
    
     // kinect.getUsers(userList);
    
      //if (userList.size() > 0) {
    
        //int userId = userList.get(0);
    
        //if ( kinect.isTrackingSkeleton(userId)) {
    
          //PVector rightHand = new PVector();
          //kinect.getJointPositionSkeleton(userId, SimpleOpenNI.SKEL_LEFT_HAND, rightHand);
    
          //PVector convertedRightHand = new PVector();
          //kinect.convertRealWorldToProjective(rightHand, convertedRightHand);
           PVector location = new PVector();
           //convertedRightHand = location;
           location = new PVector(mouseX,mouseY);
    
           PVector acceleration = new PVector(0, 0.05);
    
           PVector velocity = new PVector(random(-1, 1), random(-2, 0)); 
           float lifespan = 255;
    
          //ellipse(convertedRightHand.x, convertedRightHand.y, 10, 10);
          ps.run(location,acceleration,velocity,lifespan);
          ps.addParticle();
        //}
      //}
    }
    
    void onNewUser(SimpleOpenNI kinect, int userID) {
      println("Start skeleton tracking");
      kinect.startTrackingSkeleton(userID);
    }
    
    //startTracking is alterd 
    void onEndCalibration(int userId, boolean successful) {
      if (successful) {
        println(" User calibrated !!!");
        kinect.startTrackingSkeleton(userId);
      } else {
        println("  Failed to calibrate user !!!");
        kinect.startTrackingSkeleton(userId);
      }
    }
    
    //There is aproblem here,deleted the pose
    void onStartPose( int userId) {
      println("Started pose for user");
      kinect.stopTrackingSkeleton(userId);
    }
    

    The particle system:

    class ParticleSystem{
     ArrayList<Particle> particles;
    PVector origin;
    
    PImage image;
    
    
    ParticleSystem(PImage _img){
    
      particles = new ArrayList<Particle>();
    image = _img;
    
    
    } 
    
      void addParticle(){
       particles.add(new Particle(image)); 
    
      }
    
      void run(PVector _location, PVector _acceleration, PVector _velocity, float _lifespan){
       Iterator<Particle> it = particles.iterator();
      while (it.hasNext()){
       Particle p = it.next();
       p.run(_location, _acceleration, _velocity,_lifespan);
       if(p.isDead(_lifespan)){
         println("khgkhk");
        it.remove(); 
       }
      } 
    
      }
    
    
    }
    

    The particle class:

    class Particle {

      //float lifespan;
      PImage image;
    
      Particle(PImage img) {
    
        //lifespan = _lifespan;
    
        image = img;
        //print("location "+location);
      }
    
      void run(PVector loc,PVector acc,PVector vel, float lifesp) {
        update(loc,acc,vel,lifesp);
        display(loc,lifesp);
      }
    
      void update(PVector _loc,PVector _acc,PVector _vel,float _lifesp) {
        _vel.add(_acc);
        _loc.add(_vel); 
        _lifesp -= 2.0;
      }
    
    
      void display(PVector _locat,float _lifespa) {
        stroke(0, _lifespa);
        imageMode(CENTER);
        tint(255,_lifespa);
        image(image,_locat.x,_locat.y);
        //fill(175, lifespan);
        //ellipse(location.x, location.y, 8, 8);
      }
    
      boolean isDead(float lifespa) {
        if (lifespa < 0.0) { 
          return true;
        } else {
          return false;
        }
      }
    }
    

    I have processing 2.21, i dont know where is the problem

  • this version works and is totally without the kinect

    import java.util.Iterator;
    ParticleSystem ps;
    
    void setup() {
      size(640, 480, P3D);
    
      PImage image = loadImage("texture.png");
      image.resize(5, 5); 
      ps = new ParticleSystem(image);
    }
    
    void draw() {
      background(0);
    
      PVector location = new PVector();
      location = new PVector(mouseX, mouseY);
    
      PVector acceleration = new PVector(0, 0.05);
    
      PVector velocity = new PVector(random(-1, 1), random(-2, 0)); 
      float lifespan = 122;
    
      ps.run(location, acceleration, velocity, lifespan);
      ps.addParticle();
    }
    
    // The particle system:
    
    class ParticleSystem {
      ArrayList<Particle> particles;
      PVector origin;
    
      PImage image;
    
      ParticleSystem(PImage _img) {
        particles = new ArrayList<Particle>();
        image = _img;
      } 
    
      void addParticle() {
        particles.add(new Particle(image));
      }
    
      void run(PVector _location, PVector _acceleration, PVector _velocity, float _lifespan) {
        Iterator<Particle> it = particles.iterator();
        while (it.hasNext()) {
          Particle p = it.next();
          p.run(_location, _acceleration, _velocity, _lifespan);
          if (p.isDead(_lifespan)) {
            it.remove();
          }
        }
      }
    }
    
    //The particle class:
    
    class Particle {
    
      PImage image;
    
      Particle(PImage img) {
        image = img;
      }
    
      void run(PVector loc, PVector acc, PVector vel, float lifesp) {
        update(loc, acc, vel, lifesp);
        display(loc, lifesp);
      }
    
      void update(PVector _loc, PVector _acc, PVector _vel, float _lifesp) {
        _vel.add(_acc);
        _loc.add(_vel); 
        _lifesp -= 2.0;
      }
    
      void display(PVector _locat, float _lifespa) {
        stroke(0, _lifespa);
        imageMode(CENTER);
        tint(255, _lifespa);
        image(image, _locat.x, _locat.y);
      }
    
      boolean isDead(float lifespa) {
        if (lifespa < 0.0) { 
          return true;
        } else {
          return false;
        }
      }
    }
    
  • edited July 2016

    Java language introduced the "enhanced" for ( : ) in its version 5 at year 2004, 12 years ago! @-)
    Still folks rely on Iterator, a more complicated technique for traversing containers! :-O
    It's true Iterator allows us to remove() within a loop. That's a legit use.
    But for List containers, the same can be accomplished w/ a backwards for ( ; ; ) loop too. *-:)
    Another advantage at avoiding Iterator is keeping compatibility w/ Pjs for web deployment. \m/

  • i dont still unsterstand much of the iterator,Chrisir do you know what i have done wrong?

  • it is doing the same effect chrisir.This has complete different effect.The original particle system is this:

    import java.util.Iterator;
    ParticleSystem ps;
    
    
    void setup(){
     size(640,360,P2D);
    PImage image = loadImage("texture.png");
    ps = new ParticleSystem(image);
    
    
    }
    
    void draw(){
     background(0);
     //ps.applyForce();
    ps.run();
    ps.addParticle();
    
    
    }
    

    //The Particle class

    class Particle {
    
      PVector location;
      PVector velocity;
      PVector acceleration;
      float lifespan;
    PImage image;
    
      Particle(PImage img) {
        location = new PVector(mouseX,mouseY);
        acceleration = new PVector(0, 0.05);
        velocity = new PVector(random(-1, 1), random(-2, 0)); 
        lifespan = 255;
        image = img;
      }
    
      void run() {
        update();
        display();
      }
    
      void update() {
        velocity.add(acceleration);
        location.add(velocity); 
        lifespan -= 2.0;
      }
    
    
      void display() {
        stroke(0, lifespan);
        imageMode(CENTER);
        tint(255,lifespan);
        image(image,location.x,location.y);
        //fill(175, lifespan);
        //ellipse(location.x, location.y, 8, 8);
      }
    
      boolean isDead() {
        if (lifespan < 0.0) { 
          return true;
        } else {
          return false;
        }
      }
    }
    

    //the particle system:

    class ParticleSystem{
     ArrayList<Particle> particles;
    PVector origin;
    
    PImage image;
    
    ParticleSystem(PImage _img){
    
      particles = new ArrayList<Particle>();
    image = _img;
    
    } 
    
      void addParticle(){
       particles.add(new Particle(image)); 
    
      }
    
      void run(){
       Iterator<Particle> it = particles.iterator();
      while (it.hasNext()){
       Particle p = it.next();
       p.run();
       if(p.isDead()){
         println("khgkhk");
        it.remove(); 
       }
      } 
    
      }
    
    
    }
    

    sorry for disturbance,im trying to find how to solve it,because it will give my a lot of advance in the latest sketch with kinect

  • and GoToLoop can you be more specific?

  • edited July 2016

    I believe I was clear I was talking about Iterator usage only? ^#(^

  • do you think that the iterator makes the problem,should i use an array and for( , ,) loop?

  • No problems. It's just some relic from the past. ~O)

  • ok,thank you,but can you see any problem in the original code and the code that a give the parameters inside draw.The original particle system is taken from Nature of code,maybe there is also in the examples of processing,or here

    because i dont know what else to do

  • and this is the effect that now does:

  • what should it do?

  • do you know what is the problem?

  • if you see the first video the effect is different

  • the first video is like particles the second is like lines

  • Answer ✓

    i took out the image stuff (because i didn't have one) and what was left looks fine

    import java.util.Iterator;
    ParticleSystem ps;
    
    void setup() {
      size(640, 360, P2D);
      //PImage image = loadImage("texture.png");
      ps = new ParticleSystem();
    }
    
    void draw() {
      background(0);
      //ps.applyForce();
      ps.run();
      ps.addParticle();
    }
    
    class Particle {
    
      PVector location;
      PVector velocity;
      PVector acceleration;
      float lifespan;
    
      Particle() {
        location = new PVector(mouseX, mouseY);
        acceleration = new PVector(0, 0.05);
        velocity = new PVector(random(-1, 1), random(-2, 0)); 
        lifespan = 255;
      }
    
      void run() {
        update();
        display();
      }
    
      void update() {
        velocity.add(acceleration);
        location.add(velocity); 
        lifespan -= 2.0;
      }
    
      void display() {
        stroke(0, lifespan);
        imageMode(CENTER);
        tint(255, lifespan);
        ellipse(location.x, location.y, 5, 5);
        //fill(175, lifespan);
        //ellipse(location.x, location.y, 8, 8);
      }
    
      boolean isDead() {
        if (lifespan < 0.0) { 
          return true;
        } else {
          return false;
        }
      }
    }
    
    class ParticleSystem {
      ArrayList<Particle> particles;
      PVector origin;
    
      ParticleSystem() {
        particles = new ArrayList<Particle>();
      } 
    
      void addParticle() {
        particles.add(new Particle());
      }
    
      void run() {
        Iterator<Particle> it = particles.iterator();
        while (it.hasNext()) {
          Particle p = it.next();
          p.run();
          if (p.isDead()) {
            println("khgkhk");
            it.remove();
          }
        }
      }
    }
    

    if you NEED the image, and if it's the same for every particle, then just make it a global variable - much less fuss.

  • the concept is to instatiate the parameteres in draw function not in particle class ,so to connect it with kinect.i know that it works.i doesnt work good,or better with different effect if i take the parameters in draw function :P

  • that is my problerm,when i give the parameters to draw rather than declare them in particle class the effect is completely different

  • edited July 2016 Answer ✓

    Re-tweaked the sketch again in order to use backwards loop in place of Iterator.
    Now it can be deployed only via Pjs library (JS Mode). Check it out: :-bd
    http://studio.ProcessingTogether.com/sp/pad/export/ro.9tixqv9DytSpX

    /**
     * Colorful Circle Particles (v1.1)
     * GoToLoop (2016-Jul-11)
     *
     * forum.Processing.org/two/discussion/17453/class-parameters#Item_38
     * studio.ProcessingTogether.com/sp/pad/export/ro.9tixqv9DytSpX
     */
    
    import java.util.List;
    
    static final boolean ONLINE = 1/2 == 1/2.;
    static final PVector ACC = new PVector(0, .05);
    
    final ParticleSystem ps = new ParticleSystem();
    
    void setup() {
      size(800, 480, ONLINE? P2D : FX2D);
      smooth(3);
      frameRate(60);
      noStroke();
      ellipseMode(CENTER);
      mouseX = mouseY = width>>1;
    }
    
    void draw() {
      background(0);
      ps.action().addParticle();
    }
    
    class ParticleSystem {
      final List<Particle> particles = new ArrayList<Particle>();
    
      ParticleSystem addParticle() {
        particles.add(new Particle());
        return this;
      }
    
      ParticleSystem action() {
        for (int len = particles.size(), i = len; i-- != 0; )
          if (particles.get(i).action()) {
            particles.set(i, particles.get(--len));
            particles.remove(len);
          }
        return this;
      }
    }
    
    class Particle {
      static final int HEALTH = 0400, LIFELOSS = 2, DIAM = 5;
      int lifespan = HEALTH;
    
      final color c = (color) random(#000000);
    
      final PVector loc = new PVector(mouseX, mouseY);
      final PVector vel = new PVector(random(-1, 1), random(-2));
    
      boolean action() {
        return update().display().isDead();
      }
    
      Particle update() {
        vel.add(ACC);
        loc.add(vel); 
        lifespan -= LIFELOSS;
        return this;
      }
    
      Particle display() {
        fill(c, lifespan);
        ellipse(loc.x, loc.y, DIAM, DIAM);
        return this;
      }
    
      boolean isDead() {
        return lifespan <= 0;
      }
    }
    
  • phaidon, essentially, you want to join two sketches:

    • a particle system that runs on mouse position (A)

    • a kinect system that gives you values (B).

    Make sure A and B work separately.

    Now in the joined sketch (C) make sure both parts work independently and C runs.

    Now, we want to replace the mouse values with the kinect values and display the particle system. Thus make a global variable PVector globalLocation;

    Give it values from the kinect.

    Use it where you had mouseX and mouseY in the old sketch.

    please hit ctrl-t in processing to auto-format your sketch in processing.

    please post the entire sketch C as it is now.

    Thank you.

    Best, Chrisir ;-)

  • Answer ✓

    the concept is to instatiate the parameteres in draw function not in particle class

    you mean like this?

    import java.util.Iterator;
    ParticleSystem ps;
    
    void setup() {
      size(640, 360, P2D);
      //PImage image = loadImage("texture.png");
      ps = new ParticleSystem();
    }
    
    void draw() {
      background(0);
      //ps.applyForce();
      ps.run();
      ps.addParticle(mouseX, mouseY); // <- new particle here
    }
    
    class Particle {
    
      PVector location;
      PVector velocity;
      PVector acceleration;
      float lifespan;
    
      Particle(int x, int y) {
        location = new PVector(x, y);
        acceleration = new PVector(0, 0.05);
        velocity = new PVector(random(-1, 1), random(-2, 0)); 
        lifespan = 255;
      }
    
      void run() {
        update();
        display();
      }
    
      void update() {
        velocity.add(acceleration);
        location.add(velocity); 
        lifespan -= 2.0;
      }
    
      void display() {
        stroke(0, lifespan);
        imageMode(CENTER);
        tint(255, lifespan);
        ellipse(location.x, location.y, 5, 5);
        //fill(175, lifespan);
        //ellipse(location.x, location.y, 8, 8);
      }
    
      boolean isDead() {
        if (lifespan < 0.0) { 
          return true;
        } else {
          return false;
        }
      }
    }
    
    class ParticleSystem {
      ArrayList<Particle> particles;
      PVector origin;
    
      ParticleSystem() {
        particles = new ArrayList<Particle>();
      } 
    
      void addParticle(int x, int y) {
        particles.add(new Particle(x, y));
      }
    
      void run() {
        Iterator<Particle> it = particles.iterator();
        while (it.hasNext()) {
          Particle p = it.next();
          p.run();
          if (p.isDead()) {
            println("khgkhk");
            it.remove();
          }
        }
      }
    }
    
  • Chrisir all the sketches work,the only problem is when i put the parameters in draw function the effect completely different from the original sketch.i will try both solutions,and i will let you know.

  • koog your solution works it has to do something with the add.particle function :P

  • i will try also removing the iterator.

Sign In or Register to comment.