xxxxxxxxxx
181
/*
Mimi Yin NYU-ITP
Controlling speed and angle of line with joint positions.
*/
// IP Address of kinectron server
let IP = "localhost";
// Scale size of skeleton
let SCL = 0.5;
// Declare kinectron
let kinectron = null;
// Variable for moving line
let y = 0;
// Variables for 2 joints
let right, left;
// Keep track of bodies
let bodies = [];
// 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);
// 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);
kinectron.startBodies(function(bodies){
console.log(bodies);
});
background(0);
}
function draw() {
//Nothing to see here
// Once there's data
if (left && right) {
// Map right hand to speed
let speed = map(right.y, 500, 100, 100, 0);
// Map left hand to angle of rotation
let a = map(left.y, 500, 100, 0, PI);
a = constrain(a, 0, PI);
// Add speed
y += speed;
if(y > height) y = -height;
stroke('white');
// Draw a line
push();
translate(width/2, height/2);
rotate(a);
line(0, -height, 0, y);
pop();
}
}
function bodyTracked(body) {
background(0, 10);
// Get all the joints off the tracked body and do something with them
let joints = body.skeleton.joints;
// Mid-line
let pelvis = scaleJoint(joints[PELVIS]);
let spineNaval = scaleJoint(joints[SPINE_NAVAL]);
let spineChest = scaleJoint(joints[SPINE_CHEST]);
let neck = scaleJoint(joints[NECK]);
// Left Arm
let clavicleLeft = scaleJoint(joints[CLAVICLE_LEFT]);
let shoulderLeft = scaleJoint(joints[SHOULDER_LEFT]);
let elbowLeft = scaleJoint(joints[ELBOW_LEFT]);
let wristLeft = scaleJoint(joints[WRIST_LEFT]);
let handLeft = scaleJoint(joints[HAND_LEFT]);
let handTipLeft = scaleJoint(joints[HANDTIP_LEFT]);
let thumbLeft = scaleJoint(joints[THUMB_LEFT]);
// Right Arm
let clavicleRight = scaleJoint(joints[CLAVICLE_LEFT]);
let shoulderRight = scaleJoint(joints[SHOULDER_RIGHT]);
let elbowRight = scaleJoint(joints[ELBOW_RIGHT]);
let wristRight = scaleJoint(joints[WRIST_RIGHT]);
let handRight = scaleJoint(joints[HAND_RIGHT]);
let handTipRight = scaleJoint(joints[HANDTIP_RIGHT]);
let thumbRight = scaleJoint(joints[THUMB_RIGHT]);
// Left Leg
let hipLeft = scaleJoint(joints[HIP_LEFT]);
let kneeLeft = scaleJoint(joints[KNEE_LEFT]);
let ankleLeft = scaleJoint(joints[ANKLE_LEFT]);
let footLeft = scaleJoint(joints[FOOT_LEFT]);
// Right Leg
let hipRight = scaleJoint(joints[HIP_RIGHT]);
let kneeRight = scaleJoint(joints[KNEE_RIGHT])
let ankleRight = scaleJoint(joints[ANKLE_RIGHT]);
let footRight = scaleJoint(joints[FOOT_RIGHT]);
// Head
let head = scaleJoint(joints[HEAD]);
let nose = scaleJoint(joints[NOSE]);
let eyeLeft = scaleJoint(joints[EYE_LEFT]);
let earLeft = scaleJoint(joints[EAR_LEFT]);
let eyeRight = scaleJoint(joints[EYE_RIGHT]);
let earRight = scaleJoint(joints[EAR_RIGHT]);
right = handRight;
left = handLeft;
}
// 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);
}