在运动物体的轴上旋转

rotation on the axis of a moving body

我正在学习如何使用 push 和 pop 矩阵。我希望汽车(对象)在其轴上旋转而不是(旋转)与翻译坐标的 0,0。 {初级编程爱好者}

尝试在使用 rotate() 时重新平移轴。

PImage c = new PImage();

Car forza = new Car();
Trees tr = new Trees();

float wrap = 100;

void setup(){
  size(800,800);
}

void draw(){
  background(0);
  forza.update();
  forza.display();
  tr.road();
}

class Car {

  float posX;
  float posY;
  float speed;
  float accel;
  float angle;

  Car(){
    posX = 0;
    posY = 0;
    speed = .9;
    angle = sin(0);
    accel = 0;
  }


  void update(){
    pushMatrix();
    translate(posX,posY);
    if (keyPressed) {
      if (key == 'd') {
        angle += 1;
      }else if (key == 'a'){
        angle -= 1;
        rotate(radians(angle));
      } else if (key == 'w'){
        posX += speed;
      } else if (key == 's'){
        posX -= speed;
      }
    }  
    popMatrix();
  }

  void display(){
    pushMatrix();
    translate(width/2,height/2);
    rotate(radians(angle));
    c = loadImage("car.jpg");
    fill(255);
    stroke(255);
    imageMode(CENTER);
    image(c,posX,posY,wrap,wrap);
    line(0,0,posX,posY);
    print(posX);
    println(posY);
    popMatrix();
  }

}

class Trees {
  float x;
  float y;

  Trees(){
    //x = random(0,);
  }

  void trash(){

  }
  void road(){
    fill(250,50);
    rectMode(CENTER);
    rect(width/2,height/2, width/2, height);
  }

  void show(){

  }
}

我只想知道它的算法,以及在效率和美学方面是否还有其他算法。 ^^

如果对象要绕原点旋转,那么旋转必须在平移之前完成。由于像 rotate() and translate() 这样的操作会设置一个矩阵并将当前矩阵乘以新矩阵,这意味着 rotate() 必须在绘制对象之前最后完成。即使在特定位置(例如 posXposY)绘制对象也像翻译一样。

你必须在位置 (0, 0) 画车。然后你必须旋转它。最后将它翻译到它的最终位置:

void display(){
    pushMatrix();
    translate(width/2,height/2);

    pushMatrix();
    translate(posX,posY);
    rotate(radians(angle));

    fill(255);
    stroke(255);
    imageMode(CENTER);
    image(c,0,0,wrap,wrap);
    popMatrix();

    line(0,0,posX,posY);
    popMatrix();
}

矩阵操纵运算,改变矩阵。该矩阵应用于绘图操作中的坐标。 update中的矩阵运算没有用,因为开始是一个pushMatrix() at the end a popMatrix(),但根本没有绘制。

Car.

的构造函数中每帧加载一次图像是一种性能浪费

看例子:

Car forza;
Trees tr;
float wrap = 100;

void setup(){
    size(800,800);
    forza = new Car();
    tr = new Trees();
}

void draw(){
    background(0);
    forza.update();
    forza.display();
    tr.road();
}

class Car {

    float posX;
    float posY;
    float speed;
    float accel;
    float angle;
    PImage c;

    Car(){
        posX = 0;
        posY = 0;
        speed = .9;
        angle = sin(0);
        accel = 0;
        c = loadImage("car.jpg");
    }

    void update(){
        if (keyPressed) {
          if (key == 'd') {
            angle += 1;
          }else if (key == 'a'){
            angle -= 1;
          } else if (key == 'w'){
            posX += speed;
          } else if (key == 's'){
            posX -= speed;
          }
        }  
    }

    void display(){
        pushMatrix();
        translate(width/2,height/2);

        pushMatrix();
        translate(posX,posY);
        rotate(radians(angle));

        fill(255);
        stroke(255);
        imageMode(CENTER);
        image(c,0,0,wrap,wrap);
        popMatrix();

        line(0,0,posX,posY);
        popMatrix();
    }
}

class Trees {
    float x;
    float y;

    Trees(){
      //x = random(0,);
    }

    void trash(){

    }
    void road(){
      fill(250,50);
      rectMode(CENTER);
      rect(width/2,height/2, width/2, height);
    }

    void show(){

    }
}