Advertisement
Guest User

Untitled

a guest
Apr 24th, 2014
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.85 KB | None | 0 0
  1.  
  2. /* INCLUDES --------------------------------- */
  3.  
  4. import java.awt.AWTException;
  5. import java.awt.Robot;
  6. import SimpleOpenNI.*;
  7.  
  8. /* VARIABLES & SETTINGS --------------------- */
  9.  
  10. // settings ---
  11.  
  12. Boolean mirrorHands=false;
  13.  
  14. float turnOnGroundThreshold=80f;
  15. float flyThreshold=80f;
  16.  
  17. // holders ---
  18.  
  19. SimpleOpenNI context;
  20. Robot robot;
  21.  
  22. // state ---
  23.  
  24. Boolean fly=false;
  25. Boolean turnLeft=false;
  26. Boolean turnRight=false;
  27.  
  28. PVector centerOfMass=new PVector();
  29. PVector rightHandPos=new PVector();
  30. PVector leftHandPos=new PVector();
  31. PVector shoulderAveragePos=new PVector();
  32.  
  33. /* INIT ------------------------------------ */
  34.  
  35. void setup() {
  36.  
  37. size(640, 480);
  38. background(255, 255, 255);
  39. smooth();
  40.  
  41. context=new SimpleOpenNI(this);
  42.  
  43. try {
  44. robot=new Robot();
  45. } catch(AWTException e) {
  46. }
  47.  
  48. if(context.isInit() == false) {
  49. exit();
  50. return;
  51. }
  52.  
  53. context.enableDepth();
  54. context.enableUser();
  55.  
  56. }
  57.  
  58. /* PRIVATE SETTERS -------------------------------- */
  59.  
  60. void updatePositionData(int userId) {
  61.  
  62. // center of mass ---
  63.  
  64. context.getCoM(userId, centerOfMass);
  65. context.convertRealWorldToProjective(centerOfMass, centerOfMass);
  66.  
  67. // hands ---
  68.  
  69. context.getJointPositionSkeleton(userId, SimpleOpenNI.SKEL_RIGHT_HAND, rightHandPos);
  70. context.convertRealWorldToProjective(rightHandPos, rightHandPos);
  71.  
  72. context.getJointPositionSkeleton(userId, SimpleOpenNI.SKEL_LEFT_HAND, leftHandPos);
  73. context.convertRealWorldToProjective(leftHandPos, leftHandPos);
  74.  
  75. // shoulders ---
  76.  
  77. PVector p1=new PVector();
  78. PVector p2=new PVector();
  79. PVector p3=new PVector();
  80.  
  81. context.getJointPositionSkeleton(userId, SimpleOpenNI.SKEL_RIGHT_SHOULDER, p1);
  82. context.convertRealWorldToProjective(p1, p1);
  83.  
  84. context.getJointPositionSkeleton(userId, SimpleOpenNI.SKEL_LEFT_SHOULDER, p2);
  85. context.convertRealWorldToProjective(p2, p2);
  86.  
  87. p3.x=(p1.x+p2.x)/2;
  88. p3.y=(p1.y+p2.y)/2;
  89. p3.z=(p1.z+p2.z)/2;
  90.  
  91. shoulderAveragePos=p3;
  92.  
  93. }
  94.  
  95. // gestures ------------
  96.  
  97. void checkTurning(int userId) {
  98.  
  99. // get hand differences from center of mass ---
  100.  
  101. float difLeft=centerOfMass.x-leftHandPos.x;
  102. difLeft=Math.abs(difLeft);
  103.  
  104. float difRight=centerOfMass.x-rightHandPos.x;
  105. difRight=Math.abs(difRight);
  106.  
  107. float dif=Math.abs(difLeft-difRight);
  108.  
  109. Boolean release=true;
  110. int turnTo=0;
  111.  
  112. // check if should turn
  113.  
  114. if(dif>turnOnGroundThreshold) {
  115.  
  116. if(difLeft>difRight) {
  117. turnTo=-1;
  118.  
  119. } else if(difRight>difLeft) {
  120. turnTo=1;
  121. }
  122.  
  123. }
  124.  
  125. if(mirrorHands) turnTo*=-1;
  126. Turn(turnTo);
  127.  
  128. }
  129.  
  130. //------------
  131.  
  132. void checkFlight(int userId) {
  133.  
  134. if(
  135. leftHandPos.y<shoulderAveragePos.y &&
  136. rightHandPos.y<shoulderAveragePos.y
  137. ) {
  138.  
  139. Fly(true);
  140.  
  141. } else {
  142.  
  143. Fly(false);
  144.  
  145. }
  146.  
  147. }
  148.  
  149. /* PUBLIC SETTERS ------------------------- */
  150.  
  151. void Turn(int direction) {
  152.  
  153. switch(direction) {
  154.  
  155. case -1:
  156. robot.keyPress(37);
  157. break;
  158.  
  159. case 1:
  160. robot.keyPress(39);
  161. break;
  162.  
  163. default:
  164. robot.keyRelease(37);
  165. robot.keyRelease(39);
  166. break;
  167.  
  168. }
  169.  
  170. }
  171.  
  172. }
  173.  
  174. //------------
  175.  
  176. void Fly(Boolean b) {
  177.  
  178. if(b) {
  179. robot.keyPress(38);
  180. } else {
  181. robot.keyRelease(38);
  182. }
  183.  
  184. }
  185.  
  186. /* EVENTS --------------------------------- */
  187.  
  188. void draw() {
  189.  
  190. context.update();
  191.  
  192. // debug:
  193. image(context.userImage(),0,0);
  194.  
  195. int[] users=context.getUsers();
  196. int id;
  197.  
  198. if(users.length>0) {
  199.  
  200. id=users[0];
  201.  
  202. // debug ---
  203. drawSkeleton(id);
  204.  
  205. updatePositionData(id);
  206. checkTurning(id);
  207. checkFlight(id);
  208.  
  209. }
  210.  
  211. }
  212.  
  213. // handle user presence ---
  214.  
  215. void onNewUser(SimpleOpenNI curContext, int userId) {
  216. curContext.startTrackingSkeleton(userId);
  217. }
  218.  
  219. void onLostUser(SimpleOpenNI curContext, int userId) {
  220. Turn(0);
  221. Fly(false);
  222. }
  223.  
  224. void onVisibleUser(SimpleOpenNI curContext, int userId) {
  225. }
  226.  
  227. /* DEBUG --------------------------------- */
  228.  
  229. void drawSkeleton(int userId) {
  230.  
  231. strokeWeight(1);
  232.  
  233. // to get the 3d joint data
  234. context.drawLimb(userId, SimpleOpenNI.SKEL_HEAD, SimpleOpenNI.SKEL_NECK);
  235.  
  236. context.drawLimb(userId, SimpleOpenNI.SKEL_NECK, SimpleOpenNI.SKEL_LEFT_SHOULDER);
  237. context.drawLimb(userId, SimpleOpenNI.SKEL_LEFT_SHOULDER, SimpleOpenNI.SKEL_LEFT_ELBOW);
  238. context.drawLimb(userId, SimpleOpenNI.SKEL_LEFT_ELBOW, SimpleOpenNI.SKEL_LEFT_HAND);
  239.  
  240. context.drawLimb(userId, SimpleOpenNI.SKEL_NECK, SimpleOpenNI.SKEL_RIGHT_SHOULDER);
  241. context.drawLimb(userId, SimpleOpenNI.SKEL_RIGHT_SHOULDER, SimpleOpenNI.SKEL_RIGHT_ELBOW);
  242. context.drawLimb(userId, SimpleOpenNI.SKEL_RIGHT_ELBOW, SimpleOpenNI.SKEL_RIGHT_HAND);
  243.  
  244. context.drawLimb(userId, SimpleOpenNI.SKEL_LEFT_SHOULDER, SimpleOpenNI.SKEL_TORSO);
  245. context.drawLimb(userId, SimpleOpenNI.SKEL_RIGHT_SHOULDER, SimpleOpenNI.SKEL_TORSO);
  246.  
  247. context.drawLimb(userId, SimpleOpenNI.SKEL_TORSO, SimpleOpenNI.SKEL_LEFT_HIP);
  248. context.drawLimb(userId, SimpleOpenNI.SKEL_LEFT_HIP, SimpleOpenNI.SKEL_LEFT_KNEE);
  249. context.drawLimb(userId, SimpleOpenNI.SKEL_LEFT_KNEE, SimpleOpenNI.SKEL_LEFT_FOOT);
  250.  
  251. context.drawLimb(userId, SimpleOpenNI.SKEL_TORSO, SimpleOpenNI.SKEL_RIGHT_HIP);
  252. context.drawLimb(userId, SimpleOpenNI.SKEL_RIGHT_HIP, SimpleOpenNI.SKEL_RIGHT_KNEE);
  253. context.drawLimb(userId, SimpleOpenNI.SKEL_RIGHT_KNEE, SimpleOpenNI.SKEL_RIGHT_FOOT);
  254.  
  255. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement