Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
clayton-kenney committed Dec 15, 2019
0 parents commit db0d9c4
Show file tree
Hide file tree
Showing 3 changed files with 386 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
// Original Code based off of Coding train challenge #88, by Daniel Shiffman
// thecodingtrain.com/CodingChallenges/088-snowfall.html
// Modied by Clayton Kenney, Nov 2019.

import com.hamoid.*;

import org.openkinect.freenect.*;
import org.openkinect.processing.*;

// The kinect stuff is happening in another class
KinectTracker tracker;
Kinect kinect;

ArrayList<Snowflake> snow; //array that holds the snowflakes
PVector gravity; //gravity global variable
float angle; //angle global variable
int t; //time global variable
VideoExport videoExport;
int frameRate = 30;
boolean render = false;
float xOff;



//generates random that skews smaller by comapring random numbers. add one to ensure flakes aren't too small
float getRandomSize() {
float r3 = randomGaussian() * 2 + 5;
return constrain(abs(r3), 3, 100);
}

//snowflake constructor
class Snowflake {

PVector pos;
PVector vel;
PVector acc;
float r;
float radius;
float initialAngle;


Snowflake(float x1, float y1) {
float x = x1; //starting point of flake based on width
float y = y1; //generate flakes offscreen so they are falling
float xv = random(-.5, .5); //assign random horizontal vector on creation
pos = new PVector(x, y); //position vector
vel = new PVector(xv, 0); //initial velocity vector
acc = new PVector(); //acceleration vector
r = getRandomSize(); //snowflake size based on above rando algo
radius = sqrt(random(pow(width / 2, 2)));
initialAngle = random(0, 2 * PI); //generate random starting angle between 0 & 2PI
xOff = 0;
}

void applyForce(PVector force){
//faux parallax effect
PVector f = force.copy();
f.mult(r); //gravity acts more on 'bigger' aka heavier particles
acc.add(f); //gravity force to accelerator

}
void randomize() {
float x = random(width);
float y = random(-100, -10);
float xv = random(0); //assign random horizontal vector on creation
pos = new PVector(x, y);
vel = new PVector(xv, 0); //initial velocity vector
acc = new PVector(); //acceleration vector
initialAngle = random(0, 2 * PI); //generate random starting angle between 0 & 2PI
r = getRandomSize(); //snowflake size based on above rando algo

}
void update() {
vel.add(acc);
vel.limit(r * .35); //gravity limit .2-.5
pos.add(vel);
acc.mult(0);

/*
//commented out for now
float w = 0.002; // angular speed
float angle = w * t + initialAngle;
pos.x = width/ 2 + radius * sin(angle);
*/

if (pos.y > height + r) {
randomize();
}

}
void offsetR() {
acc.x += random(100, 200);
}
void offsetL() {
acc.x -= (random(100, 200));
}
void render() {
stroke(255);
strokeWeight(r);
point(pos.x, pos.y);
}
}
void setup() {
//const canvas = createCanvas(1080, 3438); //canvas is 1/4 scale of tower, set to canvas variable
fullScreen(); //1080x3438 full res, 270x860 1/4 scale
kinect = new Kinect(this);
tracker = new KinectTracker();

if (render) {
videoExport = new VideoExport(this, "3000.mp4");
videoExport.setQuality(100, 128);
videoExport.setFrameRate(frameRate);
}
frameRate(frameRate);

gravity = new PVector(0, 1); //define gravity
snow = new ArrayList<Snowflake>();
for (int i=0; i < 2000; i++) {
float x = random(width);
float y = random(height);
snow.add(new Snowflake(x, y));
}

//no export if render is set to false
if (render) {
videoExport.startMovie();
}

}
void draw(){
background(1); //black for test
t = frameCount; //set the time

// Run the tracking analysis
tracker.track();
// Show the image
tracker.display();

// Let's draw the raw location
PVector v1 = tracker.getPos();
float vx1 = map(v1.x, 0, 640, 0, 1280);
float vy1 = map(v1.y, 0, 480, 0, 680);
fill(0, 0, 0, 200);
stroke(255, 0, 0);
strokeWeight(.5);
ellipse(vx1, vy1, 175, 200);

// Display some info
noStroke();
int t = tracker.getThreshold();
fill(255);
textSize(10);
text("threshold: " + t + " " + int(frameRate) + " " +
"UP increase threshold, DOWN decrease threshold", 10, 710);
textSize(100);
fill(40);
text("LET", 100, 200);
text("IT", 400, 550);
text("SNOW", 900, 400);


//Make Snow Flakes!!

for(int i =0; i< snow.size(); i++){
Snowflake flake = snow.get(i);
flake.applyForce(gravity);
flake.update();
flake.render();
if (flake.pos.x > vx1 && flake.pos.x < vx1 + 110 && flake.pos.y > vy1 - 100 && flake.pos.y < vy1 + 100){
flake.offsetR();
//flake.pos.x += 200;
//flake.vel.x += random(200, 300);
}
if (flake.pos.x < vx1 && flake.pos.x > vx1 - 110 && flake.pos.y > vy1 - 100 && flake.pos.y < vy1 + 100){
flake.offsetL();
//flake.pos.x -= 200;
// flake.vel.x -= random(200, 300);

}
}


//Renderer
if (render) {
videoExport.saveFrame();
}
noStroke();
fill(150);
}

