【问题标题】:Processing: Simultaneous drawing of random particle trajectories处理:同时绘制随机粒子轨迹
【发布时间】:2015-01-24 07:41:11
【问题描述】:
class loc {
float[] x;
float[] y;
float v_o_x, v_o_y;

float[] locationx = new float[0];
float[] locationy = new float[0];

loc(float x_o, float y_o, float v_o, float theta, int t_end) {

theta = radians(theta);

v_o_x = v_o_x = v_o * cos(theta);

v_o_y = abs(v_o) * sin(theta);

for (int i=0; i<t_end; i++) {
  locationx = append(locationx, (v_o_x * i + x_o));
  locationy = append(locationy, (0.5*10*pow(i, 2) - v_o_y*i + y_o));
}

this.x = locationx;
this.y = locationy;
}
}

loc locations;

int wait = 75; // change delay between animation
int i = 0;
int j = 0;

float randV = random(-70, 70);
float randAng = random(30, 50);

int len = 17;

void setup() {

  size(1500, 800);

  background(255);
}

void draw() {


  fill(0);


  int d = 20; // diameter

  float[] xx, yy;

  if (i < len) {

    locations = new loc(width/2, height/3.5, randV, randAng, len);

    xx = locations.x;
    yy = locations.y;

    //background(255);
    rect(width/2-d, height/3.5+d, d*2, d*2);

    float s = 255/locations.x.length;
    fill((0+i*s));
    ellipse(xx[i], yy[i], d, d);

    i += 1;

    delay(wait);
  } else { 
    randV = random(-70, 70);
    randAng = random(30, 50);
    i = 0;
  }
}

我编写了一个简单的代码,它为随机初始角度和速度的球轨迹设置动画。当前运行时,它会发出一个球,等待它落地,然后再随机发出另一个球。我的希望是让它同时发出多个随机球,以创造一种喷泉效果。我很难做到这一点,有什么建议吗?

【问题讨论】:

    标签: processing


    【解决方案1】:

    现在,您有一些变量表示单个球的位置(和过去的位置)。为了这个问题,我暂时忽略您似乎从未使用过其中一些变量。

    您可以复制所有这些变量并为您想要的每个球重复它们。您将拥有 ballOneLocations、ballTwoLocations 等。

    但这太可怕了,所以你应该将所有这些变量包装到一个 Ball 类中。 Ball 的每个实例都将代表一个单独的球及其过去的位置。

    然后您需要做的就是创建一个数组或一个 Ball 实例的 ArrayList,然后循环遍历它们以更新和绘制它们。

    Here 是一个关于如何在 Processing 中使用 OOP 来创建多个在屏幕上弹跳的球的教程。

    【讨论】:

      【解决方案2】:

      同意 Kevin Workman 的观点,上课是去这里的方式。 这方面的最佳资源之一是Daniel Shiffman,尤其是他的书Nature of Code。您的问题将在粒子系统章节(第 4 章)中处理。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多