# Problems with converting a P5.js sketch to Processing

**URL:** https://discourse.processing.org/t/problems-with-converting-a-p5-js-sketch-to-processing/30963
**Category:** Coding Questions
**Tags:** homework
**Created:** [June 29, 2021, 12:10pm UTC](https://discourse.processing.org/t/problems-with-converting-a-p5-js-sketch-to-processing/30963 "2021-06-29T12:10:09Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![lmato](https://avatars.discourse-cdn.com/v4/letter/l/41988e/32.png) [@lmato](https://discourse.processing.org/u/lmato)
#### Post date: [June 29, 2021, 12:10pm UTC](https://discourse.processing.org/t/problems-with-converting-a-p5-js-sketch-to-processing/30963/1 "2021-06-29T12:10:09Z")

</div>

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)

> **[Tadpoles - OpenProcessing](https://openprocessing.org/sketch/386063)**
>
> Tadpoles simulation

My code is this

```auto
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();
  }
}

```

```auto
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

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [June 29, 2021, 12:52pm UTC](https://discourse.processing.org/t/problems-with-converting-a-p5-js-sketch-to-processing/30963/2 "2021-06-29T12:52:58Z")

</div>

Hello,

There are resources here:  
_[processing.org](http://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](https://natureofcode.com/book/)_

This may help:  
_[Processing transition · processing/p5.js Wiki · GitHub](https://github.com/processing/p5.js/wiki/Processing-transition)_

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

`:)`

---

<div class="post-metadata">

### Author: ![lmato](https://avatars.discourse-cdn.com/v4/letter/l/41988e/32.png) [@lmato](https://discourse.processing.org/u/lmato)
#### Post date: [June 29, 2021, 1:58pm UTC](https://discourse.processing.org/t/problems-with-converting-a-p5-js-sketch-to-processing/30963/3 "2021-06-29T13:58:17Z")

</div>

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

```auto

// 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(); 
}

```

```auto
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;
  }
}

```

```auto
class Particles {
  float x,y;
  
  Particles() {
    init();
  }
  void init(){
    x = 0;
    y = 0;
  }
}

```

I get nuller pointer exception in line

```auto
   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

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

```

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

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [June 29, 2021, 4:26pm UTC](https://discourse.processing.org/t/problems-with-converting-a-p5-js-sketch-to-processing/30963/4 "2021-06-29T16:26:32Z")

</div>

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

> **[Tadpoles - OpenProcessing](https://openprocessing.org/sketch/1226376)**
>
> Tadpoles simulation

```auto
/**
 * 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;
  }
}

```

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [June 29, 2021, 5:01pm UTC](https://discourse.processing.org/t/problems-with-converting-a-p5-js-sketch-to-processing/30963/5 "2021-06-29T17:01:47Z")

</div>

🙄 Oh, some1 had just flagged my alternative solution above! O\_o
