日常の進捗

主に自分のための,行為とその習慣化の記録

クラスをつかったボールのアニメーションの発展(重力・摩擦・3D)

昨日の続き。

takawo.hatenablog.com

重力

int num = 100;
Ball[] balls = new Ball[num];
void setup() {
  size(960, 540);
  colorMode(HSB, 360, 100, 100);
  for (int i = 0; i < num; i = i+1) {
    balls[i] = new Ball();
  }
  background(0, 0, 0);
}

void draw() {
  fill(0, 0, 0, 30);
  rect(0, 0, width, height);
  for (int i = 0; i < num; i = i+1) {
    balls[i].update();
    balls[i].display();
  }
}

void mousePressed() {
  for (int i = 0; i < num; i = i+1) {
    balls[i].reset();
  }
}



class Ball {
  float x; //ボールのx座標
  float y; //ボールのx座標
  float xSpeed; //ボールのx方向の速度
  float ySpeed; //ボールのy方向の速度
  float gravity = 0.1;
  color c; //ボールの色
  float d; //ボールの大きさ

  Ball() {
    reset();
  }
  
  void reset() {
    x = random(width);
    y = random(height);
    xSpeed = random(-3, 3);
    ySpeed = random(-3, 3);
    c = color(random(360), 80, 100, 100);
    d = random(10, 30);
  }

  void update() {
    x = x + xSpeed;
    y = y + ySpeed;
    ySpeed += gravity;

    if (x > width) {
      xSpeed *= -1;
    }
    if (x < 0) {
      xSpeed *= -1;
    }
    if (y > height) {
      ySpeed *= -1;
    }
    if (y < 0) {
      ySpeed *= -1;
    }
  }

  void display() {
    fill(c);
    noStroke();
    ellipse(x, y, d, d);
  }
}

摩擦

int num = 100;
Ball[] balls = new Ball[num];
void setup() {
  size(960, 540);
  colorMode(HSB, 360, 100, 100);
  for (int i = 0; i < num; i = i+1) {
    balls[i] = new Ball();
  }
  background(0, 0, 0);
}

void draw() {
  fill(0, 0, 0, 30);
  rect(0, 0, width, height);
  for (int i = 0; i < num; i = i+1) {
    balls[i].update();
    balls[i].display();
  }
}

void mousePressed() {
  for (int i = 0; i < num; i = i+1) {
    balls[i].reset();
  }
}


class Ball {
  float x; //ボールのx座標
  float y; //ボールのx座標
  float xSpeed; //ボールのx方向の速度
  float ySpeed; //ボールのy方向の速度
  float gravity = 0.1;
  float friction = 0.9;
  color c; //ボールの色
  float d; //ボールの大きさ

  Ball() {
    reset();
  }

  void reset() {
    x = random(width);
    y = random(height);
    xSpeed = random(-3, 3);
    ySpeed = random(-3, 3);
    c = color(random(360), 80, 100, 100);
    d = random(10, 30);
  }

  void update() {
    x = x + xSpeed;
    y = y + ySpeed;
    ySpeed += gravity;

    if (x > width) {
      xSpeed *= -1;
      xSpeed *= friction;
    }
    if (x < 0) {
      xSpeed *= -1;
      xSpeed *= friction;
    }
    if (y > height) {
      ySpeed *= -1;
      ySpeed *= friction;
    }
    if (y < 0) {
      ySpeed *= -1;
      ySpeed *= friction;
    }
  }

  void display() {
    fill(c);
    noStroke();
    ellipse(x, y, d, d);
  }
}

3D

int num = 100;
Ball[] balls = new Ball[num];
void setup() {
  size(960, 540, WEBGL);
  colorMode(HSB, 360, 100, 100);
  for (int i = 0; i < num; i = i+1) {
    balls[i] = new Ball(500);
  }
  background(0, 0, 0);
  sphereDetail(8);
}

void draw() {
  background(0, 0, 0);

  translate(width/2, height/2, -500);
  rotateX(-PI/12);
  rotateY(frameCount*0.003);
  stroke(0, 0, 100);
  noFill();
  box(500);
  for (int i = 0; i < num; i = i+1) {
    balls[i].update();
    balls[i].display();
  }
}

void mousePressed() {
  for (int i = 0; i < num; i = i+1) {
    balls[i].reset();
  }
}


class Ball {
  float x; //ボールのx座標
  float y; //ボールのx座標
  float z; //ボールのx座標
  float xSpeed; //ボールのx方向の速度
  float ySpeed; //ボールのy方向の速度
  float zSpeed; //ボールのy方向の速度
  float gravity = 0.1;
  float friction = 0.9;
  color c; //ボールの色
  float d; //ボールの大きさ

  float box;

  Ball(float boxSize) {
    reset();
    box = boxSize;
  }

  void reset() {
    x = random(-box/2, box/2);
    y = random(-box/2, box/2);
    z = random(-box/2, box/2);
    xSpeed = random(-3, 3);
    ySpeed = random(-3, 3);
    zSpeed = random(-3, 3);
    c = color(random(360), 80, 100, 100);
    d = random(10, 30);
  }

  void update() {
    x = x + xSpeed;
    y = y + ySpeed;
    z = z + zSpeed;
    ySpeed += gravity;

    if (x > box/2) {
      xSpeed *= -1;
      xSpeed *= friction;
    }
    if (x < -box/2) {
      xSpeed *= -1;
      xSpeed *= friction;
    }
    if (y > box/2) {
      ySpeed *= -1;
      ySpeed *= friction;
    }
    if (y < -box/2) {
      ySpeed *= -1;
      ySpeed *= friction;
    }
    if (z > box/2) {
      zSpeed *= -1;
      zSpeed *= friction;
    }
    if (z < -box/2) {
      zSpeed *= -1;
      zSpeed *= friction;
    }
  }

  void display() {
    pushMatrix();
    translate(x, y, z);
    fill(c);
    noStroke();
    sphere(d);
    popMatrix();
  }
}