curveVertex through PVector position

edited October 2013 in Questions about Code

hi , I just wanted to make a curveVertex through the path of my PVector which is moving . here's the code : ( I just dont know how to give Location of PVector in a frame before current frame)

PVector loc;
PVector vel;
PVector acc;



void setup(){
  size(1280,720);
  smooth();
  loc = new PVector (width/2,height/2);
  vel = new PVector (0,0);
  acc = new PVector (.01,0);

}


void draw(){

  acc = PVector.random2D();
  vel.add(acc);
  loc.add(vel);
  acc.limit(1);
  noFill();

  beginShape();
  curveVertex(loc.x,loc.y);
  ////////////////////////
  endShape();

}

Answers

  • Hi,

    For draw a curveVertex, you need at least 4 vertices. The first and last aren't drawn, they use for compute the tangent on the second and (n-1) vertices. For that you need memorised the old position. For exemple

    PVector loc, loc_old;
    PVector vel;
    PVector acc;
    
    
    
    void setup(){
      size(1280,720);
      smooth();
      loc = new PVector (width/2,height/2);
      loc_old = loc.get();
      vel = new PVector (0,0);
      acc = new PVector (.01,0);
    
    }
    
    
    void draw(){
    
      acc = PVector.random2D();
      vel.add(acc);
      loc.add(vel);
      acc.limit(1);
      noFill();
    
      beginShape();
      curveVertex(loc_old.x,loc_old.y);
      curveVertex(loc_old.x,loc_old.y);
      curveVertex(loc.x,loc.y);
      curveVertex(loc.x,loc.y);
      endShape();
    
      loc_old=loc.get();
    }
    

    But the curve can go out of window...

  • Yea I got it ! thanx man !

    also wrote this to check for edges :

      void checkEdges() {
    
        if (loc.x > width) {
          loc.x = 0;
            loc_old=loc.get();
        } 
        else if (loc.x < 0) {
          loc.x = width;
            loc_old=loc.get();
        }
    
        if (loc.y > height) {
          loc.y = 0;
            loc_old=loc.get();
        } 
        else if (loc.y < 0) {
          loc.y = height;
            loc_old=loc.get();
        }
      }
    
Sign In or Register to comment.