处理:沿同一方向移动多个形状

时间:2017-06-08 19:59:43

标签: vector rotation processing

所以我试图使用PVector值使多个形状在同一方向上移动。我面临的问题是,为了制作我想要的对象(在这种情况下是苍蝇的粗略图示),我需要旋转对象。这样做可以使矢量数学添加到对象旋转的方向。我可以用一些函数或数学来标准化每个形状的角速度吗?

class tFly{

boolean count=false;
boolean countTwo=false;
boolean move=false;
PVector location;
PVector velocity;




  tFly(){
   location= new PVector(0,0);
  velocity = new PVector(1,1); 
  }
void flap(){//flap wings if moving, else keep them still

  if(move==false){
  pushMatrix(); //wings
noStroke();

translate(235,285);
rotate(radians(25));
ellipse(location.x,location.y,25,50);

popMatrix();


pushMatrix(); //wings
noStroke();

translate(263,285);
rotate(radians(340));
ellipse(location.x,location.y,25,50);


popMatrix();


  }else{
pushMatrix(); //wings
noStroke();
translate(235,285);
if(count==false){
rotate(radians(49));
ellipse(location.x,location.y,25,50);
count=true;
}else{
rotate(radians(21));
ellipse(location.x,location.y,25,50);
count=false;
}

popMatrix();


pushMatrix(); //wings
noStroke();
translate(263,285);
if(countTwo==false){
rotate(radians(320));
ellipse(location.x,location.y,25,50);
countTwo=true;
}else{
rotate(radians(340));
ellipse(location.x,location.y,25,50);
countTwo=false;
}

popMatrix();
}
}
void display(){
  fill(0);
ellipse(location.x+250,location.y+265,20,50);

ellipse(location.x+250,location.y+280,25,34);
pushMatrix();

translate(240,230);
rotate(radians(70));
rect(location.x,location.y,15,.5);
popMatrix();

pushMatrix();
translate(262,232);
rotate(radians(110));
rect(location.x,location.y,15,.5);
popMatrix();


pushMatrix();
translate(230,260);
rotate(radians(40));
rect(location.x,location.y,15,.5);
popMatrix();

pushMatrix();
translate(270,262);
rotate(radians(140));
rect(location.x,location.y,15,.5);
popMatrix();

pushMatrix();
translate(240,285);
rotate(radians(120));
rect(location.x,location.y,15,.5);
popMatrix();

pushMatrix();
translate(260,285);
rotate(radians(50));
rect(location.x,location.y,15,.5);
popMatrix();

fill(255,0,0);

ellipse(location.x+240,location.y+250,20,18);
ellipse(location.x+260,location.y+250,20,18);
fill(128,128,128,127);

flap();

}
void update(){
location.add(velocity);

}

}

以下是我在设置和绘制中使用它的方法

tFly fly;


void setup(){
size(500,500);
background(255);
fly=new tFly();
}

void draw(){
background(255);
  ellipseMode(CENTER);

fly.display();
fly.update();

}

1 个答案:

答案 0 :(得分:1)

您已经掌握pushMatrix()/popMatrix() ...为什么不在display()电话中嵌套来自pushMatrix()/popMatrix()的所有绘图电话,并简单地翻译整个组?

class tFly {

  boolean count=false;
  boolean countTwo=false;
  boolean move=true;
  PVector location;
  PVector velocity;




  tFly() {
    location= new PVector(0, 0);
    velocity = new PVector(1, 1);
  }
  void flap() {//flap wings if moving, else keep them still

    if (move==false) {
      pushMatrix(); //wings
      noStroke();

      translate(235, 285);
      rotate(radians(25));
      ellipse(0, 0, 25, 50);

      popMatrix();


      pushMatrix(); //wings
      noStroke();

      translate(263, 285);
      rotate(radians(340));
      ellipse(0, 0, 25, 50);


      popMatrix();
    } else {
      pushMatrix(); //wings
      noStroke();
      translate(235,285);
      if (count==false) {
        rotate(radians(49));
        ellipse(0, 0, 25, 50);
        count=true;
      } else {
        rotate(radians(21));
        ellipse(0, 0, 25, 50);
        count=false;
      }

      popMatrix();


      pushMatrix(); //wings
      noStroke();
      translate(263, 285);
      if (countTwo==false) {
        rotate(radians(320));
        ellipse(0, 0, 25, 50);
        countTwo=true;
      } else {
        rotate(radians(340));
        ellipse(0, 0, 25, 50);
        countTwo=false;
      }

      popMatrix();
    }
  }
  void display() {
    pushMatrix();
    translate(location.x, location.y);

    fill(0);
    ellipse(250, 265, 20, 50);

    ellipse(250, 280, 25, 34);
    pushMatrix();

    translate(240, 230);
    rotate(radians(70));
    rect(0, 0, 15, .5);
    popMatrix();

    pushMatrix();
    translate(262, 232);
    rotate(radians(110));
    rect(0, 0, 15, .5);
    popMatrix();


    pushMatrix();
    translate(230, 260);
    rotate(radians(40));
    rect(0, 0, 15, .5);
    popMatrix();

    pushMatrix();
    translate(270, 262);
    rotate(radians(140));
    rect(0, 0, 15, .5);
    popMatrix();

    pushMatrix();
    translate(240, 285);
    rotate(radians(120));
    rect(0, 0, 15, .5);
    popMatrix();

    pushMatrix();
    translate(260, 285);
    rotate(radians(50));
    rect(0, 0, 15, .5);
    popMatrix();

    fill(255, 0, 0);

    ellipse(240, 250, 20, 18);
    ellipse(260, 250, 20, 18);
    fill(128, 128, 128, 127);

    flap();

    popMatrix();
  }
  void update() {
    location.add(velocity);
  }
}


tFly fly;


void setup() {
  size(500, 500);
  background(255);
  fly=new tFly();
}

void draw() {
  background(255);
  ellipseMode(CENTER);

  fly.display();
  fly.update();
}