Why does changing the rendering colour and shape of a class of boids make them whizz around?

edited February 2014 in Questions about Code

Why does changing:

  void render() {   
    if (type == 1) {
      fill(156, 206, 255);
      stroke(16, 16, 222);
      ellipse(pos.x, pos.y, mass, mass);
      PVector dir = vel.get();
      dir.normalize();
      line(pos.x, pos.y, pos.x + dir.x*10, pos.y + dir.y*10);
    }
    else if (type == 2) {    

      // Draw a triangle rotated in the direction of velocity        
      float theta = vel.heading2D() + radians(90);
      pushMatrix();
      translate(pos.x, pos.y);
      rotate(theta); 
     fill(220, 0, 0);
     noStroke();
     beginShape(TRIANGLES);
     vertex(0, -mass);
     vertex(-3, mass);
     vertex(3, mass);
     endShape(); 
      popMatrix();
    }

to this :

  void render() {   
    if (type == 1) {
      fill(156, 206, 255);
      stroke(16, 16, 222);
      ellipse(pos.x, pos.y, mass, mass);
      PVector dir = vel.get();
      dir.normalize();
      line(pos.x, pos.y, pos.x + dir.x*10, pos.y + dir.y*10);
    }
    else if (type == 2) {    

      // Draw a triangle rotated in the direction of velocity        
      float theta = vel.heading2D() + radians(90);
      pushMatrix();
      translate(pos.x, pos.y);
      rotate(theta);
      fill(220, 52, 255);
      stroke(16, 16, 222);
      ellipse(pos.x, pos.y, mass, mass);
      PVector dir = vel.get();
      dir.normalize();
      line(pos.x, pos.y, pos.x + dir.x*10, pos.y + dir.y*10);         
      popMatrix();
    }

make the type 2 boids whizz around erratically and go outside the boundary?

I only wanted to change the shape and colour of the render to the same shape as type 1 but red, because they're representing parasitized sheep rather than predators now.

Answers

  • Answer ✓

    Ah, sorry, fixed it myself already!!

    (I don't know why it worked, but I just commented out

    pushMatrix();
        translate(pos.x, pos.y);
        rotate(theta);
    popMatrix();
    
  • edited February 2014

    Because you already used the pos PVector (I assume it's a PVector) in ellipse()/line(), shifting the current transformation matrix by pos would actually double the translation. Just replace line 19 and 22 in the above shown code with the following two lines:

    ellipse(0, 0, mass, mass);
    line(0, 0, dir.x*10, dir.y*10);
    

    As you already translated by pos, you don't need to use pos in you drawing functions.

Sign In or Register to comment.