Advertisement
KelvinMead

WallRunning - Requires Kinect Tracker

Aug 30th, 2013
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.54 KB | None | 0 0
  1. import org.openkinect.*;
  2. import org.openkinect.processing.*;
  3. import codeanticode.syphon.*;
  4. PGraphics canvas;
  5. SyphonServer server;
  6.  
  7. // Showing how we can farm all the kinect stuff out to a separate class
  8. KinectTracker tracker;
  9. // Kinect Library object
  10. Kinect kinect;
  11.  
  12. float deg = 15; // Start at 15 degrees
  13.  
  14. int npartTotal = 300;
  15. int npartPerFrame = 2;
  16. float speed = 2.0;
  17. float gravity = 0.05;
  18. float partSize = 20;
  19.  
  20. int totalx = 640;
  21. int totaly = 480;
  22.  
  23. int partLifetime;
  24. PVector positions[];
  25. PVector velocities[];
  26. int lifetimes[];
  27.  
  28. int fcount, lastm;
  29. float frate;
  30. int fint = 3;
  31.  
  32. boolean fill = false;
  33.  
  34. void setup() {
  35. size(totalx, totaly, P3D);
  36. kinect = new Kinect(this);
  37. tracker = new KinectTracker();
  38. kinect.tilt(deg);
  39.  
  40. canvas = createGraphics(totalx, totaly, P3D);
  41. // frameRate(120);
  42.  
  43. partLifetime = npartTotal / npartPerFrame;
  44. initPositions();
  45. initVelocities();
  46. initLifetimes();
  47.  
  48. server = new SyphonServer(this, "Processing Syphon");
  49. canvas.background(0);
  50. }
  51.  
  52. void draw () {
  53. // Run the tracking analysis
  54. tracker.track();
  55. // Show the image
  56. // tracker.display();
  57.  
  58. // Let's get the raw location
  59. PVector v1 = tracker.getPos();
  60.  
  61. canvas.beginDraw();
  62. canvas.background(0);
  63. infodisplay();
  64. // canvas.stroke(255);
  65. // noFill();
  66. canvas.fill(255);
  67. canvas.tint(255, 255);
  68. canvas.normal(0, 0, 1);
  69.  
  70. for (int n = 0; n < npartTotal; n++) { //lifetime check
  71. lifetimes[n]++;
  72. if (lifetimes[n] == partLifetime) {
  73. lifetimes[n] = 0;
  74. }
  75. if (0 <= lifetimes[n]) { // spawn
  76. if (lifetimes[n] == 0) {
  77. // Re-spawn dead particle
  78. // positions[n].x = mouseX;
  79. // positions[n].y = mouseY;
  80. positions[n].x = v1.x;
  81. positions[n].y = v1.y;
  82. float angle = random(0, TWO_PI);
  83. float s = random(0.5 * speed, 0.5 * speed);
  84. velocities[n].x = s * cos(angle);
  85. velocities[n].y = s * sin(angle);
  86. }
  87. else { // gravity
  88. positions[n].x += velocities[n].x;
  89. positions[n].y += velocities[n].y;
  90. velocities[n].y += gravity;
  91. }
  92. drawParticle(positions[n]); //, opacity);
  93. }
  94. }
  95.  
  96. if (fill) canvas.stroke(0);
  97. else canvas.stroke(255);
  98.  
  99. if (v1.y < height/4) {
  100. partSize = 10;
  101. gravity = 0.01;
  102. speed = 10;
  103. }
  104. else {
  105. partSize = 20;
  106. gravity = 0.2;
  107. speed = 2;
  108. }
  109.  
  110.  
  111. fcount += 1;
  112. int m = millis();
  113. if (m - lastm > 1000 * fint) {
  114. frate = float(fcount) / fint;
  115. fcount = 0;
  116. lastm = m;
  117. println("fps: " + frate);
  118. }
  119. canvas.endDraw();
  120. image(canvas, 0, 0);
  121. server.sendImage(canvas);
  122. }
  123.  
  124. void drawParticle(PVector center) { //, float opacity) {
  125. canvas.beginShape(QUAD);
  126. canvas.vertex(center.x - partSize/2, center.y - partSize/2, 0, 0);
  127. canvas.vertex(center.x + partSize/2, center.y - partSize/2, 64, 0);
  128. canvas.vertex(center.x + partSize/2, center.y + partSize/2, 64, 64);
  129. canvas.vertex(center.x - partSize/2, center.y + partSize/2, 0, 64);
  130. canvas.endShape();
  131. }
  132.  
  133. void initPositions() {
  134. positions = new PVector[npartTotal];
  135. for (int n = 0; n < positions.length; n++) {
  136. positions[n] = new PVector();
  137. }
  138. }
  139.  
  140. void initVelocities() {
  141. velocities = new PVector[npartTotal];
  142. for (int n = 0; n < velocities.length; n++) {
  143. velocities[n] = new PVector();
  144. }
  145. }
  146.  
  147. void initLifetimes() {
  148. // Initializing particles with negative lifetimes so they are added
  149. // progressively into the screen during the first frames of the sketch
  150. lifetimes = new int[npartTotal];
  151. int t = -1;
  152. for (int n = 0; n < lifetimes.length; n++) {
  153. if (n % npartPerFrame == 0) {
  154. t++;
  155. }
  156. lifetimes[n] = -t;
  157. }
  158. }
  159.  
  160. void keyPressed() {
  161. int t = tracker.getThreshold();
  162. if (key == CODED) {
  163. if (keyCode == LEFT) {
  164. t+=5;
  165. tracker.setThreshold(t);
  166. }
  167. else if (keyCode == RIGHT) {
  168. t-=5;
  169. tracker.setThreshold(t);
  170. }
  171. else if (keyCode == UP) {
  172. deg++;
  173. }
  174. else if (keyCode == DOWN) {
  175. deg--;
  176. }
  177. deg = constrain(deg, 0, 30);
  178. kinect.tilt(deg);
  179. }
  180. }
  181.  
  182. void keyReleased()
  183. {
  184. switch(key) {
  185. case 'f':
  186. if (fill) fill = false;
  187. else if (!fill) fill = true;
  188. break;
  189. }
  190. }
  191.  
  192. void infodisplay() {
  193. // Display some info
  194. int t = tracker.getThreshold();
  195. println("threshold: " + t + " " + "framerate: " + (int)frameRate + " " + "Angle:" + deg);
  196. }
  197.  
  198. void stop() {
  199. tracker.quit();
  200. background(0);
  201. super.stop();
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement