Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* INCLUDES --------------------------------- */
- import java.awt.AWTException;
- import java.awt.Robot;
- import SimpleOpenNI.*;
- /* VARIABLES & SETTINGS --------------------- */
- // settings ---
- Boolean mirrorHands=false;
- float turnOnGroundThreshold=80f;
- float flyThreshold=80f;
- // holders ---
- SimpleOpenNI context;
- Robot robot;
- // state ---
- Boolean fly=false;
- Boolean turnLeft=false;
- Boolean turnRight=false;
- PVector centerOfMass=new PVector();
- PVector rightHandPos=new PVector();
- PVector leftHandPos=new PVector();
- PVector shoulderAveragePos=new PVector();
- /* INIT ------------------------------------ */
- void setup() {
- size(640, 480);
- background(255, 255, 255);
- smooth();
- context=new SimpleOpenNI(this);
- try {
- robot=new Robot();
- } catch(AWTException e) {
- }
- if(context.isInit() == false) {
- exit();
- return;
- }
- context.enableDepth();
- context.enableUser();
- }
- /* PRIVATE SETTERS -------------------------------- */
- void updatePositionData(int userId) {
- // center of mass ---
- context.getCoM(userId, centerOfMass);
- context.convertRealWorldToProjective(centerOfMass, centerOfMass);
- // hands ---
- context.getJointPositionSkeleton(userId, SimpleOpenNI.SKEL_RIGHT_HAND, rightHandPos);
- context.convertRealWorldToProjective(rightHandPos, rightHandPos);
- context.getJointPositionSkeleton(userId, SimpleOpenNI.SKEL_LEFT_HAND, leftHandPos);
- context.convertRealWorldToProjective(leftHandPos, leftHandPos);
- // shoulders ---
- PVector p1=new PVector();
- PVector p2=new PVector();
- PVector p3=new PVector();
- context.getJointPositionSkeleton(userId, SimpleOpenNI.SKEL_RIGHT_SHOULDER, p1);
- context.convertRealWorldToProjective(p1, p1);
- context.getJointPositionSkeleton(userId, SimpleOpenNI.SKEL_LEFT_SHOULDER, p2);
- context.convertRealWorldToProjective(p2, p2);
- p3.x=(p1.x+p2.x)/2;
- p3.y=(p1.y+p2.y)/2;
- p3.z=(p1.z+p2.z)/2;
- shoulderAveragePos=p3;
- }
- // gestures ------------
- void checkTurning(int userId) {
- // get hand differences from center of mass ---
- float difLeft=centerOfMass.x-leftHandPos.x;
- difLeft=Math.abs(difLeft);
- float difRight=centerOfMass.x-rightHandPos.x;
- difRight=Math.abs(difRight);
- float dif=Math.abs(difLeft-difRight);
- Boolean release=true;
- int turnTo=0;
- // check if should turn
- if(dif>turnOnGroundThreshold) {
- if(difLeft>difRight) {
- turnTo=-1;
- } else if(difRight>difLeft) {
- turnTo=1;
- }
- }
- if(mirrorHands) turnTo*=-1;
- Turn(turnTo);
- }
- //------------
- void checkFlight(int userId) {
- if(
- leftHandPos.y<shoulderAveragePos.y &&
- rightHandPos.y<shoulderAveragePos.y
- ) {
- Fly(true);
- } else {
- Fly(false);
- }
- }
- /* PUBLIC SETTERS ------------------------- */
- void Turn(int direction) {
- switch(direction) {
- case -1:
- robot.keyPress(37);
- break;
- case 1:
- robot.keyPress(39);
- break;
- default:
- robot.keyRelease(37);
- robot.keyRelease(39);
- break;
- }
- }
- }
- //------------
- void Fly(Boolean b) {
- if(b) {
- robot.keyPress(38);
- } else {
- robot.keyRelease(38);
- }
- }
- /* EVENTS --------------------------------- */
- void draw() {
- context.update();
- // debug:
- image(context.userImage(),0,0);
- int[] users=context.getUsers();
- int id;
- if(users.length>0) {
- id=users[0];
- // debug ---
- drawSkeleton(id);
- updatePositionData(id);
- checkTurning(id);
- checkFlight(id);
- }
- }
- // handle user presence ---
- void onNewUser(SimpleOpenNI curContext, int userId) {
- curContext.startTrackingSkeleton(userId);
- }
- void onLostUser(SimpleOpenNI curContext, int userId) {
- Turn(0);
- Fly(false);
- }
- void onVisibleUser(SimpleOpenNI curContext, int userId) {
- }
- /* DEBUG --------------------------------- */
- void drawSkeleton(int userId) {
- strokeWeight(1);
- // to get the 3d joint data
- context.drawLimb(userId, SimpleOpenNI.SKEL_HEAD, SimpleOpenNI.SKEL_NECK);
- context.drawLimb(userId, SimpleOpenNI.SKEL_NECK, SimpleOpenNI.SKEL_LEFT_SHOULDER);
- context.drawLimb(userId, SimpleOpenNI.SKEL_LEFT_SHOULDER, SimpleOpenNI.SKEL_LEFT_ELBOW);
- context.drawLimb(userId, SimpleOpenNI.SKEL_LEFT_ELBOW, SimpleOpenNI.SKEL_LEFT_HAND);
- context.drawLimb(userId, SimpleOpenNI.SKEL_NECK, SimpleOpenNI.SKEL_RIGHT_SHOULDER);
- context.drawLimb(userId, SimpleOpenNI.SKEL_RIGHT_SHOULDER, SimpleOpenNI.SKEL_RIGHT_ELBOW);
- context.drawLimb(userId, SimpleOpenNI.SKEL_RIGHT_ELBOW, SimpleOpenNI.SKEL_RIGHT_HAND);
- context.drawLimb(userId, SimpleOpenNI.SKEL_LEFT_SHOULDER, SimpleOpenNI.SKEL_TORSO);
- context.drawLimb(userId, SimpleOpenNI.SKEL_RIGHT_SHOULDER, SimpleOpenNI.SKEL_TORSO);
- context.drawLimb(userId, SimpleOpenNI.SKEL_TORSO, SimpleOpenNI.SKEL_LEFT_HIP);
- context.drawLimb(userId, SimpleOpenNI.SKEL_LEFT_HIP, SimpleOpenNI.SKEL_LEFT_KNEE);
- context.drawLimb(userId, SimpleOpenNI.SKEL_LEFT_KNEE, SimpleOpenNI.SKEL_LEFT_FOOT);
- context.drawLimb(userId, SimpleOpenNI.SKEL_TORSO, SimpleOpenNI.SKEL_RIGHT_HIP);
- context.drawLimb(userId, SimpleOpenNI.SKEL_RIGHT_HIP, SimpleOpenNI.SKEL_RIGHT_KNEE);
- context.drawLimb(userId, SimpleOpenNI.SKEL_RIGHT_KNEE, SimpleOpenNI.SKEL_RIGHT_FOOT);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement