Last active
October 20, 2015 19:46
-
-
Save andresperezl/daaa3e896d9898be71ed to your computer and use it in GitHub Desktop.
Moving a circle with Processing and kinect
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package core; | |
import processing.core.*; | |
import SimpleOpenNI.*; | |
public class Test extends PApplet { | |
SimpleOpenNI kinect; | |
int rectx = 240; | |
int recty = 200; | |
float accx = 0; | |
float accy = 0; | |
float prevXLH; | |
float prevYLH; | |
float prevXRH; | |
float prevYRH; | |
float prevXLF; | |
float prevYLF; | |
float prevXRF; | |
float prevYRF; | |
public void setup() { | |
size(640, 480); | |
kinect = new SimpleOpenNI(this, SimpleOpenNI.RUN_MODE_MULTI_THREADED); | |
kinect.setMirror(true); | |
kinect.enableDepth(); | |
kinect.enableRGB(); | |
kinect.alternativeViewPointDepthToImage(); | |
kinect.enableUser(SimpleOpenNI.SKEL_PROFILE_ALL); | |
} | |
public void draw() { | |
kinect.update(); | |
PImage depth = kinect.rgbImage(); | |
image(depth, 0, 0); | |
IntVector userList = new IntVector(); | |
// write the list of detected users | |
// into our vector | |
kinect.getUsers(userList); | |
// if we found any users | |
if (userList.size() > 0) { | |
// get the first user | |
int userId = userList.get(0); | |
// if we're successfully calibrated | |
if (kinect.isTrackingSkeleton(userId)) { | |
// make a vector to store the left hand | |
PVector rightHand = new PVector(); | |
PVector leftHand = new PVector(); | |
PVector rightFoot = new PVector(); | |
PVector leftFoot = new PVector(); | |
// put the position of the left hand into that vector | |
float confidence = kinect.getJointPositionSkeleton(userId, | |
SimpleOpenNI.SKEL_LEFT_HAND, leftHand); | |
confidence = kinect.getJointPositionSkeleton(userId, | |
SimpleOpenNI.SKEL_RIGHT_HAND, rightHand); | |
confidence = kinect.getJointPositionSkeleton(userId, | |
SimpleOpenNI.SKEL_RIGHT_FOOT, rightFoot); | |
confidence = kinect.getJointPositionSkeleton(userId, | |
SimpleOpenNI.SKEL_LEFT_FOOT, leftFoot); | |
// convert the detected hand position | |
// to "projective" coordinates | |
// that will match the depth image | |
PVector convertedRightHand = new PVector(); | |
PVector convertedLeftHand = new PVector(); | |
PVector convertedRightFoot = new PVector(); | |
PVector convertedLeftFoot = new PVector(); | |
kinect.convertRealWorldToProjective(rightHand, | |
convertedRightHand); | |
kinect.convertRealWorldToProjective(leftHand, convertedLeftHand); | |
kinect.convertRealWorldToProjective(leftFoot, convertedLeftFoot); | |
kinect.convertRealWorldToProjective(rightFoot, | |
convertedRightFoot); | |
// and display it | |
float currentXLH = convertedLeftHand.x; | |
float currentYLH = convertedLeftHand.y; | |
float currentXRH = convertedRightHand.x; | |
float currentYRH = convertedRightHand.y; | |
float currentXLF = convertedLeftFoot.x; | |
float currentYLF = convertedLeftFoot.y; | |
float currentXRF = convertedRightFoot.x; | |
float currentYRF = convertedRightFoot.y; | |
fill(0, 255, 0); | |
ellipse(convertedRightHand.x, convertedRightHand.y, 10, 10); | |
fill(255, 0, 0); | |
ellipse(convertedLeftHand.x, convertedLeftHand.y, 10, 10); | |
fill(255, 255, 0); | |
ellipse(convertedRightFoot.x, convertedRightFoot.y, 10, 10); | |
fill(255, 0, 255); | |
ellipse(convertedLeftFoot.x, convertedLeftFoot.y, 10, 10); | |
if (Math.abs(currentXLH - rectx) <= 20 | |
&& Math.abs(currentYLH - recty) <= 20) { | |
accx = currentXLH - prevXLH; | |
accy = currentYLH - prevYLH; | |
} | |
if (Math.abs(currentXRH - rectx) <= 20 | |
&& Math.abs(currentYRH - recty) <= 20) { | |
accx = currentXRH - prevXRH; | |
accy = currentYRH - prevYRH; | |
} | |
if (Math.abs(currentXLF - rectx) <= 20 | |
&& Math.abs(currentYLF - recty) <= 20) { | |
accx = currentXLF - prevXLF; | |
accy = currentYLF - prevYLF; | |
} | |
if (Math.abs(currentXRF - rectx) <= 20 | |
&& Math.abs(currentYRF - recty) <= 20) { | |
accx = currentXRF - prevXRF; | |
accy = currentYRF - prevYRF; | |
} | |
prevXLH = currentXLH; | |
prevYLH = currentYLH; | |
prevXRH = currentXRH; | |
prevYRH = currentYRH; | |
prevXLF = currentXLF; | |
prevYLF = currentYLF; | |
prevXRF = currentXRF; | |
prevYRF = currentYRF; | |
} | |
} | |
rectx += accx; | |
recty += accy; | |
fill(0, 0, 255); | |
ellipse(rectx, recty, 40, 40); | |
accx *= 0.1f; | |
accy *= 0.1f; | |
} | |
public void onNewUser(int userId) { | |
println("start pose detection"); | |
kinect.startPoseDetection("Psi", userId); | |
} | |
public void onEndCalibration(int userId, boolean successful) { | |
if (successful) { | |
println(" User calibrated !!!"); | |
kinect.startTrackingSkeleton(userId); | |
} else { | |
println(" Failed to calibrate user !!!"); | |
kinect.startPoseDetection("Psi", userId); | |
} | |
} | |
public void onStartPose(String pose, int userId) { | |
println("Started pose for user"); | |
kinect.stopPoseDetection(userId); | |
kinect.requestCalibrationSkeleton(userId, true); | |
} | |
public boolean sketchFullScreen() { | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment