【问题标题】:Rotation in Processing for objects that inherit from PVector从 PVector 继承的对象的处理中的旋转
【发布时间】:2023-03-15 04:40:01
【问题描述】:

在扩展 PVector 的对象中使用时,我在处理中获得正确的旋转()功能时遇到问题。下面的代码与示例。蓝色矩形应该围绕它的中心旋转(就像其他人一样)但它围绕原点(0,0)旋转 - 为什么?

// coordinates rect 1
float xc;
float yc;

// coordinates rect 2
PVector position;

// rect 3
Shape shape;

// rect 4
ShapeInheritance shapeInheritance;

void setup() {
  size(200,200);

  xc = width *0.25f;
  yc = height * 0.5f;

  position = new PVector(width *0.75f, height*0.5f);

  shape = new Shape(width*0.5f, height*0.75f);

  shapeInheritance = new ShapeInheritance(width*0.5f, height*0.25f);

  rectMode(CENTER);
  noFill();
}

float theta = 0;

void draw() {
  theta +=0.01f;
  background(255);

  //rectangle 1 (black)
  pushMatrix();
  translate(xc,yc);
  rotate(theta);
  stroke(0);
  rect(0,0,50,50);
  popMatrix();

  //rectangle 2 (red)
  pushMatrix();
  translate(position.x, position.y);
  rotate(theta);
  stroke(255,0,0);
  rect(0,0,50,50);
  popMatrix();

  // rectangle 3 (green)
  shape.display();

  // rectangle 4 (blue)
  shapeInheritance.display();
}

class Shape {
  float shapeTheta = 0;
  PVector pos;

  Shape(float x_, float y_) {
    pos = new PVector(x_, y_);
  }

  void display() {
    shapeTheta += 0.01f;
    pushMatrix();
    translate(pos.x, pos.y);
    rotate(shapeTheta);
    stroke(0,255,0);
    rect(0,0,50,50);
    popMatrix();
  }
}

class ShapeInheritance extends PVector {
  float shapeTheta = 0;

  ShapeInheritance(float x_, float y_) {
    super(x_, y_);

  }

  void display() {
    shapeTheta += 0.01f;
    pushMatrix();
    translate(this.x, this.y);
    rotate(shapeTheta);
    stroke(0,0,255);
    rect(0,0,50,50);
    popMatrix();
  }
}

【问题讨论】:

    标签: inheritance rotation processing


    【解决方案1】:

    因为 PVector 类的 rotate() 不同,请参阅 link

    v = new PVector(10.0, 20.0);
    println(v);  // Prints "[ 10.0, 20.0, 0.0 ]"
    v.rotate(HALF_PI);
    println(v);  // Prints "[ -20.0, 9.999999, 0.0 ]"
    

    所以你改变的是向量而不是投影矩阵!您应该在继承自 PVector 的类之外使用它。

    【讨论】:

    • 感谢您指出这一点!所以现在的解决方案是在 ShapeInheritance 中保留对 PApplet 的引用并调用 applet.rotate() 而不是 rotate()。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-13
    • 2023-03-16
    • 2019-11-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多