处理:旋转矩形

时间:2018-02-15 06:11:37

标签: processing

我尝试使用此解决方案:Processing, Rotate rectangle using matrix?但他们不使用rotate()函数,而且我这样做。

我正在试图旋转我正在画画的人的手臂。问题是每个人(手臂,头部,身体等)的所有身体部位的位置都是在Person类中设置的,因为我无法使该类静止,所以我不确定如何访问'角度'类中的变量来改变旋转。我试图使手臂的角度基于mouseY。

人员类:

public class Person {

  float height; 
  boolean isMale;
  float angle = 0;

  public Person(float height, boolean isMale, float angle) {
     this.height = height;
     this.isMale = isMale;
     this.angle = angle;
  }

  void display() {
       float x = random(-100, 1300);
       //float y = (height-1000)*200;
       float y = 0;
       //scale(height);
       fill(255);

       //legs
       rect(260+x, 320-y, 30, 130);
       rect(310+x, 320-y, 30, 130);

       //body
       if(isMale)
         rect(250+x, 170-y-height, 100, 170+height);
       else
         triangle(250+x-40, 320, 300+x, 150-height, 250+x+150, 320);

       //head
       ellipse(300+x, 150-y-height, 80, 80);

       //face
       fill(0);
       ellipse(285+x, 140-y-height, 10, 10);
       ellipse(315+x, 140-y-height, 10, 10);
       ellipse(300+x, 170-y-height, 30, 30);


       //arms
       fill(255);
       translate(275+x, 250-height); 
       rotate(radians(90));
       rect(0, 0, 150, 20);

     }

     public void updateAngle(float mouseYPos) {
        print(mouseYPos + "\n");
        angle += mouseYPos;
        //return angle+mouseYPos;
     }
}

Man类(扩展Person):

public class Man extends Person {

  float height;
  boolean isMale;
  //float angle;
   public Man(float height, boolean isMale, float angle) {
     super(height, isMale, angle);
     this.height = height;
     this.isMale = isMale;
     //this.angle = angle;
     } 

}

使用draw()函数的主类:

int x = width+1400;
float size = random(1, 1.7);
float speed = random(5, 15);
float angle = 0;

Person man1 = new Man(50, true, angle);

void setup() {
  size(1920, 1080);
  frameRate(80);
  background(100, 100, 255);
  //createRoad();


  man1.display();

  //Person man2 = new Man(0, true);
  //man2.display(); 

  //Person woman1 = new Woman(50, false);
  //woman1.display(); 
  //Person woman2 = new Woman(0, false);
  //woman2.display();
}

void draw() {
  Truck myTruck = new Truck(size, x);
  if (mousePressed) {
    myTruck.display();
    x-=speed; 
    man1.updateAngle(mouseY);
  }
}

void createRoad() {
  fill(100);
  rect(0, 450, 1920, 1080); 
  fill(255, 255, 0);
  rect(width-100, 750, 175, 25);
  rect(width-400, 750, 175, 25);
  rect(width-700, 750, 175, 25);
  rect(width-1000, 750, 175, 25);
  rect(width-1300, 750, 175, 25);
  rect(width-1600, 750, 175, 25);
  rect(width-1900, 750, 175, 25);
}

1 个答案:

答案 0 :(得分:0)

首先将man1.display();移至draw()然后移至display()内,您必须在某处使用angle变量,例如:

//arms
fill(255);
translate(275+x, 250-height); 
rotate(radians(angle));
rect(0, 0, 150, 20);
相关问题