xxxxxxxxxx
119
/*
Mimi Yin NYU-ITP
Drawing skeleton joints and bones.
*/
// Declare kinectron
let kinectron = null;
// 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() {
//Nothing to see here
}
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 2 joints to connect
let start = handRight;
let end = handLeft;
// Draw a line
stroke(255);
line(start.x, start.y, end.x, end.y);
let d = dist(start.x, start.y, end.x, end.y);
// Map the distance to angle speed
let aspeed = map(d, 0, width, 0, PI/2);
// Inverse, non-linear mapping
//let aspeed = 1/d;
a+=aspeed;
noStroke();
// Calculate circular pathway
let x = cos(a)*width/4 + width/2;
let y = sin(a)*width/4 + height/2;
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);
}