can´t find error :-?

edited April 2017 in Questions about Code

I have an array of point ("stalkers") that are going to pass from state 1 to state 2 once the condition is reached. The thing is that the, in theory static, object "ball" gets also affected by this when it shouldnt and changes its position. :-??

Ball b; 
ArrayList <Target> stalkers;

boolean fCounter=true;
float hCounter;
int pop;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void setup() {
  size(1200, 900);
  pop= int (random(5, 15));
  hCounter=300;

  b= new Ball();
  stalkers= new ArrayList <Target>();

  for (int i=0; i<pop; i++) {
    Target other= new Target();
    stalkers.add(other);
  }
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void draw() {
  background(11);

  b.showUp();

  if (fCounter) {
    hCounter--;
    for (int i=0; i<pop; i++) {
      Target coorn= stalkers.get(i);
      coorn.crawl();
      coorn.display();//if counter (something) store state 1 in vectors
    }
  } else {//else state 2
    for (int i=0; i<pop; i++) {
      Target tg= stalkers.get(i);    
      PVector feed = b.sniff(tg);
      tg.update(feed); 
      tg.stalk();
    }
  }
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class Ball {
  PVector org;

  Ball() {
    org=new PVector(600, 450);
  }

  void showUp() {
    display();
  }

  void display() {
    fill(255, 0, 0);
    noStroke();
    ellipse(org.x, org.y, 20, 20);
  }

  PVector sniff(Target msk) {//new Vector(force) from human to global tStalker
    PVector force=org.sub(msk.loc);   
    float dist=force.mag();
    // println(dist);
    dist=constrain(dist, 0., 1.);
    force.normalize();
    force.mult(1 );
    println(force.x, force.y);
    return force;
  }
}
   ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class Target {
  PVector loc, vel, acc, state1;
  PVector sniff;

  Target() {
    loc= new PVector(random(1200), random(900));
    vel= new PVector(random(-1, 1), random(-1, 1));
    acc= new PVector();
    state1= new PVector(random(-.1, .1), random(-.1, .1));
  }

  void stalk() {
    move();
    display();
    limits();
  }
  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  void display() {
    noStroke();
    fill(255, 0, 0);
    rect(loc.x, loc.y, 2, 2);
  }

  void update(PVector force) {
    acc.add(force);
  }

  void move() {
    vel.add(acc);
    vel.limit(2.);
    loc.add(vel);
  }
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  void crawl() {
    if (hCounter>=0) {
      loc.add(state1);
      limits();
    } else { 
      fCounter = false;
    }
  }
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  void limits() {
    if (loc.x<0 || loc.x >1200) {
      vel.x = -vel.x;
    } else if (loc.y<0 || loc.y >900) {
      vel.y = -vel.y;
    }
  }
}

:-?

Tagged:

Answers

Sign In or Register to comment.