回答編集履歴
2
サンプル追加
test
CHANGED
@@ -115,3 +115,309 @@
|
|
115
115
|
}
|
116
116
|
|
117
117
|
```
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
---
|
122
|
+
|
123
|
+
5/27 追記
|
124
|
+
|
125
|
+
```Processing
|
126
|
+
|
127
|
+
//https://teratail.com/questions/264746
|
128
|
+
|
129
|
+
class CircleInfo {//単体円
|
130
|
+
|
131
|
+
float x, y, dia;
|
132
|
+
|
133
|
+
color col;
|
134
|
+
|
135
|
+
CircleInfo(float _x, float _y, float _dia, color _col) {
|
136
|
+
|
137
|
+
x=_x;
|
138
|
+
|
139
|
+
y=_y;
|
140
|
+
|
141
|
+
dia=_dia;
|
142
|
+
|
143
|
+
col=_col;
|
144
|
+
|
145
|
+
}
|
146
|
+
|
147
|
+
}
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
class Colony {//円の群体
|
152
|
+
|
153
|
+
float x, y;
|
154
|
+
|
155
|
+
PVector v;
|
156
|
+
|
157
|
+
ArrayList<CircleInfo> cell;//個々の円
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
Colony(float _x, float _y, PVector _v, CircleInfo c) {
|
162
|
+
|
163
|
+
x=_x;
|
164
|
+
|
165
|
+
y=_y;
|
166
|
+
|
167
|
+
v=_v;
|
168
|
+
|
169
|
+
cell=new ArrayList<CircleInfo>();
|
170
|
+
|
171
|
+
cell.add(c);
|
172
|
+
|
173
|
+
}
|
174
|
+
|
175
|
+
void addCell( CircleInfo c ) {//円の位置は群体の基準座標による
|
176
|
+
|
177
|
+
cell.add(c);
|
178
|
+
|
179
|
+
}
|
180
|
+
|
181
|
+
void addCellAtPos( CircleInfo c) {//円の位置はスクリーン座標による
|
182
|
+
|
183
|
+
cell.add(new CircleInfo(c.x-x, c.y-y, c.dia, c.col));
|
184
|
+
|
185
|
+
}
|
186
|
+
|
187
|
+
CircleInfo[] getCell() {//含まれている円のリストを提示(合体用)
|
188
|
+
|
189
|
+
ArrayList<CircleInfo> cellAtPos=new ArrayList<CircleInfo>();
|
190
|
+
|
191
|
+
for ( CircleInfo c : cell) {
|
192
|
+
|
193
|
+
cellAtPos.add(new CircleInfo(x+c.x, y+c.y, c.dia, c.col));
|
194
|
+
|
195
|
+
}
|
196
|
+
|
197
|
+
return (CircleInfo[])cellAtPos.toArray(new CircleInfo[0]);
|
198
|
+
|
199
|
+
}
|
200
|
+
|
201
|
+
void del() {
|
202
|
+
|
203
|
+
cell.clear();
|
204
|
+
|
205
|
+
}
|
206
|
+
|
207
|
+
void render() {
|
208
|
+
|
209
|
+
for ( CircleInfo c : cell) {
|
210
|
+
|
211
|
+
if (c!=null) {
|
212
|
+
|
213
|
+
fill(c.col);
|
214
|
+
|
215
|
+
circle(x+c.x, y+c.y, c.dia);
|
216
|
+
|
217
|
+
}
|
218
|
+
|
219
|
+
}
|
220
|
+
|
221
|
+
}
|
222
|
+
|
223
|
+
void update() {
|
224
|
+
|
225
|
+
x+=v.x;
|
226
|
+
|
227
|
+
y+=v.y;
|
228
|
+
|
229
|
+
//壁の跳ね返りはここで処理
|
230
|
+
|
231
|
+
//一群の最小/最大のx,y位置を求めて壁との衝突を判定
|
232
|
+
|
233
|
+
float minx=2*width, miny=2*height, maxx=-width, maxy=-height;
|
234
|
+
|
235
|
+
for (CircleInfo c : cell) {
|
236
|
+
|
237
|
+
if (c!=null) {
|
238
|
+
|
239
|
+
if (minx>x+c.x-c.dia/2) {
|
240
|
+
|
241
|
+
minx=x+c.x-c.dia/2;
|
242
|
+
|
243
|
+
}
|
244
|
+
|
245
|
+
if (maxx<x+c.x+c.dia/2) {
|
246
|
+
|
247
|
+
maxx=x+c.x+c.dia/2;
|
248
|
+
|
249
|
+
}
|
250
|
+
|
251
|
+
if (miny>y+c.y-c.dia/2) {
|
252
|
+
|
253
|
+
miny=y+c.y-c.dia/2;
|
254
|
+
|
255
|
+
}
|
256
|
+
|
257
|
+
if (maxy<y+c.y+c.dia/2) {
|
258
|
+
|
259
|
+
maxy=y+c.y+c.dia/2;
|
260
|
+
|
261
|
+
}
|
262
|
+
|
263
|
+
}
|
264
|
+
|
265
|
+
}
|
266
|
+
|
267
|
+
if (minx<=0) {
|
268
|
+
|
269
|
+
x+=-minx;
|
270
|
+
|
271
|
+
v.x=-v.x;
|
272
|
+
|
273
|
+
}
|
274
|
+
|
275
|
+
if (maxx>=width) {
|
276
|
+
|
277
|
+
x-=maxx-width;
|
278
|
+
|
279
|
+
v.x=-v.x;
|
280
|
+
|
281
|
+
}
|
282
|
+
|
283
|
+
if (miny<=0) {
|
284
|
+
|
285
|
+
y+=-miny;
|
286
|
+
|
287
|
+
v.y=-v.y;
|
288
|
+
|
289
|
+
}
|
290
|
+
|
291
|
+
if (maxy>=height) {
|
292
|
+
|
293
|
+
y-=maxy-height;
|
294
|
+
|
295
|
+
v.y=-v.y;
|
296
|
+
|
297
|
+
}
|
298
|
+
|
299
|
+
}
|
300
|
+
|
301
|
+
}
|
302
|
+
|
303
|
+
|
304
|
+
|
305
|
+
boolean collision(Colony c1, Colony c2) {
|
306
|
+
|
307
|
+
boolean ret=false;
|
308
|
+
|
309
|
+
found:
|
310
|
+
|
311
|
+
for (CircleInfo cinfo1 : c1.getCell()) {
|
312
|
+
|
313
|
+
for (CircleInfo cinfo2 : c2.getCell()) {
|
314
|
+
|
315
|
+
if (dist(cinfo1.x, cinfo1.y, cinfo2.x, cinfo2.y)<(cinfo1.dia+cinfo2.dia)/2) {
|
316
|
+
|
317
|
+
ret=true;
|
318
|
+
|
319
|
+
break found;
|
320
|
+
|
321
|
+
}
|
322
|
+
|
323
|
+
}
|
324
|
+
|
325
|
+
}
|
326
|
+
|
327
|
+
return ret;
|
328
|
+
|
329
|
+
}
|
330
|
+
|
331
|
+
|
332
|
+
|
333
|
+
void combine(Colony c1, Colony c2) {
|
334
|
+
|
335
|
+
for (CircleInfo c : c2.getCell()) {
|
336
|
+
|
337
|
+
c1.addCellAtPos(c);
|
338
|
+
|
339
|
+
}
|
340
|
+
|
341
|
+
//合体後速度は質量比とかいろいろ考えられるけどとりあえず平均で。
|
342
|
+
|
343
|
+
c1.v.x=(c1.v.x+c2.v.x)/2;
|
344
|
+
|
345
|
+
c1.v.y=(c1.v.y+c2.v.y)/2;
|
346
|
+
|
347
|
+
c2.del();
|
348
|
+
|
349
|
+
}
|
350
|
+
|
351
|
+
|
352
|
+
|
353
|
+
ArrayList<Colony> cl;
|
354
|
+
|
355
|
+
|
356
|
+
|
357
|
+
void setup() {
|
358
|
+
|
359
|
+
size(500, 500);
|
360
|
+
|
361
|
+
colorMode(HSB);
|
362
|
+
|
363
|
+
cl=new ArrayList<Colony>();
|
364
|
+
|
365
|
+
for (int i=0; i<3; i++) {
|
366
|
+
|
367
|
+
cl.add(
|
368
|
+
|
369
|
+
new Colony( random(50, width-50), random(50, height-50),
|
370
|
+
|
371
|
+
new PVector(random(-5, 5), random(-5, 5)),
|
372
|
+
|
373
|
+
new CircleInfo(0, 0, random(40, 60), color(random(256), 128, 255))
|
374
|
+
|
375
|
+
));
|
376
|
+
|
377
|
+
}
|
378
|
+
|
379
|
+
noStroke();
|
380
|
+
|
381
|
+
}
|
382
|
+
|
383
|
+
void draw() {
|
384
|
+
|
385
|
+
background(255);
|
386
|
+
|
387
|
+
//移動/描画
|
388
|
+
|
389
|
+
for (Colony c : cl) {
|
390
|
+
|
391
|
+
c.update();
|
392
|
+
|
393
|
+
c.render();
|
394
|
+
|
395
|
+
}
|
396
|
+
|
397
|
+
//衝突検出/合体
|
398
|
+
|
399
|
+
for (int i=0; i<cl.size(); i++) {
|
400
|
+
|
401
|
+
for (int j=i+1; j<cl.size(); j++) {
|
402
|
+
|
403
|
+
if (collision(cl.get(i), cl.get(j))) {
|
404
|
+
|
405
|
+
combine(cl.get(i), cl.get(j));
|
406
|
+
|
407
|
+
}
|
408
|
+
|
409
|
+
}
|
410
|
+
|
411
|
+
}
|
412
|
+
|
413
|
+
}
|
414
|
+
|
415
|
+
```
|
416
|
+
|
417
|
+
BouncingBallを使って書けないかというのもちょっと考えてみたけど
|
418
|
+
|
419
|
+
> 合体している2つの球のうち、壁に近いほうが壁にぶつかると2球とも運動の方向が変わること。
|
420
|
+
|
421
|
+
|
422
|
+
|
423
|
+
が結構面倒で断念。一緒に動いている2球(以上)が同時に壁にぶつかったときどうしようかな...というのが。
|
1
追記
test
CHANGED
@@ -17,3 +17,101 @@
|
|
17
17
|
どうでもいい小ネタですけど、色をランダムにするときはcolorMode(HSB)として、
|
18
18
|
|
19
19
|
Hだけを乱数、SとBを255にすると小汚い色にならず、ビビッドカラーが楽しめます。あるいは、Sを128にするとちょっと淡くて柔らかい感じの色とか。
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
---
|
24
|
+
|
25
|
+
5/26 13:13の返信③について、現状のプログラムであってもPVectorを使うのであればBouncingBallクラスはこんなふうにすべきかと思います。(もちろん、ここを変えれば他も影響をうけます)
|
26
|
+
|
27
|
+
```Processing
|
28
|
+
|
29
|
+
class BouncingBall {
|
30
|
+
|
31
|
+
PVector velocity;
|
32
|
+
|
33
|
+
PVector location;
|
34
|
+
|
35
|
+
float r;
|
36
|
+
|
37
|
+
color ballColour;
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
BouncingBall(PVector loc, PVector velo, float rad, color col) {
|
42
|
+
|
43
|
+
velocity=velo;
|
44
|
+
|
45
|
+
location=loc;
|
46
|
+
|
47
|
+
ballColour = col;
|
48
|
+
|
49
|
+
r=rad;
|
50
|
+
|
51
|
+
}
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
void render() {
|
56
|
+
|
57
|
+
//renderという名前のメソッドにrendering以外の仕事をさせてはいけない
|
58
|
+
|
59
|
+
noStroke();
|
60
|
+
|
61
|
+
fill(ballColour);
|
62
|
+
|
63
|
+
ellipse(location.x, location.y, r*2, r*2);
|
64
|
+
|
65
|
+
}
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
void update() {
|
70
|
+
|
71
|
+
location.x += velocity.x;
|
72
|
+
|
73
|
+
location.y += velocity.y;
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
if (location.x - r <= 0 || location.x + r >= width) {
|
78
|
+
|
79
|
+
velocity.x=-velocity.x;
|
80
|
+
|
81
|
+
if (location.x - r <= 0) {
|
82
|
+
|
83
|
+
location.x = r+1;
|
84
|
+
|
85
|
+
}
|
86
|
+
|
87
|
+
if (location.x + r >= width-1) {
|
88
|
+
|
89
|
+
location.x = width-r-1;
|
90
|
+
|
91
|
+
}
|
92
|
+
|
93
|
+
}
|
94
|
+
|
95
|
+
if (location.y - r <= 0 || location.y + r >= height) {
|
96
|
+
|
97
|
+
velocity.y=-velocity.y;
|
98
|
+
|
99
|
+
if (location.y - r <= 0) {
|
100
|
+
|
101
|
+
location.y = r+1;
|
102
|
+
|
103
|
+
}
|
104
|
+
|
105
|
+
if (location.y + r >= height-1) {
|
106
|
+
|
107
|
+
location.y = height-r-1;
|
108
|
+
|
109
|
+
}
|
110
|
+
|
111
|
+
}
|
112
|
+
|
113
|
+
}
|
114
|
+
|
115
|
+
}
|
116
|
+
|
117
|
+
```
|