Marijn:
大きなプロジェクトに取り組んでI Amが、perticlesは、彼らがお互いに近づく際に対話する必要がなかったです。プログラムは非常にsimpelです。ボールが互いに接触し、又は中心が近い互いの組み合わせ半径未満である場合、それらは色を変えます。しかし、ボールはランダムに今の色を変えていると私は理由はわかりません。それらが相互作用することができますので、オブジェクトは、すべてのオブジェクトとのArrayListを持っており、粒子発生器としてfuntions一つの大きなクラスは、彼らのです。
Box box;
void setup() {
size(1000,1000);
box = new Box(20);
}
void draw() {
background(255);
box.run();
}
オブジェクト
class Object {
int on;
PVector loc = new PVector(500,500);
boolean detect = false;
PVector v;
ArrayList<Object> others = new ArrayList<Object>();
Object(int nin) {
this.on = nin;
this.loc = this.loc.add(PVector.random2D().mult(100));
this.v = PVector.random2D();
}
void move() {
loc.add(PVector.random2D().limit(5));
}
void detect() {
for(int i = 0; i<others.size(); i++) {
if(i != on) {
Object o = others.get(i);
float distance = sqrt(sq(o.loc.x-this.loc.x)+sq(o.loc.y - this.loc.y));
if(distance < 100) {
detect = true;
}
else {
detect = false;
}
}
}
}
void display() {
if(detect == false) {
noFill();
}
if(detect == true) {
fill(200);
}
stroke(50);
ellipse(loc.x,loc.y, 50,50);
fill(0);
text(on, loc.x, loc.y);
}
}
ジェネレータ
class Box {
int n;
ArrayList<Object> objects = new ArrayList<Object>();
Box(int nin) {
this.n = nin;
for(int i = 0; i<n; i++) {
objects.add(new Object(i));
}
for(int i = 0; i< objects.size();i++) {
Object x = objects.get(i);
x.others = this.objects;
}
}
void run() {
for(Object i : objects) {
i.move();
i.detect();
i.display();
}
}
}
Rabbid76:
問題は、メソッド内のループであるdetect
int型のクラスObject
:
for(int i = 0; i<others.size(); i++) { if(i != on) { Object o = others.get(i); float distance = sqrt(sq(o.loc.x-this.loc.x)+sq(o.loc.y - this.loc.y)); if(distance < 100) { detect = true; } else { detect = false; } } }
変数はdetect
、ループの反復ごとに設定されています。ループが終了した後にこのように、の状態は、detect
リストの最後の要素に依存しますothers
。結果は、あなただけのリストの最後の要素を評価するだろうと、まったく同じです。
値があれば、ループを破るdetect
真なり。例えば:
class Object {
// [...]
void detect() {
detect = false;
for(int i = 0; i < others.size() && !detect; i++) {
if(i != on) {
Object o = others.get(i);
float distance = sqrt(sq(o.loc.x-this.loc.x)+sq(o.loc.y - this.loc.y));
detect = distance < 50;
}
}
}
// [...]
}
ノートの3番目と4番目のパラメータは、ellipse()
従って、最小距離はむしろ100より、50、幅および楕円の高さです。