i cant seem to find the syntaxerror in setup and draw in this flowfield sketch

35 Views Asked by At

this is my first post on stackoverflow and I am also quite new to Java, I made a sketch in processing 4.3, (first in python mode, which did actually work, then i made this Java version) that generates a flowfield based on Perlin noise, however whenever i try to run my sketch, i get a syntaxerror: "missing operator, semicolon or '}' near draw()" (the squiggly line also appears under setup ). Could anyone help me find the error, and maybe even help with some improvements to my code? Thanks in advance

final int particlecount;
final float dz, resolution, flowstrength;


//amount of particles, most performance heavy, up until 10k renders at a reasonable rate on my machine
particlecount = 1000;
//variation in noisefield, higher means more zoomed in
resolution = 5;
//speed in variation of noisefield, increment of z-axis, higher means slower
dz = 1;
//speed of particles, higher means faster, but makes uglier sketches
flowstrength = 5;


public class Particle {
  public float x, y;//position
  public float dx, dy;//speed
  public float px, py;//for drawing lines and calculating angle
  public float angle, hue;//color variables


  public Particle(float part_x, float part_y) {
    this.x = part_x;
    this.y = part_y;
    this.dx = 0;
    this.dy = 0;
    this.px = x;
    this.py = y;
    this.angle=0;
    this.hue=0;
  }

  public void updatevel(float newdx, float newdy) {
    this.dx = newdx;
    this.dy = newdy;
  }

  //looparound
  public void edges(boolean doLoopover) {
    if (doLoopover == true) {
      if (this.x <= 0) {
        this.x = width;
      }
      if (this.y <= 0) {
        this.y = height;
      }

      if (this.y >= height) {
        this.y = 0;
      }
      if (this.x >= width) {
        this.x = 0;
      }
      //else just pick a random position
    } else {
      if (this.x <= 0 || this.y <= 0 || this.x >= width || this.y >= height) {
        this.x = random(width);
        this.y = random(height);
      }
    }
  }

  public void show(int alpha) {
    float deltax = this.x - this.px;
    float deltay = this.y - this.py;
    this.angle = atan2(deltay, deltax);

    //map vertical direction to a brightness value; the more positive, the brighter
    float bright = map(sin(this.angle), -1, 1, 0, 100);

    push();
    colorMode(HSB);
    stroke(this.hue, 0, bright, alpha);

    //only draw non-looparound steps
    if (dist(this.x, this.y, this.px, this.py) < flowstrength * 2) {
      beginShape();
      vertex(this.x, this.y);
      vertex(this.px, this.py);
      endShape();
    }
    //point(this.x, this.y);
    pop();
  }


  public void move() {
    this.px = this.x;
    this.x += this.dx;

    this.py = this.y;
    this.y += this.dy;
  }
}



public class NoiseField {
  float resolution, z, dz;

  public NoiseField(float intresolution, float intdz) {
    this.resolution = pow(intresolution, -3);
    this.dz = pow(intdz, -1);
    this.z = 0;
  }
  //generate noise value at position, optional noise-angle
  public float getnoise(float posx, float posy, boolean returnangle) {
    float xval = this.resolution * posx;
    float yval = this.resolution * posy;
    calnoise = noise(xval, yval, this.z);
    if (returnangle == true) {
      return calnoise * TWO_PI;
    } else {
      return calnoise;
    }
  }
  //increment the z-axis
  public void tick() {
    this.z += this.dz;
  }


  public void resetZ() {
    this.z = 0;
  }
}


Particle[] particleList;
particleList = new Particle[particlecount];

//initialise noise field
noisefield = Noisefield(resolution, dz);

void setup() {
  size(500, 500);
  colorMode(RGB);
  background(40);
  stroke(5);
  strokeWeight(1);
  noFill();

  for (int i = 0; i < particlecount; i++) {
    Particle newparticle = new Particle(random(width), random(height) );
    particleList[i] = newparticle;
  }
}


void draw() {
  colorMode(RGB);
  background(20);


  for (int i = 0; i < particlecount; i++) {
    //access current particle and calculate its velocity based on its position in the flowfield
    Particle currentparticle = particleList[i];
    paticlenoise = noisefield.getnoise(currentparticle.x, currentparticle.y, true);
    float xvel = flowstrength * cos(particlenoise);
    float yvel = flowstrength * sin(particlenoise);

    currentparticle.updatevel(xvel, yvel);
    currentparticle.move();
    //do looparound
    currentparticle.edges(true);
    currentparticle.show(50);
  }

  //saveFrame("frames/flowfield_####.png");
  push();
  fill(40);
  rect(5, 0, 30, 20);
  fill(200);
  text(frameCount, 10, 10);
  pop();
  noisefield.tick();
}

