xxxxxxxxxx
170
/*
Mimi Yin NYU-ITP
Drawing the skeleton with joints and bones.
Re-organizing the skeleton with vector-math
and transformations.
*/
// Declare kinectron
let kinectron = null;
function setup() {
createCanvas(windowWidth, windowHeight);
// Define and create an instance of kinectron
kinectron = new Kinectron("192.168.0.117");
// Connect with application over peer
kinectron.makeConnection();
// Request all tracked bodies and pass data to your callback
kinectron.startTrackedBodies(bodyTracked);
background(0);
}
function draw() {
}
function bodyTracked(body) {
background(0);
// 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]);
noFill();
stroke(255);
strokeWeight(10);
// Draw Bust
beginShape();
vertex(head.x, head.y);
vertex(neck.x, neck.y);
vertex(spineShoulder.x, spineShoulder.y);
vertex(spineMid.x, spineMid.y);
vertex(spineBase.x, spineBase.y);
endShape();
// Draw shoulders
line(spineShoulder.x, spineShoulder.y, shoulderRight.x, shoulderRight.y);
line(spineShoulder.x, spineShoulder.y, shoulderLeft.x, shoulderLeft.y);
// Draw Right Arm
beginShape();
vertex(shoulderRight.x, shoulderRight.y);
vertex(elbowRight.x, elbowRight.y);
vertex(wristRight.x, wristRight.y);
vertex(handRight.x, handRight.y);
vertex(handTipRight.x, handTipRight.y);
endShape();
// Draw Right Thumb
line(handRight.x, handRight.y, thumbRight.x, thumbRight.y);
// Hold a key down to draw the left arm on top of the head
if (keyIsPressed) {
push();
let offset = p5.Vector.sub(head, shoulderLeft);
translate(offset.x, offset.y);
beginShape();
vertex(shoulderLeft.x, shoulderLeft.y);
vertex(elbowLeft.x, elbowLeft.y);
vertex(wristLeft.x, wristLeft.y);
vertex(handLeft.x, handLeft.y);
vertex(handTipLeft.x, handTipLeft.y);
endShape();
pop();
}
else {
// Draw Left Arm
beginShape();
vertex(shoulderLeft.x, shoulderLeft.y);
vertex(elbowLeft.x, elbowLeft.y);
vertex(wristLeft.x, wristLeft.y);
vertex(handLeft.x, handLeft.y);
vertex(handTipLeft.x, handTipLeft.y);
endShape();
}
// // Draw Left Thumb
line(handLeft.x, handLeft.y, thumbLeft.x, thumbLeft.y);
//
// // Draw hips
line(spineBase.x, spineBase.y, hipRight.x, hipRight.y);
line(spineBase.x, spineBase.y, hipLeft.x, hipLeft.y);
//
// // Draw Right Leg
beginShape();
vertex(hipRight.x, hipRight.y);
vertex(kneeRight.x, kneeRight.y);
vertex(ankleRight.x, ankleRight.y);
vertex(footRight.x, footRight.y);
endShape();
//
// // Draw Left Leg
beginShape();
vertex(hipLeft.x, hipLeft.y);
vertex(kneeLeft.x, kneeLeft.y);
vertex(ankleLeft.x, ankleLeft.y);
vertex(footLeft.x, footLeft.y);
endShape();
}
// 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 a Vector
function scaleJoint(joint) {
return createVector((joint.cameraX * width / 2) + width / 2, (-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);
}