Created
February 25, 2016 22:56
-
-
Save JoshuaSullivan/046d57c0decb3fb12de3 to your computer and use it in GitHub Desktop.
A Processing 3 sketch animating sprites moving smoothly about the interior of an imaginary cube.
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
static float FOCAL_LENGTH = 200.0; | |
static float ORBIT_RADIUS = 180.0; | |
static int SPARK_COUNT = 200; | |
class Spark { | |
color clr; | |
float orbitRadius; | |
float rx, ry, rz, drx, dry, drz; | |
Spark(color c, float orbitRadius) { | |
this.clr = c; | |
this.orbitRadius = orbitRadius; | |
float rMax = PI / 600.0; | |
this.rx = random(TWO_PI); | |
this.ry = random(TWO_PI); | |
this.rz = random(TWO_PI); | |
this.drx = random(-rMax, rMax); | |
this.dry = random(-rMax, rMax); | |
this.drz = random(-rMax, rMax); | |
} | |
void update() { | |
this.rx += this.drx; | |
this.ry += this.dry; | |
this.rz += this.drz; | |
} | |
void draw() { | |
float z = sin(this.rz) * this.orbitRadius + ORBIT_RADIUS; | |
float scale = FOCAL_LENGTH / (FOCAL_LENGTH + z); | |
float x = sin(this.rx) * this.orbitRadius * scale; | |
float y = sin(this.ry) * this.orbitRadius * scale; | |
float size = 10 * scale; | |
fill(this.clr); | |
ellipse(x, y, size, size); | |
} | |
} | |
Spark[] sparks; | |
void setup() { | |
size(400, 400, P3D); | |
background(0); | |
sparks = new Spark[SPARK_COUNT]; | |
color c = color(255, 0, 0); | |
for (int i = 0; i < SPARK_COUNT; i++) { | |
sparks[i] = new Spark(c, ORBIT_RADIUS); | |
} | |
} | |
void draw() { | |
clear(); | |
pushMatrix(); | |
translate(200, 200); | |
noStroke(); | |
for (int i = 0; i < SPARK_COUNT; i++) { | |
sparks[i].update(); | |
sparks[i].draw(); | |
} | |
popMatrix(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment