Skip to content

Instantly share code, notes, and snippets.

@camb416
Created February 3, 2016 18:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save camb416/1f083e1ef5dcd8834e37 to your computer and use it in GitHub Desktop.
Save camb416/1f083e1ef5dcd8834e37 to your computer and use it in GitHub Desktop.
/**
* Simple Swarmer 2
*
*/
int numAttractors = 256;
int numMasses = 8192*2;
float maxSpeed = 4.0f;
float maxAccel = 1.0f;
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();
smooth();
background(212);
//fill(255,212,192,16);
//rect(0,0,width,height);
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;
}
float getAngle(){
return atan2(y,x);
}
void setMax(float _m){
float f = dist(x,y,0,0);
if(abs(f) > _m){
float scale = _m/abs(f);
x *= scale;
y *= scale;
}
}
};
class PointMass {
color c;
Point pos;
Point vel;
Point acc;
PointMass() {
c = color(random(64)+64,random(64)+64,random(64)+64);
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.setMax(4.0f);
//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);
vel.setMax(1.0f);
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);
stroke(c);
noFill();
// ellipse(pos.x, pos.y, 2, 2);
// rect(pos.x,pos.y,1,1);
point(pos.x,pos.y);
}
};
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(6.0f) - 3.0f, random(6.0f) - 3.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() {
stroke(255);
noFill();
// rect(pos.x,pos.y,1,1);
point(pos.x,pos.y);
// 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