Problems with converting a P5.js sketch to Processing

Hi, we have to research about particles and I found one sketch that I liked, but noticed its on P5.js. I’ve trying to open remake this sketch in processing but i’m having a really hard time doing it (3 and half hours until now)

My code is this

Tadpoles[] tads = new Tadpoles[40];

void setup() {
    size(450, 450);
    for (int i = 0; i < tads.length; i++) {
    tads[i] = new Tadpoles();
  };
}

void draw() {
    for (int i = 0; i < 40; i += 1) {
    tads[i].update();
    tads[i].display();
  }
}
class Tadpoles {
  float i, ii, t = 0, ts = 0;
  float x=0,y=0;
  Prev[]  p = new Prev[10];

  Tadpoles(){
  t = random(0,10000);
  ts = random(0,100);
  }

   void update () {
    PVector pos = (
      noise(t + ts) * width,
      noise(t + tts + 100) * height
    );
    t = t + 0.005;
  }

  void display () {
    fill(0);
    ellipse(pos.x, pos.y, 4, 4);

for (ii = 9; ii > 0; ii -= 1) {
  this.prev[ii].x = this.prev[ii - 1].x;
  this.prev[ii].y = this.prev[ii - 1].y;
  fill(20, 50, 0, 10);
  ellipse(this.prev[ii].x, this.prev[ii].y, 14-ii, 14-ii);
}
this.prev[0].x = this.pos.x;
this.prev[0].y = this.pos.y;
  }
}

The class Tadpoles is not finished, but i wanted to ask because i just keep and keep getting errors, so I have no idea if i’m actually making progress

Is there a concrete way to convert the syntax of p5.js to processing ? (it’s javascript to java, right) ?

thanks for reading

Hello,

There are resources here:
processing.org

Check out the Simulate examples.
I had a lot of fun dissecting this code and understanding it and now have some cool projects!

Nature of Code also includes particles simulations:
The Nature of Code

This may help:
Processing transition · processing/p5.js Wiki · GitHub

I suggest you glean insight from the examples and work on your own simulation or particle system… when you are ready.

:)

Hi, after reading some documentation I reached this stage but I’m getting an error


// A Mover object
Mover mover;

void setup() {
  size(640,360);
  mover = new Mover(); 
}

void draw() {
  background(0);
  
  // Update the location
  mover.update();
  // Display the Mover
  mover.display(); 
}
class Mover {

  // The Mover tracks location, velocity, and acceleration 
  PVector location;
  PVector velocity;
  PVector acceleration;
  // The Mover's maximum speed
  Particles[] particle = new Particles[10];
  float topspeed;
  int e;

  Mover() {
    // Start in the center
    location = new PVector(width/2,height/2);
    velocity = new PVector(0,0);
    topspeed = 5;
    
    for(e = 0; e<9; e++){
      particle[e] = new Particles();
  }}

  void update() {
    
    // Compute a vector that points from location to mouse
    PVector mouse = new PVector(mouseX,mouseY);
    PVector acceleration = PVector.sub(mouse,location);
    // Set magnitude of acceleration
    acceleration.setMag(0.2);
    
    // Velocity changes according to acceleration
    velocity.add(acceleration);
    // Limit the velocity by topspeed
    velocity.limit(topspeed);
    // Location changes by velocity
    location.add(velocity);
  }

  void display() {
    fill(255);
    ellipse(location.x, location.y, 4, 4);
    for(int i = 9; i>0; i -=1){
      particle[i].x = particle[i-1].x;
      particle[i].y = particle[i-1].y;
    }
    particle[0].x = location.x;
    particle[0].y = location.y;
  }
}
class Particles {
  float x,y;
  
  Particles() {
    init();
  }
  void init(){
    x = 0;
    y = 0;
  }
}

I get nuller pointer exception in line

   particle[i].x = particle[i-1].x;

This code is to generate the particle (the head and tail).

Gonna keep trying

I found the problem, in

    for(e = 0; e<9; e++){
      particle[e] = new Particles();
  }

Had to change the 9 for the list length and that’s it :slight_smile: !

2 Likes

This is my take on it. Here’s my forked version: :fork_and_knife:

/**
 * Tadpoles (v1.0.1) [Java/Pjs]
 *
 * p5.js by Srdjan Markovi.ch (2016/Oct/13)
 * https://OpenProcessing.org/sketch/386063
 *
 * Pjs fork by GoToLoop (2021/Jun/29)
 * https://OpenProcessing.org/sketch/1226376
 *
 * https://Discourse.Processing.org/t/
 * problems-with-converting-a-p5-js-sketch-to-processing/30963/4
 */

final Tadpole[] tadpoles = new Tadpole[120];

void setup() {
  size(500, 500);
  noStroke();
  for (int i = 0; i < tadpoles.length; tadpoles[i++] = new Tadpole());
}

void draw() {
  background(-1);
  for (final Tadpole tadpole : tadpoles)  tadpole.update().display();
}

class Tadpole {
  final PVector prevs[] = new PVector[10], pos = new PVector();
  float t = random(10000), ts = random(100);

  Tadpole() {
    for (int i = 0; i < prevs.length; prevs[i++] = new PVector());
  }

  Tadpole update() {
    final float tts = t + ts;
    t += .005;

    pos.set(noise(tts) * width, noise(tts + 100) * height);
    prevs[0].set(pos);

    return this;
  }

  Tadpole display() {
    fill(0);
    ellipse(pos.x, pos.y, 4, 4);
    fill(20, 50, 0, 10);

    for (int i = prevs.length; --i > 0; ) {
      final PVector curr = prevs[i];
      final int diam = 14 - i;

      curr.set(prevs[i - 1]);
      ellipse(curr.x, curr.y, diam, diam);
    }

    return this;
  }
}

:roll_eyes: Oh, some1 had just flagged my alternative solution above! O_o