method get() from the type PVector is deprecated

Hi there! I am using an ArrayList to store some PVectors as history of the position values of some moving particles; than I would like to use vector components to draw lines connecting all the old positions in my ArrayList, in order to have trails for my particles. Sound simple but after just a bunch of particles on screen, performance drop down heavily.

As I have see much more complex stuff running smooth on processing I am wondering what is it that slow down my program so impressively. I noticed the console message: "method get() from the type PVector is deprecated". So I am most likely doing something wrong but I can't figure it out.

Any help? Suggestion? Thanks a Lot!

class Particle {
  //DATA
  PVector position;
  PVector velocity;
  PVector acceleration;
  float mass;
  float G; // gravitational constant
  //////
  int posToStore; // how many position do I want to keep track of
  ArrayList <PVector> history; // an ArrayList to store my particle's prev positions


  //CONSTRUCTOR
  Particle(PVector v, float m) {
    position = v.get();
    velocity = new PVector();
    acceleration = new PVector();  
    mass = m;
    G = 1;
    //////
    posToStore = 400;
    history = new ArrayList <PVector>();

  }

  //FUNCTIONALITY
  void run() {
    //borders();
    update();
    display();
  }

  void applyForce(PVector force) {
    //PVector f = PVector.div(force, mass);
    PVector f = force.get(); // making a copy of the original force passed
    f.div(mass);
    acceleration.add(f);
  }

  void update() {
    velocity.add(acceleration);
    position.add(velocity);
    acceleration.mult(0); // resetting accelleration
    velocity.limit(7); // constraining velocity
    //////

    if(!isFinished()){ // if there are still available positions in the array 
      history.add(position.get()); // pushing a new PVector into my position's history arraylList
    } else {
      history.remove(0);
    }
  }

  void display() {
    stroke(255);
    //fill(175);
    println(history.size());
    for(int i = history.size()-1; i > 1; i--){
      //println(history[i].x);
      PVector oldhpos =  history.get(i-1);
      line(history.get(i).x, history.get(i).y, oldhpos.x, oldhpos.y);
    } 
    stroke(255,0,0);
    ellipse(position.x, position.y, 40*mass, 40*mass);
  }

  // attraction force defined for every particle as a function which return a PVector
  PVector attract(Particle p) {
    PVector force = PVector.sub(position, p.position); // Calculate direction of force
    float d = force.mag(); // distance btw objects 
    d = constrain(d, 5.0, 25.0); 
    force.normalize();  // now force just gives me the direction of the vector 
    float strength = (G * mass * p.mass) / (d * d);      // Calculate gravitional force magnitude
    force.mult(strength);                                // Get force vector --> direction * magnitude
    return force;
  }

  boolean isFinished(){
    if(history.size() > posToStore){
     return true; 
    } else {
     return false; 
    }    
  }
}

