random2D() function does not exist

edited October 2017 in Questions about Code

In the void update() function ... random2D() is unrecognised. I may be missing something small here but can't figure out right now...I've opened up an example code (Shiffman , nature of code) and in the example the function is recognised.

"The function random2D() doesn't exist" and the error line is acceleration = PVector.random2D(); in the mover class update

Mover mover;


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


void draw() {

  mover.update();
  mover.edgeCheck();
  mover.display();
}


class Mover {


  PVector location; 
  PVector velocity ;
  PVector acceleration;

  float topSpeed;



  Mover() {

    location = new PVector(width/2, height/2);
    velocity = new PVector(0, 0);
    //acceleration = new PVector(0,0);
    topSpeed = 10;
  }



  //functions


  void update() {
    velocity.add(acceleration);
    velocity.limit(topSpeed);
    location.add(velocity);
    acceleration = PVector.random2D();
  }

  void display() {

    stroke(0);
    fill(175);
    ellipse(location.x, location.y, 16, 16);
  }

  void edgeCheck() {

    if (location.x >width) {
      location.x = 0;
    } else if (location.x<0) {
      location.x = width;
    }

    if (location.y >height) {
      location.y = 0;
    } else if (location.y<0) {
      location.y = height;
    }
  }
}

class PVector {

  float x, y;

  PVector(float x_, float y_) {

    x = x_;
    y = y_;
  }

  void add(PVector v) {

    x = x + v.x; 
    y = y + v.y;
  }

  void sub(PVector v) {

    x = x - v.x; 
    y = y - v.y;
  }

  void mult(float n) {

    x = x * n; 
    y = y * n;
  }
  void div(float n) {

    x = x / n; 
    y = y / n;
  }


  float mag() {
    return sqrt(x*x + y*y);
  }

  void normalize() {

    float m = mag();
    if (m != 0) {
      div(m);
    }
  }

  void limit (float max) {

    if (max>10) {
      normalize();
      mult(max);
    }
  }
}
Tagged:

Answers

  • What is the specific error message text?

    Also, please edit your post above (gear icon) and format your code correctly: https://forum.processing.org/two/discussion/15473/readme-how-to-format-code-and-text

  • edited October 2017

    You just posted a class, not a minimal sketch, so it is really hard to know what you wanted to do with this. It seems to work fine if this is what you intended:

    Mover m;
    
    void setup() {
      m = new Mover();
    }
    
    void draw() {
      background(0);
      m.update();
      m.display();
    }
    
    class Mover {
      PVector location; 
      PVector velocity ; 
      PVector acceleration;
      float topSpeed;
      Mover() {
        location = new PVector(width/2, height/2);
        velocity = new PVector(0, 0);
        acceleration = new PVector(0, 0);
        topSpeed = 1;
      }
      void update() {
        velocity.add(acceleration);
        velocity.limit(topSpeed);
        location.add(velocity);
        acceleration = PVector.random2D();
      }
      void display() {
        stroke(0);
        fill(175);
        ellipse(location.x, location.y, 16, 16);
      }
      void edgeCheck() {
        if (location.x >width) {
          location.x = 0;
        } else if (location.x<0) {
          location.x = width;
        }
        if (location.y >height) {
          location.y = 0;
        } else if (location.y<0) {
          location.y = height;
        }
      }
    }
    
  • thanks jeremydouglass for the insight into code editing - i updated my query with the full sketch. I know the function works in other code, im wondering why it won't work in mine. Maybe with the full picture it will be easier for others to gain an insight..I feel like ive left some brackets off somewhere, so the code is running into other parts but I cant for the life of me spot where, and even if that is the case.

  • Why have you created another class exactly named PVector if Processing already got 1?! @-)
    https://Processing.org/reference/PVector.html

  • it was part of the exercise outlined in The Nature of Code in order to understand what the PVector class functions do...

  • so yes, there was a bit of confusion in my sketch - my sketch was lookin at THAT PVector class for a random function, but with a new sketch that didn't include my own class the problem resolved itself.

  • edited October 2017 Answer ✓

    If you wish to add or override methods from class PVector, you can create another class w/ a diff. name which extends PVector, and use that:

    https://Processing.org/reference/extends.html
    class PVecPlus extends PVector { }

  • Thanks for the reference GoToLoop, as the link states its fundamental to understanding OOP

Sign In or Register to comment.