在加工过程中沿贝塞尔曲线移动

时间:2015-12-02 04:13:03

标签: processing

the ball moving in a Bezier Curve from start to the middle of the curve的代码是:

     void ballMove()
    {

      if(y[0]==height*1/10)
      {

        bezier (x[0], y[0],x[1], y[1], x[2], y[2], x[3], y[3]);
      float x0; float x1; float x2; float x3; 
    float y0; float y1; float y2; float y3;

    x0 = x[0]; x1 = x[1]; x2 = x[2]; x3 = x[3]; 
    y0 = y[0]; y1 = y[1]; y2 = y[2]; y3 = y[3];


     float t =  (frameCount/100.0)%1;
      float x = bezierPoint(x0, x1, x2, x3, t);
      float y = bezierPoint( y0, y1, y2, y3, t);

       if(t>=0.5)
      {
        t=0;
      }

      while(t==0.5)
     {
       a=x;
       b=y;
     }
      while(t>0.5)
      {
        ellipse(a,b,30,30);
      }
      fill(255,0,0);
      if(t!=0)
      {
      ellipse(x, y, 15, 15);
      }
      }
    }

我已经在设置,绘制等方面定义了所有内容,但是我想在按下空间时从Bezier曲线的开始到中间发射球只有一次。

当前版本向我展示了循环。我怎样才能做到这一点?

尝试一切像返回,中断,更改t参数等,但代码不起作用。我是处理新手。

你有什么建议吗?

1 个答案:

答案 0 :(得分:0)

您计算的最大错误是在计算红球的tx位置后改变y的值。为了避免这种情况,您需要首先在[0,0]之间的[0,1]中计算t,然后根据程序的状态更改此值。

您从t计算frameCount时犯的第二个错误。首先使用模数提取数字[0,50],然后将其映射到范围[0,0.5]中,如下所示

float t =  (frameCount % 50) * 0.01;

您还提到要在按某个键后重复此动画。为此,您需要keyPressed方法和一些全局变量来表示程序状态并存储动画的起始帧(因为frameCount应该是只读的)。所以基本功能可以这样实现:

boolean run = false;
float f_start = 0;

void ballMove() {
  noFill();
  bezier (x0, y0, x1, y1, x2, y2, x3, y3);

  float t =  ((frameCount - f_start) % 50) * 0.01;

  if (run == false) {
    t = 0;
  }
  float x = bezierPoint(x0, x1, x2, x3, t);
  float y = bezierPoint( y0, y1, y2, y3, t);

  fill(255, 0, 0);
  ellipse(x, y, 5, 5);
}

void keyPressed() {
  run = !run;
  f_start = frameCount;
}

希望这会对你有所帮助。下次请发布MCVE,以便我们不需要与您的代码进行斗争。