【问题标题】:Processing PVector rotations处理 Pvector 旋转
【发布时间】:2013-10-18 19:39:08
【问题描述】:

问题是我在中间的主 PVector 周围放置了一组 PVector。我希望我的 PVector 数组基于旋转变量围绕我的主 PVector 旋转。有没有办法做到这一点?

现在我有这段代码,但它不会旋转 PVector,只是根据旋转变量将它们放在更远的地方。

class Box {
  PVector location;
  PVector[] points;
  float rotation = random(360);
  Box() {
    location = new PVector(random(width), random(height));
    points = new PVector[4];

    for(a = 0; a < points.length; a ++) {
      points[a] = new PVector(0,0); 
    }
  }

  void update() {
    points[0].x = location.x + 10 * sin(rotation);
    points[0].y = location.y + 10 * sin(rotation);

    points[1].x = location.x + 10 * sin(rotation);
    points[1].y = location.y - 10 * sin(rotation);

    points[2].x = location.x - 10 * sin(rotation);
    points[2].y = location.y + 10 * sin(rotation);

    points[3].x = location.x - 10 * sin(rotation);
    points[3].y = location.y - 10 * sin(rotation);
}

【问题讨论】:

  • 您没有考虑点的原始坐标。这是故意的吗?

标签: rotation processing


【解决方案1】:

要旋转向量,您确实需要像在代码中那样使用像 sincos 这样的三角函数。但是,您的方法并不是最好的。在每次更新时添加到现有的 (x,y) 坐标实际上并不可行,因为您必须添加的数字每次都在变化。为每次更新覆盖和计算新值更容易。给定角度的xy 坐标由单位圆给出:

因此,给定PVectorxcos(theta) 而变化,ysin(theta) 而变化。检查以下代码:

Box b;

void setup(){
  size(300,300);
  b = new Box();
}
void draw(){
  background(255);
  b.update(mouseX, mouseY);
  b.display();
}

class Box {
  PVector location;
  PVector[] points;
  float rotation;
  float radius;
  Box() {
    location = new PVector(width/2,height/2);
    points = new PVector[7];
    rotation = 0;
    radius = 50;
    for(int i = 0; i < points.length; i ++) {
      //this centers the points around (0,0), so you need to add in
      //the box coordinates later on.
      points[i] = new PVector(radius*cos(rotation + i*TWO_PI/points.length),
                              radius*sin(rotation + i*TWO_PI/points.length)); 
    }
  }
  void update(int x, int y) {
    location.set(x,y);
    rotation += 0.08; // change for different rotation speeds.
    for(int i = 0; i < points.length; i++){
      points[i].set(radius*cos(rotation + i*TWO_PI/points.length),
                    radius*sin(rotation + i*TWO_PI/points.length)); 
    }
  }
  void display(){
    stroke(0);
    for(int i = 0; i < points.length; i++){
      //points are treated as offsets from the center point:
      line(location.x,location.y,location.x+points[i].x,location.y+points[i].y);
      ellipse(location.x+points[i].x,location.y+points[i].y,10,10);
    }
  }
}

对于每个update() 调用,它会递增rotation 变量并为数组中的每个点计算新的xy 值。您可以通过将0.08 更改为更大/更小/正/负数来更改旋转速度和方向。

【讨论】:

  • 遇到了一个问题,即使我移动了位置,点 x 和 y 似乎也不会改变。
  • 你如何移动位置?另外,您是否记得在移动后重新显示它?如果值得提出一个新问题,请将其标记为已解决,因为这些 cmets 不应该用于跟进。
  • 对不起,我不知道你不能再问一个问题。但无论哪种方式,我都发现了问题
【解决方案2】:

围绕位置旋转一个点:

double x = cos(rotation) * (point.x-location.x) - sin(rotation) * (point.y-location.y) + location.x;
double y = sin(rotation) * (point.x-location.x) + cos(rotation) * (point.y-location.y) + location.y;
point.x = x;
point.y = y;

Rotate a point by an angle

【讨论】:

    猜你喜欢
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多