Author: Not specified Language: java
Description: Not specified Timestamp: 2017-06-22 14:49:56 +0000
View raw paste Reply
  1. import kinect4WinSDK.Kinect;
  2. import kinect4WinSDK.SkeletonData;
  3. import processing.sound.*;
  4.  
  5. enum HandState {
  6.   LEFT,
  7.   TOP,
  8.   RIGHT,
  9.   BOTTOM,
  10.   CENTER
  11. }
  12.  
  13. enum SessionState {
  14.   RUNNING,
  15.   NOT_RUNNING
  16. }
  17.  
  18. Kinect kinect;
  19. ArrayList <SkeletonData> bodies;
  20. SessionState sessionState = SessionState.NOT_RUNNING;
  21. HandState rightHandState = HandState.CENTER;
  22. HandState leftHandState = HandState.CENTER;
  23.  
  24. int currentImage;
  25. int currentSound;
  26.  
  27. final int MAX_IMAGES = 10;
  28. final int MAX_SOUNDS = 10;
  29.  
  30. SoundFile sound;
  31. PImage image;
  32.  
  33. void setup()
  34. {
  35.   size(640, 480);
  36.   background(255);
  37.   kinect = new Kinect(this);
  38.   smooth();
  39.   bodies = new ArrayList<SkeletonData>();
  40.   sessionState = SessionState.NOT_RUNNING;
  41.   popSound(currentSound);
  42.   popImage(currentImage);
  43. }
  44.  
  45. void draw()
  46. {
  47.   background(255);
  48.   noStroke();
  49.  
  50.   if(image != null) {
  51.     imageMode(CENTER);
  52.     image(image, width * 0.5F, height * 0.5F, width, height);
  53.   }
  54.  
  55.   fill(0, 100, 255);
  56.  
  57.   if(bodies.size() > 0) {
  58.     updateHandsMotion(bodies.get(0));
  59.     drawHandsMotion(bodies.get(0));
  60.   }
  61. }
  62.  
  63. void updateHandsMotion(SkeletonData skeletonData) {
  64.  
  65.   PVector right = getBodyPartPositions(skeletonData, Kinect.NUI_SKELETON_POSITION_HAND_RIGHT);
  66.   PVector left = getBodyPartPositions(skeletonData, Kinect.NUI_SKELETON_POSITION_HAND_LEFT);
  67.  
  68.   if(rightHandState == HandState.CENTER) {
  69.     if(isHand_TOP(right) && sessionState == SessionState.NOT_RUNNING) {
  70.       sessionState = SessionState.RUNNING;
  71.       rightHandState = HandState.TOP;
  72.       resetResources();
  73.     } else if(isHand_DOWN(right) && sessionState == SessionState.RUNNING) {
  74.       sessionState = SessionState.NOT_RUNNING;
  75.       rightHandState = HandState.BOTTOM;
  76.       resetResources();
  77.     }
  78.   } else if(isHand_CENTERED(right)) {
  79.     rightHandState = HandState.CENTER;
  80.     println("RIGHT HAND : CENTER");
  81.   }
  82.  
  83.   if(sessionState == SessionState.NOT_RUNNING)
  84.     return;
  85.  
  86.   if(leftHandState == HandState.CENTER) {
  87.     if(isHand_LEFT(left) && currentImage - 1 >= 0) {
  88.       leftHandState = HandState.LEFT;
  89.       currentImage = currentImage - 1;
  90.       popImage(currentImage);
  91.     } else if(isHand_TOP(left) && currentSound + 1 < MAX_SOUNDS) {
  92.       leftHandState = HandState.TOP;
  93.       currentSound = currentSound + 1;
  94.       popSound(currentSound);
  95.     } else if(isHand_RIGHT(left) && currentImage + 1 < MAX_IMAGES) {
  96.       leftHandState = HandState.RIGHT;
  97.       currentImage = currentImage + 1;
  98.       popImage(currentImage);
  99.     } else if(isHand_DOWN(left) && currentSound - 1 >= 0) {
  100.       leftHandState = HandState.BOTTOM;
  101.       currentSound = currentSound - 1;
  102.       popSound(currentSound);
  103.     }
  104.   } else if(isHand_CENTERED(left)) {
  105.     leftHandState = HandState.CENTER;
  106.     println("LEFT HAND : CENTER");
  107.   }
  108. }
  109.  
  110. void drawHandsMotion(SkeletonData skeletonData) {
  111.   if (isBodyPartTracked(skeletonData, Kinect.NUI_SKELETON_POSITION_HAND_RIGHT)) {
  112.     ellipse(
  113.       getBodyPartPositions(skeletonData, Kinect.NUI_SKELETON_POSITION_HAND_RIGHT).x * width,
  114.       getBodyPartPositions(skeletonData, Kinect.NUI_SKELETON_POSITION_HAND_RIGHT).y * height,
  115.       16,
  116.       16
  117.     );
  118.   }
  119.   if (isBodyPartTracked(skeletonData, Kinect.NUI_SKELETON_POSITION_HAND_LEFT)) {
  120.     ellipse(
  121.       getBodyPartPositions(skeletonData, Kinect.NUI_SKELETON_POSITION_HAND_LEFT).x * width,
  122.       getBodyPartPositions(skeletonData, Kinect.NUI_SKELETON_POSITION_HAND_LEFT).y * height,
  123.       16,
  124.       16
  125.     );
  126.   }
  127. }
  128.  
  129. PVector getBodyPartPositions(SkeletonData skeletonData, int bodyPart) {
  130.   return skeletonData.skeletonPositions[bodyPart];
  131. }
  132.  
  133. boolean isBodyPartTracked(SkeletonData skeletonData, int bodyPart) {
  134.   return skeletonData.skeletonPositionTrackingState[bodyPart] != Kinect.NUI_SKELETON_POSITION_NOT_TRACKED;
  135. }
  136.  
  137. boolean isHand_LEFT(PVector hand) {
  138.   return hand.x * width < width * 0.25F;
  139. }
  140.  
  141. boolean isHand_TOP(PVector hand) {
  142.   return hand.y * height < height * 0.25F;
  143. }
  144.  
  145. boolean isHand_RIGHT(PVector hand) {
  146.   return hand.x * width > width * 0.75F;
  147. }
  148.  
  149. boolean isHand_DOWN(PVector hand) {
  150.   return hand.y * height > height * 0.75F;
  151. }
  152.  
  153. boolean isHand_CENTERED(PVector hand) {
  154.   return hand.x * width > width * 0.33F
  155.       && hand.x * width < width * 0.66F
  156.       && hand.y * height > height * 0.33F
  157.       && hand.y * height < height * 0.66F;
  158. }
  159.  
  160. void appearEvent(SkeletonData skeletonData) {
  161.   if (skeletonData.trackingState == Kinect.NUI_SKELETON_NOT_TRACKED)
  162.     return;
  163.   synchronized(bodies) {
  164.     bodies.add(skeletonData);
  165.   }
  166. }
  167.  
  168. void disappearEvent(SkeletonData skeletonData) {
  169.   synchronized(bodies) {
  170.     for (int i = bodies.size() - 1; i >= 0; i --) {
  171.       if (skeletonData.dwTrackingID == bodies.get(i).dwTrackingID) {
  172.         bodies.remove(i);
  173.       }
  174.     }
  175.   }
  176. }
  177.  
  178. void moveEvent(SkeletonData skeletonDataB, SkeletonData skeletonDataA) {
  179.   if (skeletonDataA.trackingState == Kinect.NUI_SKELETON_NOT_TRACKED) {
  180.     return;
  181.   }
  182.   synchronized(bodies) {
  183.     for (int i = bodies.size() - 1; i >= 0; i --) {
  184.       if (skeletonDataB.dwTrackingID == bodies.get(i).dwTrackingID) {
  185.         bodies.get(i).copy(skeletonDataA);
  186.         break;
  187.       }
  188.     }
  189.   }
  190. }
  191.  
  192. int lastPoppedSound = -1;
  193. int lastPoppedImage = -1;
  194.  
  195. void popSound(int index) {
  196.   if(lastPoppedSound == index)
  197.     return;
  198.   lastPoppedSound = index;
  199.   if(sound != null)
  200.     sound.stop();
  201.   sound = new SoundFile(this, "sounds/" + index + ".mp3");
  202.   if(sound != null)
  203.     sound.loop();
  204. }
  205.  
  206. void popImage(int index) {
  207.   if(lastPoppedImage == index)
  208.     return;
  209.   lastPoppedImage = index;
  210.   image = loadImage("images/" + index + ".png");
  211. }
  212.  
  213. void resetResources() {
  214.   currentSound = 0;
  215.   currentImage = 0;
  216.   popSound(currentSound);
  217.   popImage(currentImage);
  218. }
View raw paste Reply