Answers

  • edited June 2016

    "method get() from the type PVector is deprecated".

    Just ignore it! :-j But if you can't bear the red sight of it, replace it w/ copy(). :P

    I am wondering what is it that slows down my program so impressively.

    • Field history doesn't belong to class Particle since it was declared at the top of your sketch.
    • It's bad programming to directly access something which belongs to another class.
    • So you should move your history loop outta your class Particle and turn it a sketch's method.
    • It's gonna become a double loop after that. See these 2 online examples in order to know how:

    http://studio.ProcessingTogether.com/sp/pad/export/ro.9s0026dE7B8v-
    http://studio.ProcessingTogether.com/sp/pad/export/ro.989GaZC5t7EkE

  • Thank you for the tip GoToLoop! I have a long learning way in front of me ;)

    What do you mean > Field history doesn't belong to class Particle since it was declared at the top of your sketch. ?

    ArrayList history; It is indeed declared inside my Particle class.. So I ma accessing something declared within the class... No?

  • edited June 2016

    ArrayList history; It is indeed declared inside my Particle class..

    Oops, my bad! Somehow it's escaped moi! X_X

  • edited June 2016
    • Generally when we plan some class, it's intended to represent 1 entity only.
    • In your case, it encapsulates all particles. So you should use a plural or a collective name for it.
    • For example, instead of Particle, rename it to Particles or ParticleList, etc.
    • However, since there's only 1 position, velocity, acceleration, mass, it means all your history share those attributes at 1 given time. No individualization at all! :|
  • what I am trying to achieve is to keep track of the history of my particle's prev position in the ArrayList, then use the position to draw lines btw them in the following loop (it works, but extremely slow):

    void display() {
        stroke(255);
        //Drawing the trail
        for(int i = 1; i < history.size(); i++){
          PVector oldhpos =  history.get(i-1);
          PVector hpos =  history.get(i);
          line(hpos.x, hpos.y, oldhpos.x, oldhpos.y);
        } 
        //Drawing the particle
        ellipse(position.x, position.y, 40*mass, 40*mass);
      }
    
  • edited June 2016 Answer ✓

    Those trails are similar to these 2 online examples:

    1. http://studio.ProcessingTogether.com/sp/pad/export/ro.9GTDpA6dp4tH1
    2. http://studio.ProcessingTogether.com/sp/pad/export/ro.90vdKMfkiO$zf

    But if you're looking for a more adequate container, how about an ArrayDeque as a Queue?:

    1. http://docs.Oracle.com/javase/8/docs/api/java/util/Queue.html
    2. http://docs.Oracle.com/javase/8/docs/api/java/util/ArrayDeque.html

    Gonna re-post a very good example for it from this old forum thread: O:-)
    https://forum.Processing.org/two/discussion/2829/fifo-queue-problem-with-code#Item_1

    /** 
     * Brownian Motion (v2.32)
     * by  Ammon.Owed (2013/Aug)
     * mod GoToLoop
     * 
     * forum.Processing.org/one/topic/controlling-the-length-of-line-
     * from-x-y-to-a-point-which-is-randomly-chosen
     *
     * forum.Processing.org/two/discussion/2829/fifo-queue-problem-with-code
     */
    
    import java.util.Queue;
    import java.util.ArrayDeque;
    
    static final int DIM = 100, MAX = 03000, DETAIL = 1000, DEPTH = 2000;
    static final int HUE = 1<<10, FPS = 60, TOLERANCE = 40, ALIASING = 2;
    
    static final float BOLD = 1.5, AMP = 10.0;
    static final boolean HAS_MAX_LIMIT = true;
    
    final Queue<PVector> points = new ArrayDeque(MAX);
    final PVector cam = new PVector(), lp = new PVector();
    
    float canvasRatio, zNear, zFar;
    
    void setup() {
      size(1000, 700, P3D);
      colorMode(HSB, HUE, 1, 1);
    
      frameRate(FPS);
      smooth(ALIASING);
    
      strokeWeight(BOLD);
      noFill();
    
      canvasRatio = (float) width/height;
    
      final float camZ = .5*height / tan(PI*FPS/360.0);
      zNear = camZ/AMP;
      zFar  = camZ*AMP;
    
      frameRate = TOLERANCE<<1;
    }
    
    void draw() {
      // local-cached & short-named variables:
      int fc = frameCount;
      final int fr = round(frameRate);
    
      // recycled or new vector point:
      final PVector np = 
        HAS_MAX_LIMIT & fc > MAX | !HAS_MAX_LIMIT & fr < TOLERANCE
        ? points.remove() : new PVector();
    
      // interpolated rotating camera aimed at latest added point (tail):
      cam.mult(.99);
      cam.add(PVector.mult(lp, .01, np));
    
      camera(cam.x + sin(fc*.01)*DETAIL, cam.y + cos(10.0 + fc*8e-3)*DETAIL, 
        cam.z - DEPTH, cam.x, cam.y, cam.z, 0, 1, 0);
    
      //perspective();
      perspective(THIRD_PI, canvasRatio, zNear, zFar);
    
      // draw colored curved lines:
      clear();
    
      beginShape();
    
      for (final PVector p : points) {
        stroke(fc++ & HUE-1, 1, 1);
        curveVertex(p.x, p.y, p.z);
      }
    
      endShape();
    
      // generate a new point at specified range:
      PVector.random3D(np, this).mult(DIM); // pick a new random direction.
      np.add(lp);                           // add that up from latest point.
      lp.set(np);                           // this is now the latest point too.
    
      points.add(np);                       // add new point to queue's tail.
    
      frame.setTitle("Brownian Motion   FPS: " + fr +
        "   Size: " + points.size());
    }
    
  • GULP! this code if far too advanced for my poor coding skills (I confess It is the first time I see "final" keyword ;) ) But I will dig into it hoping to find answers and improve my toolset! Thank you very very much for your help! ^:)^

  • edited June 2016

    It is to me as well. I'm not its author btW. :-\"
    The important thing is to observe how the PVector objects are recycled back into the Queue.
    That is, once the size() reaches MAX, no more PVector objects are ever created. *-:)

    // recycled or new vector point:
    final PVector np = 
      HAS_MAX_LIMIT & fc > MAX | !HAS_MAX_LIMIT & fr < TOLERANCE
      ? points.remove() : new PVector();
    

    See how variable np receives the PVector remove() from the ArrayDeque points container if fc > MAX.

  • Thanks! ~O)

  • (I confess It is the first time I see final keyword)

    https://Processing.org/reference/final.html

  • Interesting! The more I learn, the more there is to learn ;) Have a great day!

Sign In or Register to comment.