Skip to content

Instantly share code, notes, and snippets.

@joshgiesbrecht
Created March 17, 2020 22:50
Show Gist options
  • Save joshgiesbrecht/8434bba256bc8bb5638c7b8ba8cfdda1 to your computer and use it in GitHub Desktop.
Save joshgiesbrecht/8434bba256bc8bb5638c7b8ba8cfdda1 to your computer and use it in GitHub Desktop.
small Processing sketch that creates a set of channel colors for the tf MUD client, for use on ifMUD
int SIZE = 20;
float maxval = 5;
float mindist = 2;
float springconst = 0.02;
PVector[] nodes = new PVector[SIZE];
PVector[] pushes = new PVector[SIZE];
void setup() {
size(400,400);
// initialize nodes
for (int i=0; i < SIZE; i++) {
nodes[i] = new PVector(random(0,maxval),random(0,maxval),random(0,maxval));
pushes[i] = new PVector(0,0,0);
}
frameRate(10);
}
void draw() {
background(0);
noStroke();
for (int i=0; i < SIZE; i++) {
pushes[i] = new PVector(0,0,0); // clear old movement
for (int j=0; j < SIZE; j++) {
if (PVector.dist(roundVec(nodes[i]),roundVec(nodes[j])) < mindist) {
PVector diff = PVector.sub(nodes[i],nodes[j]);
PVector tmp = getSpringForce(diff, mindist, springconst);
pushes[i].add(tmp); // let's see if that works
}
if (nodes[i].x > maxval) {
nodes[i].x = maxval;
}
if (nodes[i].y > maxval) {
nodes[i].y = maxval;
}
if (nodes[i].z > maxval) {
nodes[i].z = maxval;
}
if (nodes[i].x < 0) {
nodes[i].x = 0;
}
if (nodes[i].y < 0) {
nodes[i].y = 0;
}
if (nodes[i].z < 0) {
nodes[i].z = 0;
}
// make sure color isn't too dark overall
if (roundVec(nodes[i]).mag() < 3) {
PVector tmp = getSpringForce(nodes[i], 3, springconst);
pushes[i].add(tmp);
}
// don't let anything stay at pure blue, it's too dark
if ( round(nodes[i].x) + round(nodes[i].y) < 3 ) {
// make sure we're keeping the B component high at least
if (round(nodes[i].z) < 4) {
PVector tmp = new PVector(0,0,springconst);
pushes[i].add(tmp);
}
PVector tmp = getSpringForce(nodes[i], 2, springconst);
tmp = new PVector(tmp.x, tmp.y, 0); // ignore z
pushes[i].add(tmp);
}
}
//pushes[i].normalize(); // re-normalize after multiple pushes
//pushes[i].mult(0.02);
nodes[i].add(pushes[i]); // move the node
fill( round(nodes[i].x) * 256 / maxval,
round(nodes[i].y) * 256 / maxval,
round(nodes[i].z) * 256 / maxval );
ellipse( nodes[i].x * (width-50) / 5 + 25,
nodes[i].y * (height-50) / 5 + 25,
nodes[i].z + 5, nodes[i].z + 5 );
}
}
void keyPressed() {
for (int i=0; i<SIZE; i++) {
print("Crgb" + round(nodes[i].x) + round(nodes[i].y) + round(nodes[i].z) + " ");
}
println();
println();
}
PVector roundVec(PVector v) {
PVector tmp = new PVector(round(v.x), round(v.y), round(v.z));
return tmp;
}
PVector getSpringForce(PVector diff, float relaxedDist, float springConst) {
PVector tmp = diff.get();
tmp.normalize();
float k = diff.mag();
float m = relaxedDist - k;
if (m < relaxedDist/10) {
m = relaxedDist/10;
}
m = m * springConst;
tmp.mult(m);
return tmp;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment