Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
49 additions
and
0 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
16-physics/vel_random_walk_array/vel_random_walk_array.pde
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// 速度によるランダムウォーク(たくさんのエージェント) | ||
|
||
// エージェントの位置 | ||
float[] x; | ||
float[] y; | ||
// エージェントの速度 | ||
float[] vx; | ||
float[] vy; | ||
|
||
void setup() { | ||
size(600, 600); | ||
noStroke(); | ||
fill(255, 100); | ||
background(0); | ||
|
||
x = new float[100]; | ||
y = new float[100]; | ||
vx = new float[100]; | ||
vy = new float[100]; | ||
|
||
for (int i = 0; i < 100; i++) { | ||
// 初期位置 | ||
x[i] = width / 2; | ||
y[i] = height / 2; | ||
|
||
// 初速 | ||
vx[i] = 0; | ||
vy[i] = 0; | ||
} | ||
} | ||
|
||
void draw() { | ||
for (int i = 0; i < 100; i++) { | ||
// 速度を位置に反映 | ||
x[i] += vx[i]; | ||
y[i] += vy[i]; | ||
|
||
// 速度をランダムに変化させる | ||
vx[i] += random(-0.5, +0.5) * 2 / 4; | ||
vy[i] += random(-0.5, +0.5) * 2 / 4; | ||
|
||
// 簡易的な空気抵抗を加える | ||
vx[i] *= 0.99; | ||
vy[i] *= 0.99; | ||
|
||
// エージェントを描く | ||
ellipse(x[i], y[i], 2, 2); | ||
} | ||
} |