【问题标题】:Moving along Bezier Curve in processing在加工中沿贝塞尔曲线移动
【发布时间】:2015-12-03 18:48:28
【问题描述】:

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

我已经在设置、绘制等中定义了所有内容,但我只想在按下空格时将球从贝塞尔曲线的起点发射到中间一次。

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

尝试了所有方法,例如返回、中断、更改 t 参数等,但代码不起作用。我是新来的处理。

你有什么建议吗?

【问题讨论】:

  • 你能发一个MCVE而不是一个断开连接的方法吗?

标签: processing


【解决方案1】:

您犯的最大错误是在计算了红球的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,这样我们就不需要和你的代码打架了。

【讨论】:

    猜你喜欢
    • 2011-12-06
    • 2011-11-22
    • 1970-01-01
    • 1970-01-01
    • 2013-01-18
    • 1970-01-01
    • 2017-06-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多