回答編集履歴
2
サンプル追加
answer
CHANGED
@@ -56,4 +56,157 @@
|
|
56
56
|
}
|
57
57
|
}
|
58
58
|
}
|
59
|
-
```
|
59
|
+
```
|
60
|
+
|
61
|
+
---
|
62
|
+
5/27 追記
|
63
|
+
```Processing
|
64
|
+
//https://teratail.com/questions/264746
|
65
|
+
class CircleInfo {//単体円
|
66
|
+
float x, y, dia;
|
67
|
+
color col;
|
68
|
+
CircleInfo(float _x, float _y, float _dia, color _col) {
|
69
|
+
x=_x;
|
70
|
+
y=_y;
|
71
|
+
dia=_dia;
|
72
|
+
col=_col;
|
73
|
+
}
|
74
|
+
}
|
75
|
+
|
76
|
+
class Colony {//円の群体
|
77
|
+
float x, y;
|
78
|
+
PVector v;
|
79
|
+
ArrayList<CircleInfo> cell;//個々の円
|
80
|
+
|
81
|
+
Colony(float _x, float _y, PVector _v, CircleInfo c) {
|
82
|
+
x=_x;
|
83
|
+
y=_y;
|
84
|
+
v=_v;
|
85
|
+
cell=new ArrayList<CircleInfo>();
|
86
|
+
cell.add(c);
|
87
|
+
}
|
88
|
+
void addCell( CircleInfo c ) {//円の位置は群体の基準座標による
|
89
|
+
cell.add(c);
|
90
|
+
}
|
91
|
+
void addCellAtPos( CircleInfo c) {//円の位置はスクリーン座標による
|
92
|
+
cell.add(new CircleInfo(c.x-x, c.y-y, c.dia, c.col));
|
93
|
+
}
|
94
|
+
CircleInfo[] getCell() {//含まれている円のリストを提示(合体用)
|
95
|
+
ArrayList<CircleInfo> cellAtPos=new ArrayList<CircleInfo>();
|
96
|
+
for ( CircleInfo c : cell) {
|
97
|
+
cellAtPos.add(new CircleInfo(x+c.x, y+c.y, c.dia, c.col));
|
98
|
+
}
|
99
|
+
return (CircleInfo[])cellAtPos.toArray(new CircleInfo[0]);
|
100
|
+
}
|
101
|
+
void del() {
|
102
|
+
cell.clear();
|
103
|
+
}
|
104
|
+
void render() {
|
105
|
+
for ( CircleInfo c : cell) {
|
106
|
+
if (c!=null) {
|
107
|
+
fill(c.col);
|
108
|
+
circle(x+c.x, y+c.y, c.dia);
|
109
|
+
}
|
110
|
+
}
|
111
|
+
}
|
112
|
+
void update() {
|
113
|
+
x+=v.x;
|
114
|
+
y+=v.y;
|
115
|
+
//壁の跳ね返りはここで処理
|
116
|
+
//一群の最小/最大のx,y位置を求めて壁との衝突を判定
|
117
|
+
float minx=2*width, miny=2*height, maxx=-width, maxy=-height;
|
118
|
+
for (CircleInfo c : cell) {
|
119
|
+
if (c!=null) {
|
120
|
+
if (minx>x+c.x-c.dia/2) {
|
121
|
+
minx=x+c.x-c.dia/2;
|
122
|
+
}
|
123
|
+
if (maxx<x+c.x+c.dia/2) {
|
124
|
+
maxx=x+c.x+c.dia/2;
|
125
|
+
}
|
126
|
+
if (miny>y+c.y-c.dia/2) {
|
127
|
+
miny=y+c.y-c.dia/2;
|
128
|
+
}
|
129
|
+
if (maxy<y+c.y+c.dia/2) {
|
130
|
+
maxy=y+c.y+c.dia/2;
|
131
|
+
}
|
132
|
+
}
|
133
|
+
}
|
134
|
+
if (minx<=0) {
|
135
|
+
x+=-minx;
|
136
|
+
v.x=-v.x;
|
137
|
+
}
|
138
|
+
if (maxx>=width) {
|
139
|
+
x-=maxx-width;
|
140
|
+
v.x=-v.x;
|
141
|
+
}
|
142
|
+
if (miny<=0) {
|
143
|
+
y+=-miny;
|
144
|
+
v.y=-v.y;
|
145
|
+
}
|
146
|
+
if (maxy>=height) {
|
147
|
+
y-=maxy-height;
|
148
|
+
v.y=-v.y;
|
149
|
+
}
|
150
|
+
}
|
151
|
+
}
|
152
|
+
|
153
|
+
boolean collision(Colony c1, Colony c2) {
|
154
|
+
boolean ret=false;
|
155
|
+
found:
|
156
|
+
for (CircleInfo cinfo1 : c1.getCell()) {
|
157
|
+
for (CircleInfo cinfo2 : c2.getCell()) {
|
158
|
+
if (dist(cinfo1.x, cinfo1.y, cinfo2.x, cinfo2.y)<(cinfo1.dia+cinfo2.dia)/2) {
|
159
|
+
ret=true;
|
160
|
+
break found;
|
161
|
+
}
|
162
|
+
}
|
163
|
+
}
|
164
|
+
return ret;
|
165
|
+
}
|
166
|
+
|
167
|
+
void combine(Colony c1, Colony c2) {
|
168
|
+
for (CircleInfo c : c2.getCell()) {
|
169
|
+
c1.addCellAtPos(c);
|
170
|
+
}
|
171
|
+
//合体後速度は質量比とかいろいろ考えられるけどとりあえず平均で。
|
172
|
+
c1.v.x=(c1.v.x+c2.v.x)/2;
|
173
|
+
c1.v.y=(c1.v.y+c2.v.y)/2;
|
174
|
+
c2.del();
|
175
|
+
}
|
176
|
+
|
177
|
+
ArrayList<Colony> cl;
|
178
|
+
|
179
|
+
void setup() {
|
180
|
+
size(500, 500);
|
181
|
+
colorMode(HSB);
|
182
|
+
cl=new ArrayList<Colony>();
|
183
|
+
for (int i=0; i<3; i++) {
|
184
|
+
cl.add(
|
185
|
+
new Colony( random(50, width-50), random(50, height-50),
|
186
|
+
new PVector(random(-5, 5), random(-5, 5)),
|
187
|
+
new CircleInfo(0, 0, random(40, 60), color(random(256), 128, 255))
|
188
|
+
));
|
189
|
+
}
|
190
|
+
noStroke();
|
191
|
+
}
|
192
|
+
void draw() {
|
193
|
+
background(255);
|
194
|
+
//移動/描画
|
195
|
+
for (Colony c : cl) {
|
196
|
+
c.update();
|
197
|
+
c.render();
|
198
|
+
}
|
199
|
+
//衝突検出/合体
|
200
|
+
for (int i=0; i<cl.size(); i++) {
|
201
|
+
for (int j=i+1; j<cl.size(); j++) {
|
202
|
+
if (collision(cl.get(i), cl.get(j))) {
|
203
|
+
combine(cl.get(i), cl.get(j));
|
204
|
+
}
|
205
|
+
}
|
206
|
+
}
|
207
|
+
}
|
208
|
+
```
|
209
|
+
BouncingBallを使って書けないかというのもちょっと考えてみたけど
|
210
|
+
> 合体している2つの球のうち、壁に近いほうが壁にぶつかると2球とも運動の方向が変わること。
|
211
|
+
|
212
|
+
が結構面倒で断念。一緒に動いている2球(以上)が同時に壁にぶつかったときどうしようかな...というのが。
|
1
追記
answer
CHANGED
@@ -7,4 +7,53 @@
|
|
7
7
|
|
8
8
|
|
9
9
|
どうでもいい小ネタですけど、色をランダムにするときはcolorMode(HSB)として、
|
10
|
-
Hだけを乱数、SとBを255にすると小汚い色にならず、ビビッドカラーが楽しめます。あるいは、Sを128にするとちょっと淡くて柔らかい感じの色とか。
|
10
|
+
Hだけを乱数、SとBを255にすると小汚い色にならず、ビビッドカラーが楽しめます。あるいは、Sを128にするとちょっと淡くて柔らかい感じの色とか。
|
11
|
+
|
12
|
+
---
|
13
|
+
5/26 13:13の返信③について、現状のプログラムであってもPVectorを使うのであればBouncingBallクラスはこんなふうにすべきかと思います。(もちろん、ここを変えれば他も影響をうけます)
|
14
|
+
```Processing
|
15
|
+
class BouncingBall {
|
16
|
+
PVector velocity;
|
17
|
+
PVector location;
|
18
|
+
float r;
|
19
|
+
color ballColour;
|
20
|
+
|
21
|
+
BouncingBall(PVector loc, PVector velo, float rad, color col) {
|
22
|
+
velocity=velo;
|
23
|
+
location=loc;
|
24
|
+
ballColour = col;
|
25
|
+
r=rad;
|
26
|
+
}
|
27
|
+
|
28
|
+
void render() {
|
29
|
+
//renderという名前のメソッドにrendering以外の仕事をさせてはいけない
|
30
|
+
noStroke();
|
31
|
+
fill(ballColour);
|
32
|
+
ellipse(location.x, location.y, r*2, r*2);
|
33
|
+
}
|
34
|
+
|
35
|
+
void update() {
|
36
|
+
location.x += velocity.x;
|
37
|
+
location.y += velocity.y;
|
38
|
+
|
39
|
+
if (location.x - r <= 0 || location.x + r >= width) {
|
40
|
+
velocity.x=-velocity.x;
|
41
|
+
if (location.x - r <= 0) {
|
42
|
+
location.x = r+1;
|
43
|
+
}
|
44
|
+
if (location.x + r >= width-1) {
|
45
|
+
location.x = width-r-1;
|
46
|
+
}
|
47
|
+
}
|
48
|
+
if (location.y - r <= 0 || location.y + r >= height) {
|
49
|
+
velocity.y=-velocity.y;
|
50
|
+
if (location.y - r <= 0) {
|
51
|
+
location.y = r+1;
|
52
|
+
}
|
53
|
+
if (location.y + r >= height-1) {
|
54
|
+
location.y = height-r-1;
|
55
|
+
}
|
56
|
+
}
|
57
|
+
}
|
58
|
+
}
|
59
|
+
```
|