# 斥力を働かせ合うパーティクル（壁からも斥力）

```/**
* particles with reputation (walls are also repulsive)
*
* @author aa_debdeb
* @date 2016/06/15
*/

float G = 1000.0;
float MAX_VEL = 5.0;
float MAX_ACC = 1.0;
ArrayList<Particle> particles;

void setup(){
size(500, 500);
particles = new ArrayList<Particle>();
}

void draw(){
background(60);
for(Particle p1: particles){
for(Particle p2: particles){
if(p1 != p2){
}
}
}
for(Particle p: particles){
p.update();
p.display();
}
}

class Particle{

PVector pos, vel, acc;

Particle(float x, float y){
pos = new PVector(x, y);
vel = new PVector(0, 0);
acc = new PVector(0, 0);
}

float d = sq(pos.dist(p.pos));
PVector force = PVector.sub(pos, p.pos);
force.normalize();
force.mult(G / d);
}

void display(){
noStroke();
fill(255, 128, 0, 200);
ellipse(pos.x, pos.y, 20, 20);
fill(255, 200);
ellipse(pos.x, pos.y, 5, 5);
}

void update(){
PVector[] walls = new PVector[4];
walls[0] = new PVector(pos.x, 0);
walls[1] = new PVector(width, pos.y);
walls[2] = new PVector(pos.x, height);
walls[3] = new PVector(0, pos.y);
for(PVector w: walls){
float d = sq(pos.dist(w));
PVector force = PVector.sub(pos, w);
force.normalize();
force.mult(G / d);
}
acc.limit(MAX_ACC);
vel.limit(MAX_VEL);
acc = new PVector(0, 0);
if(pos.x < 0){
vel.x *= -1;
pos.x += vel.x;
}
if(pos.x >= width){
vel.x *= -1;
pos.x += vel.x;
}
if(pos.y < 0){
vel.y *= -1;
pos.y += vel.y;
}
if(pos.y >= width){
vel.y *= -1;
pos.y += vel.y;
}
}
}
```