元Webデザイナー兼コーダーの備忘録

ウェブデザインやプログラミング、ブログのカスタマイズなどについてアウトプットしています。

 メニュー

» HTML入門のまとめはこちらです。

Processing:万有引力

The Nature of Code(PDF版)から万有引力について取り上げます。PVectorを用いて、万有引力について学びます。Processingでプログラムを書いて、動作を確認します。動作を確認できるところがProcessingの楽しいところです。

万有引力を示す式

万有引力を示す式は、以下です。

F = G{\frac{m_{1}m_{2}}{r^2}}

この式をベクトルを使い、以下のように表します。

F = G{\frac{m_{1}m_{2}}{r^2}}\hat{r}

  • \hat{r}:m1からm2を指す単位ベクトル

この式を向きと大きさに分解して考えます。

  • 向き
    • \hat{r}:m1からm2を指す単位ベクトル
  • 大きさ

万有引力を適用する例

以下は、万有引力を適用したプログラムの参考例です。

class Mover {
  PVector location;
  PVector velocity;
  PVector acceleration;
  float mass;  //質量

  //コンストラクタ
  Mover() {
    location = new PVector(200,50);
    velocity = new PVector(1,0);
    acceleration = new PVector(0,0);
    mass = 1;
  }

  //力を足し合わせる(力の蓄積)
  void applyForce(PVector force) {
    PVector f = PVector.div(force,mass);
    acceleration.add(f);
  }

  //ボールの位置の更新
  void update() {
    velocity.add(acceleration);
    location.add(velocity);
    acceleration.mult(0);  //力の蓄積をリセットする
  }

  //ボールの描画
  void display() {
    stroke(0);
    strokeWeight(2);
    fill(0, 127);
    ellipse(location.x,location.y,mass*16,mass*16);
  }
}
class Attractor{
  float mass;
  float G;  //引力の定数
  PVector location;

  //コンストラクタ
  Attractor(){
    location = new PVector(width/2, height/2);
    mass = 20;
    G = 0.4;
  }

  //引力を計算して返す
  PVector attract(Mover m){
    PVector force = PVector.sub(location, m.location);  //2点間のベクトル
    float distance = force.mag();  //2点間の距離
    distance = constrain(distance, 5.0, 25.0);  //距離の制限
    force.normalize();  //力の方向の取得
    float strength = (G * mass * m.mass) / (distance * distance);
    force.mult(strength);

    return force;
  }

  // アトラクタの描画
  void display(){
    stroke(0);
    fill(175, 200);
    ellipse(location.x, location.y, mass*2, mass*2);
  }
}
//Attraction
Mover mover;
Attractor attractor;

void setup() {
  size(400,200);
  mover = new Mover();
  attractor = new Attractor();
}

void draw() {
  background(255);

  PVector force = attractor.attract(mover);  //引力
  mover.applyForce(force);
  mover.update();

  attractor.display();
  mover.display();
}

静止画だと分かりにくいので、プログラムを実行して確認することをおすすめします。

プログラムの解説

Attractorクラスを作ります。Attractorクラスは、引力を計算するメソッドを持ちます。

//引力を計算して返す
PVector attract(Mover m){
  PVector force = PVector.sub(location, m.location);  //2点間のベクトル
  float distance = force.mag();  //2点間の距離
  distance = constrain(distance, 5.0, 25.0);  //距離の制限
  force.normalize();  //力の方向の取得
  float strength = (G * mass * m.mass) / (distance * distance);
  force.mult(strength);

  return force;
}

draw()メソッド内で、物体に引力を適用します。

void draw() {
  ...
  PVector force = attractor.attract(mover);  //引力
  mover.applyForce(force);
  ...
}

メモ

  • constrain(制限する値, 最小値, 最大値);
  • 値が最小値と最大値を超えないようにする
void draw(){
  background(204);
  //mouseXの値は30〜70をとる
  float mx = constrain(mouseX, 30, 70);
  rect(mx-10, 40, 20, 20);
  println(mx);
}

まとめ

The Nature of Code(PDF版)から万有引力について取り上げました。今回は、PVectorを用いて万有引力について学びました。引き続き、The Nature of Code(PDF版)の内容を勉強します。

参考サイト

» HTML入門のまとめはこちらです。