【问题标题】:Processing: Rotate rectangle处理:旋转矩形
【发布时间】:2018-02-15 06:11:36
【问题描述】:

我尝试使用此解决方案: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);
}

【问题讨论】:

  • 您究竟是在哪里尝试访问angle 函数?

标签: processing


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-11
    • 2020-11-30
    • 1970-01-01
    相关资源
    最近更新 更多