xxxxxxxxxx
148
/*
Mimi Yin NYU-ITP
Drawing skeleton joints and bones.
*/
// Declare kinectron
let kinectron = null;
// Keep track of bodies
let bodies = {};
function setup() {
createCanvas(windowWidth, windowHeight);
// Define and create an instance of kinectron
kinectron = new Kinectron("192.168.0.100");
// 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);
for (let b in bodies) {
// Get this body using it's id
let body = bodies[b];
// Check to see if this body is dead
// Delete dead bodies and move onto next one
if (frameCount - body.frameCount > 30) {
delete bodies[b];
continue;
}
// Get all the joints off the tracked body and do something with them
let joints = body.joints;
// Mid-line
let head = scaleJoint(joints[kinectron.HEAD]);
let neck = scaleJoint(joints[kinectron.NECK]);
let spineShoulder = scaleJoint(joints[kinectron.SPINESHOULDER]);
let spineMid = scaleJoint(joints[kinectron.SPINEMID]);
let spineBase = scaleJoint(joints[kinectron.SPINEBASE]);
// Right Arm
let shoulderRight = scaleJoint(joints[kinectron.SHOULDERRIGHT]);
let elbowRight = scaleJoint(joints[kinectron.ELBOWRIGHT]);
let wristRight = scaleJoint(joints[kinectron.WRISTRIGHT]);
let handRight = scaleJoint(joints[kinectron.HANDRIGHT]);
let handTipRight = scaleJoint(joints[kinectron.HANDTIPRIGHT]);
let thumbRight = scaleJoint(joints[kinectron.THUMBRIGHT]);
// Left Arm
let shoulderLeft = scaleJoint(joints[kinectron.SHOULDERLEFT]);
let elbowLeft = scaleJoint(joints[kinectron.ELBOWLEFT]);
let wristLeft = scaleJoint(joints[kinectron.WRISTLEFT]);
let handLeft = scaleJoint(joints[kinectron.HANDLEFT]);
let handTipLeft = scaleJoint(joints[kinectron.HANDTIPLEFT]);
let thumbLeft = scaleJoint(joints[kinectron.THUMBLEFT]);
// Right Leg
let hipRight = scaleJoint(joints[kinectron.HIPRIGHT]);
let kneeRight = scaleJoint(joints[kinectron.KNEERIGHT]);
let ankleRight = scaleJoint(joints[kinectron.ANKLERIGHT]);
let footRight = scaleJoint(joints[kinectron.FOOTRIGHT]);
// Left Leg
let hipLeft = scaleJoint(joints[kinectron.HIPLEFT]);
let kneeLeft = scaleJoint(joints[kinectron.KNEELEFT]);
let ankleLeft = scaleJoint(joints[kinectron.ANKLELEFT]);
let footLeft = scaleJoint(joints[kinectron.FOOTLEFT]);
// Pick a joint to track
let pos = handLeft;
let newSpeed;
// If this body has a ppos...
if (body.ppos) {
let speed = dist(pos.x, pos.y, body.ppos.x, body.ppos.y);
fill(255, 0, 0, 10);
noStroke();
ellipse(pos.x, pos.y, speed * 5, speed * 5);
// Use the average Speed to control speed of circle
let aspeed = speed / 1000; // What about speed/100??
//let aspeed = 0.1/speed;
body.a += aspeed;
}
// Calculate circular pathway
let x = cos(body.a) * width / 4 + width / 2;
let y = sin(body.a) * width / 4 + height / 2;
fill('white');
noStroke();
ellipse(x, y, 5, 5);
// Create a ppos for this body
body.ppos = pos;
}
}
function bodyTracked(body) {
let id = body.trackingId;
if (id in bodies) {
bodies[id].joints = body.joints;
bodies[id].frameCount = frameCount;
} else {
console.log("NEW BODY!");
bodies[id] = {
joints: body.joints,
frameCount: frameCount,
ppos : null,
a: 0
}
}
}
// 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);
}