Problems with translating a p5js sketch into processing

hey coders,
i translate a p5js scetch to processing because i want to generate a mov file. the scetch is based on a tutorial from daniel shiffman on kadenze. but i´ve got a problem now with a boolean output. how can i code this piece of p5js code in processing

function draw() {
  for( var i = 0; i < particles.length; i++) {
    if(particles[i].isDead()) {
      //code
    }
  }
}

function Particles() {
   this.isDead = function() {
      var distance = p5.Vector.sub(attractor.pos, this.pos);
      var d = distance.mag();
    
      if(d < 5) {
        return true;
      } else {
        return false;
      }
    }
}

first i tried it with void, but void hasn´t got an output. then i tried something like this with boolean but it also doesn´t work.

void setup() {
   //code
}

void draw () {
   for (int i = 0; i < particles.length; i++) {
        if(particles[i].isDead()) {
          //code
        }
    }
}

Class Particle {
   Particle() {
        //code
    }

    boolean isDead() {
        PVector distance = PVector.sub(a.location, location);
        float d = distance.mag();
   
        if(d < 5) {
          return true;
        } else {
          return false;
        }
   }
}

it will be great if somebody can help me.

regards mattias

Tagged:

Answers

  • edited August 2016

    It's pretty rare conversions from web to desktop! @-)
    Unfortunately your posted code isn't runnable b/c it tries to access members not present, such as variable attractor.
    Therefore I've decided as 1st step get some minimum runnable p5.js sketch. Then work from there.

    Tweaked code is posted below and "watchable" from this link:
    http://CodePen.io/anon/pen/akxYWo?editors=1010

    <script async src=http://p5js.org/js/p5.min.js></script>
    
    /**
     * Particle Attractor Template (v2.1.2)
     * GoToLoop (2016-Aug-19)
     *
     * forum.Processing.org/two/discussion/17911/
     * problems-with-translating-a-p5js-sketch-to-processing
     *
     * CodePen.io/anon/pen/akxYWo?editors=1010
     */
    
    const particles = [];
    let attractor;
    
    function setup() {
      createCanvas(500, 500);
      for (let i = 0; i < 5; ++i)  particles.push(new Particle);
      attractor = new Attractor;
    }
    
    function draw() {
      background(0);
    
      const {pos} = attractor;
    
      for (let p of particles) {
        if (p.isDead(pos)) {
          // code...
        }
      }
    }
    
    class Particle {
      static get DIST() { return 5; }
    
      constructor() {
        this.pos = createVector(), this.distance = createVector();
      }
    
      isDead(position) {
        return p5.Vector.sub(position, this.pos, this.distance).mag() < Particle.DIST;
      }
    }
    
    class Attractor {
      constructor() {
        this.pos = createVector();
      }
    }
    
  • edited August 2016

    Now the corresponding Java Mode sketch: :bz

    /**
     * Particle Attractor Template (v2.1.2)
     * GoToLoop (2016-Aug-19)
     *
     * forum.Processing.org/two/discussion/17911/
     * problems-with-translating-a-p5js-sketch-to-processing
     *
     * CodePen.io/anon/pen/akxYWo?editors=1010
     */
    
    import java.util.List;
    
    final List<Particle> particles = new ArrayList<Particle>();
    Attractor attractor;
    
    void setup() {
      size(500, 500);
      for (int i = 0; i < 5; ++i)  particles.add(new Particle());
      attractor = new Attractor();
    }
    
    void draw() {
      background(0); 
    
      final PVector pos = attractor.pos; 
    
      for (final Particle p : particles) {
        if (p.isDead(pos)) {
          // code...
        }
      }
    }
    
    class Particle {
      static final int DIST = 5; 
    
      final PVector pos = new PVector(), distance = new PVector(); 
    
      boolean isDead(final PVector position) {
        return PVector.sub(position, pos, distance).mag() < DIST;
      }
    }
    
    class Attractor {
      final PVector pos = new PVector();
    }
    
  • thanks a lot :)

  • Crossposted: http://stackoverflow.com/questions/39047288/problems-with-translating-a-p5js-sketch-into-processing

    Please let us know when you crosspost to multiple websites, that way we don't waste our time answering questions that already have answers on other sites.

Sign In or Register to comment.