I looked through my whole code, and checked for semicolons and correctly matched brackets etc. but nothing seemed wrong, i also previously had variables for the canvas size and i read that processing doesn't like you doing that so i just hardcoded the canvas size

1

There are 1 best solutions below

0
elcito07 On

Apparently i did miss some type declarations, calnoise = noise(xval, yval, this.z); missed a float, noisefield = Noisefield(resolution, dz); missed Noisefield and paticlenoise = noisefield.getnoise(currentparticle.x, currentparticle.y, true); had a spelling mistake.

this is my now working code:

//speed in variation of noisefield, increment of z-axis, higher means slower
final float dz = 1;
//variation in noisefield, higher means more zoomed in
final float resolution = 6;
//speed of particles, higher means faster, but makes uglier sketches
final float flowstrength = 1;
//amount of particles, most performance heavy, up until 10k renders at a reasonable rate on my machine
final int particlecount = 10000;

//initialise list of particles
Particle[] particleList = new Particle[particlecount];



public class Particle {
  public float x, y;//position
  public float dx, dy;//speed
  public float px, py;//for drawing lines and calculating angle
  public float angle, hue;//color variables


  public Particle(float part_x, float part_y) {
    this.x = part_x;
    this.y = part_y;
    this.dx = 0;
    this.dy = 0;
    this.px = x;
    this.py = y;
    this.angle=0;
    this.hue=0;
  }

  public void updatevel(float newdx, float newdy) {
    this.dx = newdx;
    this.dy = newdy;
  }

  //looparound
  public void edges(boolean doLoopover) {
    if (doLoopover == true) {
      if (this.x < 0) {
        this.x = width;
      }
      if (this.y < 0) {
        this.y = height;
      }

      if (this.y > height) {
        this.y = 0;
      }
      if (this.x > width) {
        this.x = 0;
      }
      //else just pick a random position
    } else {
      if (this.x <= 0 || this.y <= 0 || this.x >= width || this.y >= height) {
        this.x = random(width);
        this.y = random(height);
      }
    }
  }

  public void show(int alpha) {


    //map vertical direction to a brightness value; the more positive, the brighter

    push();
    colorMode(HSB);
    stroke(0, 0, 100, alpha);

    //only draw non-looparound steps
    if (dist(this.x, this.y, this.px, this.py) < flowstrength * 2) {
      beginShape();
      vertex(this.x, this.y);
      vertex(this.px, this.py);
      endShape();
    }
    //point(this.x, this.y);
    pop();
  }


  public void move() {
    this.px = this.x;
    this.x += this.dx;

    this.py = this.y;
    this.y += this.dy;
  }
}



public class NoiseField {
  float resolution, z, dz;

  public NoiseField(float intresolution, float intdz) {
    this.resolution = pow(intresolution, -3);
    this.dz = pow(intdz, -1);
    this.z = 0;
  }
  //generate noise value at position, optional noise-angle
  public float getnoise(float posx, float posy, boolean returnangle) {
    float xval = this.resolution * posx;
    float yval = this.resolution * posy;
    float calcnoise = noise(xval, yval, this.z);

    if (returnangle == true) {
      return calcnoise * TWO_PI;
    } else {
      return calcnoise;
    }
  }
  //increment the z-axis
  public void tick() {
    this.z += this.dz;
  }


  public void resetZ() {
    this.z = 0;
  }
}
NoiseField noisefield = new NoiseField(resolution, dz);
void setup() {


  size(1920, 1080);
  colorMode(RGB);
  background(40);
  stroke(5);
  strokeWeight(1);
  noFill();

  for (int i = 0; i < particlecount; i++) {
    Particle newparticle = new Particle(random(width), random(height) );
    particleList[i] = newparticle;
  }
}

void draw() {
  colorMode(RGB);
  background(0);


  for (int i = 0; i < particlecount; i++) {
    //access current particle and calculate its velocity based on its position in the flowfield
    Particle currentparticle = particleList[i];
    float particlenoise = noisefield.getnoise(currentparticle.x, currentparticle.y, true);
    float xvel = flowstrength * cos(particlenoise);
    float yvel = flowstrength * sin(particlenoise);

    currentparticle.updatevel(xvel, yvel);
    currentparticle.move();
    //do looparound
    currentparticle.edges(true);
    currentparticle.show(200);
  }

  //saveFrame("frames/flowfield_####.png");
  push();
  fill(40);
  rect(5, 0, 30, 20);
  fill(200);
  text(frameCount, 10, 10);
  pop();
  //noisefield.tick();
}