TouchOSC and Processing Accelerometer Example Code


/ Published in: Java
Save to your folder(s)

Playing with my iPhone and Processing v1 on the train last night I started messing with TouchOSC and thought that this super simple (and fairly rough) example code using the oscP5 library may help someone on their way to interactive wobbly nirvana.


Copy this code and paste it in your HTML
  1. import processing.opengl.*;
  2. import oscP5.*;
  3.  
  4. OscP5 oscP5;
  5.  
  6. float xrot = 0;
  7. float zrot = 0;
  8.  
  9. float xrot_targ = 0;
  10. float zrot_targ = 0;
  11. float orientation = 0;
  12.  
  13. float dampSpeed = 5;
  14.  
  15. void setup() {
  16. size(400,400, OPENGL);
  17. oscP5 = new OscP5(this,8000);
  18. smooth();
  19. }
  20.  
  21. void draw() {
  22. camera( 0, 0, 300,
  23. 0, 0, 0,
  24. 0.0, 1.0, 0.0
  25. );
  26. background(0);
  27.  
  28. // Basic value smoothing
  29.  
  30. if (xrot_targ > xrot) {
  31. xrot = xrot + ((xrot_targ - xrot) / dampSpeed);
  32. } else {
  33. xrot = xrot - ((xrot - xrot_targ) / dampSpeed);
  34. }
  35.  
  36. if (zrot_targ > zrot) {
  37. zrot = zrot + ((zrot_targ - zrot) / dampSpeed);
  38. } else {
  39. zrot = zrot - ((zrot - zrot_targ) / dampSpeed);
  40. }
  41.  
  42. // Detection for if the iPhone is upsidedown or not
  43.  
  44. if (orientation < 0) {
  45. fill(255,0,0);
  46. rotateX(radians(xrot));
  47. rotateZ(radians(zrot));
  48. } else {
  49. fill(255,255,0);
  50. rotateX(radians(xrot*-1));
  51. rotateZ(radians(zrot*-1));
  52. }
  53. box(130,10,60);
  54.  
  55. }
  56.  
  57. void oscEvent(OscMessage theOscMessage) {
  58. if(theOscMessage.checkAddrPattern("/accxyz")==true) {
  59. xrot_targ = (theOscMessage.get(0).floatValue()*90);
  60. zrot_targ = (theOscMessage.get(1).floatValue()*90)*-1;
  61. orientation = theOscMessage.get(2).floatValue();
  62. }
  63. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.