在扩展PVector的对象中使用它时,我遇到了在Processing中获得正确的rotate()功能的问题。下面的代码带有示例。蓝色矩形应围绕它的中心旋转(与其他人一样),但它围绕原点(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();
}
}
答案 0 :(得分:0)
由于类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继承的类中使用它。