【问题标题】:Processing: How do I make an object move in a circular path?处理:如何使对象在圆形路径中移动?
【发布时间】:2016-01-17 19:14:53
【问题描述】:

我创建了一个类,在其中为我的程序定义 Shape 对象。这些形状中的每一个都有一个围绕它绘制的透明椭圆(我在构造函数中定义了它),如果任何其他形状移动到那个圆形椭圆区域,我希望那个形状改变它的方向,以便它在圆形路径中移动。

每个 Shape 对象都有一个定义的半径属性(因为我在每个对象周围绘制了椭圆),我想使用该值来确定 Shape 在碰撞时必须移入的圆形图案有多大。

请帮忙!任何事情都非常感谢!

编辑:

正如我上面所说,我希望形状移动到圆形路径中。但是,我希望它只在一个圆形路径中移动一次(意味着它围绕一个圆圈移动一次),然后我希望它继续在它编程的原始路径上。

【问题讨论】:

    标签: object geometry processing trigonometry calculus


    【解决方案1】:

    简短的回答是,您必须使用基本三角函数来计算点之间的角度,然后使用更基本的三角函数来确定圆形路径上的后续点。

    查看the Processing reference三角函数部分了解更多信息。

    但基本上,如果你有两个点,你可以使用atan2() 函数来计算它们之间的角度。您可以使用它来找到从圆心到形状的起始角度。

    获得该角度后,您可以简单地增加它,然后使用cos()sin() 计算出该新角度处的xy 坐标。

    这是完成上述所有操作的基本草图:

    PVector center;
    float angle;
    float radius;
    
    void setup() {
      size(500, 500);
      center = new PVector(width/2, height/2);
    
      //get the initial point
      //for you, this would be the initial location of the object
      PVector point = new PVector(random(width), random(height));
    
      //find the angle between the points
      float deltaX = center.x - point.x;
      float deltaY = center.y - point.y;
      angle = atan2(deltaX, deltaY);
    
      //find the radius of the circle
      radius = dist(center.x, center.y, point.x, point.y);
    
      ellipseMode(RADIUS);
    }
    
    void draw() {
      background(0);
    
      //draw the center point
      ellipse(center.x, center.y, 10, 10);
    
      //find the point based on the angle
      float x = center.x + cos(angle)*radius;
      float y = center.y + sin(angle)*radius;
    
      //draw the traveling point
      ellipse(x, y, 10, 10);
    
      //increment the angle to move the point
      angle += PI/120;
    }
    

    【讨论】:

      【解决方案2】:

      好吧,在我看到凯文的帖子之前,我也做了一个。不使用对象,只是一个简单的程序示例。还是发帖吧:)

      PVector pos, speed, stored;
      float diam = 40;
      boolean wonder = false;
      float angle = 0;
      
      
      void setup() {
        size(300, 300);
      
        // arbitrary positioning and speeding
        pos = new PVector(-20, height/2);
        speed = new PVector(1, 0);
        noStroke();
      }
      
      
      void draw() {
        background(5);
      
        // normally increment speed
        if (!wonder) {
          pos.add(speed);
        } else {
      
          // if is to wonder...
          if (angle <= 360) {
      
            //get circle path by trig
            pos.x = stored.x + cos(radians(angle))*diam;
            pos.y = stored.y + sin(radians(angle))*diam;
          } else {
      
            // if the circle is complete
            // reset angle and stop wondering
            wonder = false;
            angle  = 0;
          }
      
          // increment angle
          angle++;
        }
      
        // draw
        ellipse(pos.x, pos.y, diam, diam);
      }
      
      
      void mouseClicked() {
        if (isOverCircle() ) {
      
          // store position where it has being clicked
          stored = pos.get();
      
          // off set the diam
          stored.x -= diam;
      
          // trig wondering
          wonder = true;
          angle = 0;
        }
      }
      
      boolean isOverCircle() {
        float disX = pos.x - mouseX;
        float disY = pos.y - mouseY;
        return sqrt(sq(disX) + sq(disY)) < diam/2;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-07-28
        • 1970-01-01
        • 2017-02-21
        • 1970-01-01
        • 1970-01-01
        • 2011-08-26
        相关资源
        最近更新 更多