xxxxxxxxxx
240
//https://www.openprocessing.org/sketch/157576 // orignal code
var num = 300; // determines # of particles
var noiseScale = 500,
noiseStrength = 1;
var particles = [num];
let circ;
let d;
var loc;
//Kinect adds:
// IP Address of kinectron server
let IP = "localhost";
// Scale size of skeleton
let SCL = 0.25;
// Declare kinectron
let kinectron = null;
// Variable for moving line
let y = 0;
// Store y-values over time
let yes = [];
// Joint indices by name
let PELVIS = 0;
let SPINE_NAVAL = 1;
let SPINE_CHEST = 2;
let NECK = 3;
let CLAVICLE_LEFT = 4;
let SHOULDER_LEFT = 5;
let ELBOW_LEFT = 6;
let WRIST_LEFT = 7;
let HAND_LEFT = 8;
let HANDTIP_LEFT = 9;
let THUMB_LEFT = 10;
let CLAVICLE_RIGHT = 11;
let SHOULDER_RIGHT = 12;
let ELBOW_RIGHT = 13;
let WRIST_RIGHT = 14;
let HAND_RIGHT = 15;
let HANDTIP_RIGHT = 16;
let THUMB_RIGHT = 17;
let HIP_LEFT = 18;
let KNEE_LEFT = 19;
let ANKLE_LEFT = 20;
let FOOT_LEFT = 21;
let HIP_RIGHT = 22;
let KNEE_RIGHT = 23;
let ANKLE_RIGHT = 24;
let FOOT_RIGHT = 25;
let HEAD = 26;
let NOSE = 27;
let EYE_LEFT = 28;
let EAR_LEFT = 29;
let EYE_RIGHT = 30;
let EAR_RIGHT = 31;
function setup() {
createCanvas(windowWidth, windowHeight);
//Kinect adds:
// Define and create an instance of kinectron
kinectron = new Kinectron(IP);
// Set kinect version to azure
kinectron.setKinectType("azure");
// Connect with application over peer
kinectron.makeConnection();
// Request all tracked bodies and pass data to your callback
kinectron.startTrackedBodies(bodyTracked);
background(0);
noStroke();
for (let i = 0; i < num; i++) {
//x value start slightly outside the right of canvas, z value how close to viewer
loc = createVector(random(width * 1.2), random(height), 3);
var angle = 0; //any value to initialize
var dir = createVector(cos(angle), sin(angle));
var speed = random(0.5, 2);
// var speed = random(5,map(mouseX,0,width,5,20)); // faster
// creates every particle giving it a random starting location, direction, and random speed
particles[i] = new Particle(loc, dir, speed);
}
circ = new Circle(width / 2, height / 2, 50);
}
function windowResized() {
resizeCanvas(windowWidth, windowHeight);
}
function draw() {
fill(0, 20);
noStroke();
// this creates a transparent background, giving the particles trails
//rect(0, 0, width, height);
circ.show();
for (let i = 0; i < particles.length; i++) {
particles[i].run();
}
// for every particle, check the distance between particle j and the circle
for (let j = 0; j < particles.length; j++) {
d = dist(particles[j].loc.x, particles[j].loc.y, particles[j].loc.z, circ.x, circ.y, circ.r);
// if the distance is less than the radius of both the circle + the particle display a white rectangle in the top left corner
if (d < circ.r + loc.z) {
fill(255);
//rect(0, 0, 100, 100);
}
}
//print(d);
}
//Kinect adds:
function bodyTracked(body) {
background(0, 10);
// Get all the joints off the tracked body and do something with them
let joints = body.skeleton.joints;
// Pick a joint to position
let joint = scaleJoint(joints[HEAD]);
// Map join to height of canvas
let y = map(joint.y, 100, 500, 0, width);
// Add y value to array
yes.push(y);
// Only 120 most recent values
if(yes.length > 120) yes.shift();
// Calculate an average y-value
let avgY = 0;
for(let yvalue of yes) {
avgY+=yvalue;
}
avgY/=yes.length;
// Draw a point
strokeWeight(20);
stroke('white');
// Draw the unison point
//point(width / 2, y);
// Draw the average
point(width /4, avgY);
}
// 0. Scale the joint position data to fit the screen
// 1. Move it to the center of the screen
// 2. Flip the x-value to mirror
// 3. Return it as an object literal
function scaleJoint(joint) {
return {
x: (-joint.cameraX * SCL) + width / 2,
y: (joint.cameraY * SCL) + height / 2,
z: (joint.cameraZ * SCL),
}
}
// Draw skeleton
function drawJoint(joint) {
//console.log("JOINT OBJECT", joint);
let pos = scaleJoint(joint);
//Kinect location data needs to be normalized to canvas size
stroke(255);
strokeWeight(5);
point(pos.x, pos.y);
}
class Circle {
constructor(_x, _y, _r) {
this.x = _x;
this.y = _y;
this.r = _r;
}
show() {
fill(255, 100);
ellipse(this.x, this.y, this.r)
}
}
class Particle {
constructor(_loc, _dir, _speed) {
this.loc = _loc;
this.dir = _dir;
this.speed = _speed;
// var col;
}
run() {
this.move();
this.checkEdges();
this.update();
// this.checkBox();
}
move() {
let angle = noise(this.loc.x / noiseScale, this.loc.y / noiseScale, frameCount / noiseScale) * TWO_PI * noiseStrength; //0-2PI
this.dir.x = cos(angle);
this.dir.y = sin(angle);
var vel = this.dir.copy();
var d = 1; //direction change
vel.mult(this.speed * d); //vel = vel * (speed*d)
this.loc.add(vel); //loc = loc + vel
}
checkEdges() {
//float distance = dist(width/2, height/2, loc.x, loc.y);
//if (distance>150) {
if (this.loc.x < 0 || this.loc.x > width || this.loc.y < 0 || this.loc.y > height) {
this.loc.x = random(width * 1.2);
this.loc.y = random(height);
}
}
checkBox() {
if (this.loc.x < width / 2 && this.loc.y > height / 2) {
this.loc.x = random(width / 2, width);
this.loc.y = random(height / 2);
}
}
update() {
fill(0, 100, 255, 100);
ellipse(this.loc.x, this.loc.y, this.loc.z);
}
}