記録

勉強したことに関するのノート。見返して分かるようにしてます。

PVector理解3

int num=1000;
PVector[] pos=new PVector[num];
PVector[] vel=new PVector[num];
color[] c=new color[num];
int[] r=new int[num];


void setup() {
  size(960, 540);
  colorMode(HSB, 360, 100, 100);

  for (int i=0; i<num; i=i+1) {
    pos[i]=new PVector(random(width), random(height));
    vel[i]=new PVector(random(-3, 3), random(-3, 3));
    c[i]=color(random(360), random(50, 100), random(50, 100));
    r[i]=int(random(2, 10));
  }
}
void draw() {
  fill(0, 0, 0, 50);
  rect(0, 0, width, height);
  for (int i=0; i<num; i=i+1) {
    fill(c[i]);
    ellipse(pos[i].x, pos[i].y, r[i]*2, r[i]*2);
    pos[i].add(vel[i]);

    if (pos[i].x>=width-r[i]||pos[i].x<=0+r[i]) {
      vel[i].x=vel[i].x*-1;
    }
    if (pos[i].y>=height-r[i]||pos[i].y<=0+r[i]) {
      vel[i].y=vel[i].y*-1;
    }
  }
}

ボールが1000個に増えて、色もバラバラで、サイズもバラバラ。飛ぶ方向も(さっき書き換えて)バラバラにしました。軌跡も少し見えるよ