在处理过程中沿贝塞尔曲线移动



我的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);
      }
      }
    }

我已经定义了设置、绘制等方面的一切,但每当按下空格时,我只想从贝塞尔曲线的开始到中间发射一次球。

当前版本显示了循环。我该怎么做?

尝试了return、break、更改t参数等所有操作,但代码不起作用。我刚开始处理。

你有什么建议吗?

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

您在从frameCount计算t时犯的第二个错误。首先,你使用模来提取数字[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,这样我们就不需要与您的代码斗争了。

最新更新