void keyPressed() {
if (key == 'q') {
videoExport.endMovie();
exit();
}
int t = tracker.getThreshold();
if (key == CODED) {
if (keyCode == UP) {
t+=5;
tracker.setThreshold(t);
} else if (keyCode == DOWN) {
t-=5;
tracker.setThreshold(t);
}
}
}
45 changes: 45 additions & 0 deletions sketch_5000_Snowflake_final/ffmpeg.txt

Large diffs are not rendered by default.

135 changes: 135 additions & 0 deletions sketch_5000_Snowflake_final/sketch_5000_Snowflake_final.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
// Original Code based off of Coding train challenge #88, by Daniel Shiffman
// thecodingtrain.com/CodingChallenges/088-snowfall.html
// Modified by Clayton Kenney, Nov 2019.

import com.hamoid.*; //Video Export library

ArrayList<Snowflake> snow; //array that holds the snowflakes
PVector gravity; //gravity global variable
float angle; //angle global variable
int t; //time global variable
VideoExport videoExport; //Initialize video export library
int frameRate = 30; //set framerate, 60fps for final export
boolean render = false; //render variable, set to false if testing
float wind; //wind varible, didn't end up using.

//generates random number using randomGaussian function. Scaled to deliver appropriate size flakes. Change depending on render size.
float getRandomSize() {
float r3 = randomGaussian() * 3 + 5;
return constrain(abs(r3), 3, 100);
}

//snowflake constructor
class Snowflake {

PVector pos;
PVector vel;
PVector acc;
float r;
float radius;
float initialAngle;


Snowflake(float x, float y) {
float xv = random(-.5, .5); //assign random horizontal vector left or right
pos = new PVector(x, y); //position vector
vel = new PVector(xv, 0); //initial velocity vector
acc = new PVector(); //acceleration vector
r = getRandomSize(); //snowflake size based on above rando algo
radius = sqrt(random(pow(width / 2, 2))); //define radius for lateral movement of the snowflake
initialAngle = random(0, 2 * PI); //generate random starting angle between 0 & 2PI
wind = 0;
}

void applyForce(PVector force){
//faux parallax effect
PVector f = force.copy();
f.mult(r); //gravity acts more on 'bigger' aka heavier particles
acc.add(f); //gravity force to accelerator

}
void randomize() {
float x = random(width);
float y = random(-100, -10);
float xv = random(0); //assign random horizontal vector on creation
pos = new PVector(x, y);
vel = new PVector(xv, 0); //initial velocity vector
acc = new PVector(); //acceleration vector
initialAngle = random(0, 2 * PI); //generate random starting angle between 0 & 2PI
r = getRandomSize(); //snowflake size based on above rando algo

}
void update() {
vel.add(acc);
vel.limit(r * .3); //gravity limit .2-.5 depening on size of flake. to account for wind resistance
pos.add(vel);
acc.mult(0);

//horizontal movement, updates x position every frame, based on https://p5js.org/examples/simulate-snowflakes.html
float w = 0.003; // angular speed, .003 is greate for full resolution
float angle = w * t + initialAngle;
pos.x = width/ 2 + radius * sin(angle);

if (pos.y > height + r) { //if snowflake passes bottom of screen, pos.y, run randomize and move back to top offscreen
randomize();
}

}
void render() { //render the snowflake
stroke(255); //255 for white
strokeWeight(r);
point(pos.x, pos.y);
}
}
void setup() {
size(1080, 3438); //1080x3438 full res, 270x860 1/4 scale

//EXPORT SETTING, no export if render is set to false
if (render) {
videoExport = new VideoExport(this, "3000.mp4"); //change the file name here
videoExport.setQuality(100, 128); //quality settings, (Video, Audio)
videoExport.setFrameRate(frameRate);
}
frameRate(frameRate);

gravity = new PVector(0, 1); //define gravity

//Make snowflakes and add them in the Snow Arraylist
snow = new ArrayList<Snowflake>();
for (int i=0; i < 3000; i++) { //change number of snowflakes here. 3000 & 5000 look best on projection
float x = random(width);
float y = random(height);
snow.add(new Snowflake(x, y));
}

//no export if render is set to false
if (render) {
videoExport.startMovie();
}

}
void draw(){
background(1); //black background
t = frameCount; //set the time, used for horizontal movement updates

// LOOP THROUGH ARRAYLIST AND RENDER, APPLY GRAVITY, AND UPDATE POSITION
for(int i =0; i< snow.size(); i++){
Snowflake flake = snow.get(i);
flake.applyForce(gravity);
flake.update();
flake.render();
}

//Renderer Save function
if (render) {
videoExport.saveFrame();
}
}

//End render/sketch if Q is pressed
void keyPressed() {
if (key == 'q') {
videoExport.endMovie();
exit();
}
}

0 comments on commit db0d9c4

Please sign in to comment.