处理:旋转矩形



我尝试使用此解决方案:处理,使用矩阵旋转矩形?但是他们不使用rotate((函数,而我愿意。

我试图旋转我正在绘制的人的手臂。问题在于,每个人(武器,头部,身体等(的所有身体部位的位置都设置在一个人类的班级中,并且由于我无法使该班级静态,我不确定如何访问'角度'类中的变量更改旋转。我正在尝试将手臂的角度从鼠标中取决于鼠标。

人类课程:

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;
     }
}

男人类(扩展人(:

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);
}

首先将 man1.display();移至 draw(),然后在 display()中使用 angle变量,例如:

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

最新更新