xxxxxxxxxx
130
/*
Mimi Yin NYU-ITP
Drawing skeleton joints and bones.
*/
// Declare kinectron
let kinectron = null;
// Remember previous positions to calculate speed
let pposes = [];
// Variables for circle
let a = 0;
function setup() {
createCanvas(windowWidth, windowHeight);
// Define and create an instance of kinectron
kinectron = new Kinectron("10.17.201.104");
// Connect with application over peer
kinectron.makeConnection();
// Request all tracked bodies and pass data to your callback
kinectron.startTrackedBodies(bodyTracked);
background(0);
}
function draw() {
//background(0);
}
function bodyTracked(body) {
background(0, 10);
// Draw all the joints
//kinectron.getJoints(drawJoint);
// Get all the joints off the tracked body and do something with them
// Mid-line
let head = scaleJoint(body.joints[kinectron.HEAD]);
let neck = scaleJoint(body.joints[kinectron.NECK]);
let spineShoulder = scaleJoint(body.joints[kinectron.SPINESHOULDER]);
let spineMid = scaleJoint(body.joints[kinectron.SPINEMID]);
let spineBase = scaleJoint(body.joints[kinectron.SPINEBASE]);
// Right Arm
let shoulderRight = scaleJoint(body.joints[kinectron.SHOULDERRIGHT]);
let elbowRight = scaleJoint(body.joints[kinectron.ELBOWRIGHT]);
let wristRight = scaleJoint(body.joints[kinectron.WRISTRIGHT]);
let handRight = scaleJoint(body.joints[kinectron.HANDRIGHT]);
let handTipRight = scaleJoint(body.joints[kinectron.HANDTIPRIGHT]);
let thumbRight = scaleJoint(body.joints[kinectron.THUMBRIGHT]);
// Left Arm
let shoulderLeft = scaleJoint(body.joints[kinectron.SHOULDERLEFT]);
let elbowLeft = scaleJoint(body.joints[kinectron.ELBOWLEFT]);
let wristLeft = scaleJoint(body.joints[kinectron.WRISTLEFT]);
let handLeft = scaleJoint(body.joints[kinectron.HANDLEFT]);
let handTipLeft = scaleJoint(body.joints[kinectron.HANDTIPLEFT]);
let thumbLeft = scaleJoint(body.joints[kinectron.THUMBLEFT]);
// Right Leg
let hipRight = scaleJoint(body.joints[kinectron.HIPRIGHT]);
let kneeRight = scaleJoint(body.joints[kinectron.KNEERIGHT]);
let ankleRight = scaleJoint(body.joints[kinectron.ANKLERIGHT]);
let footRight = scaleJoint(body.joints[kinectron.FOOTRIGHT]);
// Left Leg
let hipLeft = scaleJoint(body.joints[kinectron.HIPLEFT]);
let kneeLeft = scaleJoint(body.joints[kinectron.KNEELEFT]);
let ankleLeft = scaleJoint(body.joints[kinectron.ANKLELEFT]);
let footLeft = scaleJoint(body.joints[kinectron.FOOTLEFT]);
// Pick joints to track
let poses = [handLeft, handRight, head];
// Find current average position of all the joints
let newAvgSpeed = 0;
// Average all the positions
for (let p = 0; p < pposes.length; p++) {
let pos = poses[p];
let ppos = pposes[p];
let speed = dist(pos.x, pos.y, ppos.x, ppos.y);
newAvgSpeed += speed;
}
if (pposes.length > 0) {
newAvgSpeed /= pposes.length;
}
// Remember current positions for next time
pposes = poses.slice();
// Use the average speed to control speed of circle
let aspeed = newAvgSpeed / 1000;
a += aspeed;
// Calculate circular pathway
let x = cos(a) * width / 4 + width / 2;
let y = sin(a) * width / 4 + height / 2;
fill('white');
noStroke();
ellipse(x, y, 5, 5);
}
// Scale the joint position data to fit the screen
// 1. Move it to the center of the screen
// 2. Flip the y-value upside down
// 3. Return it as an object literal
function scaleJoint(joint) {
return {
x: (joint.cameraX * width / 2) + width / 2,
y: (-joint.cameraY * width / 2) + height / 2,
}
}
// 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);
}