Return to an original position

edited March 2017 in Questions about Code

I wonder how I can return the walker to its original position once the counter condition is reached, no matter what the new position is.

PVector walker;
PVector start;
PVector seek;
float count;


void setup() {
  size (900, 900);

  walker= new PVector (width/2, height-10);
  start= new PVector(0, -1);
  seek=new PVector(random(-1, 1), random( 1));

  count=0;
}

void draw() {
  background(53, 111);

  count++;

  stroke(0, 0, 255);
  rect(width/2, height-10, 10, 10);

  noStroke();
  fill(255, 0, 0);
  ellipse(walker.x, walker.y, 5, 5);

  startt();
  limits();
  println(count);
}

void startt() {
  if (walker.y>=150 && count<50) {
    walker.add(start);
  } else {
    walker.add(seek);
  }
}
// once the condition is reached (count=1000) ,  walker "walks back" to its original position.


void limits() {
  if (walker.x>width || walker.x <0) {
    seek.x = -seek.x;
  } else if (walker.y>height || walker.y <0) {
    seek.y = -seek.y;
  }
}

Answers

  • @jozze -- do you mean that you want to restore the original position? You defined this as:

    walker= new PVector (width/2, height-10);
    

    ...so that would be:

    if (count==1000) {
        walker.set(width/2, height-10); // walker to original position
    }
    

    You could also store that original position in a global PVector origin and then walker.set(origin.x, origin.y);

  • @jeremydouglass __ the .set method resets the position straight away from the current one. We do not see the object "going back home". Although this is not desired, it pointed out a way I think works out:

      if (count==1000) {
      current.set(walker.x, walker.y);//store current position in a new vector 
      current.sub(origin);// measure distance between origin and current position
      current.normalize();// this line is added "to see" the way back
      walker.sub(current);
     }
    

    @GoToLoop __ Somehow yes, I found a similar approach in your AntHill Sketch (lines 118,122)

    thx both of you!

Sign In or Register to comment.