Created
February 2, 2016 23:01
-
-
Save camb416/71edd412c69af40d40e8 to your computer and use it in GitHub Desktop.
simpleSwarmer.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
/** | |
* Simple Swarmer | |
* | |
*/ | |
int numAttractors = 256; | |
int numMasses = 4096; | |
Attractor[] a = new Attractor[numAttractors]; | |
PointMass[] pts = new PointMass[numMasses]; | |
void setup() { | |
size(1280, 720); | |
frameRate(60); | |
for ( int i=0; i<numAttractors; i++) { | |
a[i] = new Attractor(); | |
} | |
for ( int i=0; i<numMasses; i++) { | |
pts[i] = new PointMass(); | |
} | |
} | |
void update() { | |
for ( int i=0; i<numAttractors; i++) { | |
a[i].update(); | |
} | |
for ( int i=0; i<numMasses; i++) { | |
pts[i].update(a); | |
} | |
} | |
void draw() { | |
update(); | |
background(212); | |
for ( int i=0; i<numAttractors; i++) { | |
a[i].draw(); | |
} | |
for ( int i=0; i<numMasses; i++) { | |
pts[i].draw(a); | |
} | |
fill(0); | |
text(frameRate + "fps", 10, 10, 100, 30); | |
} | |
class Point { | |
float x, y; | |
Point() { | |
} | |
Point(float _x, float _y) { | |
x = _x; | |
y = _y; | |
} | |
}; | |
class PointMass { | |
Point pos; | |
Point vel; | |
Point acc; | |
PointMass() { | |
pos = new Point(random(width-10f) + 5.0f, random(height-10.0f) + 5.0f); | |
vel = new Point(random(4.0f) - 2.0f, random(4.0f) - 2.0f); | |
acc = new Point(0, 0); | |
} | |
void update(Attractor[] a) { | |
for (int i=0; i<a.length; i++) { | |
float d = dist(pos.x, pos.y, a[i].pos.x, a[i].pos.y); | |
if (d<100) { | |
// float angle = atan2(a[i].pos.y - pos.y, a[i].pos.x, a[i].pos.x - pos.x); // not needed | |
acc.x += (a[i].pos.x - pos.x)*(2.0f/(d*d)); | |
acc.y += (a[i].pos.y - pos.y)*(2.0f/(d*d)); | |
} | |
} | |
acc.x = max(min(acc.x,1),-1); | |
acc.y = max(min(acc.y,1),-1); | |
// vel.x *= 0.95f; | |
// vel.y *= 0.95f; | |
vel.x += acc.x; | |
vel.y += acc.y; | |
vel.x = max(min(vel.x,2),-2); | |
vel.y = max(min(vel.y,2),-2); | |
pos.x += vel.x; | |
pos.y += vel.y; | |
// check for bounce | |
if (pos.x >width || pos.x < 0) { | |
pos.x -= 2*vel.x; | |
vel.x *= -1.0f; | |
} | |
if (pos.y > height || pos.y < 0) { | |
pos.y -= 2*vel.y; | |
vel.y *= -1.0f; | |
} | |
} | |
void draw(Attractor[] a) { | |
/* | |
for (int i=0; i<a.length; i++) { | |
float d = dist(pos.x, pos.y, a[i].pos.x, a[i].pos.y); | |
if (d<100) { | |
noFill(); | |
stroke(0); | |
line(a[i].pos.x, a[i].pos.y, pos.x, pos.y); | |
} | |
} | |
*/ | |
fill(0,0,0,64); | |
noStroke(); | |
ellipse(pos.x, pos.y, 3, 3); | |
} | |
}; | |
void keyPressed(){ | |
setup(); | |
} | |
class Attractor { | |
Point pos; | |
Point vel; | |
Attractor() { | |
pos = new Point(random(width-10f) + 5.0f, random(height-10.0f) + 5.0f); | |
vel = new Point(random(4.0f) - 2.0f, random(4.0f) - 2.0f); | |
} | |
void update() { | |
pos.x += vel.x; | |
pos.y += vel.y; | |
// check for bounce | |
if (pos.x >width || pos.x < 0) { | |
pos.x -= 2*vel.x; | |
vel.x *= -1.0f; | |
} | |
if (pos.y > height || pos.y < 0) { | |
pos.y -= 2*vel.y; | |
vel.y *= -1.0f; | |
} | |
} | |
void draw() { | |
fill(255); | |
noStroke(); | |
ellipse(pos.x, pos.y, 3, 3); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment