Advertisement
Guest User

seb

a guest
Nov 28th, 2014
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.58 KB | None | 0 0
  1. import processing.opengl.*;
  2.  
  3. import toxi.geom.*;
  4. import toxi.physics2d.*;
  5. import toxi.physics2d.behaviors.*;
  6.  
  7. import java.util.concurrent.*;
  8.  
  9. int NUM_PARTICLES = 500;
  10.  
  11. VerletPhysics2D physics;
  12.  
  13. // these queues are needed to add/remove behaviors when working with multiple threads
  14. AbstractQueue<ParticleBehavior2D> addQueue=new ConcurrentLinkedQueue<ParticleBehavior2D>();
  15. AbstractQueue<ParticleBehavior2D> killQueue=new ConcurrentLinkedQueue<ParticleBehavior2D>();
  16.  
  17. // this map will link attractors to their TUIO cursor IDs
  18. HashMap<Long, AttractionBehavior> activeAttractors=new HashMap<Long, AttractionBehavior>();
  19.  
  20. void setup() {
  21.   size(680, 382, OPENGL);
  22.   // setup physics with 10% drag
  23.   physics = new VerletPhysics2D();
  24.   physics.setDrag(0.05f);
  25.   physics.setWorldBounds(new Rect(0, 0, width, height));
  26.   // the NEW way to add gravity to the simulation, using behaviors
  27.   //  physics.addBehavior(new GravityBehavior(new Vec2D(0, 0)));
  28.  
  29.   // init the TUIO protocol
  30.   setupTUIO();
  31. }
  32.  
  33. void addParticle() {
  34.   VerletParticle2D p = new VerletParticle2D(Vec2D.randomVector().scale(5).addSelf(width / 2, height/2));
  35.   physics.addParticle(p);
  36.   // add a negative attraction force field around the new particle
  37.   physics.addBehavior(new AttractionBehavior(p, 20, -1.2f, 0.01f));
  38. }
  39.  
  40. void draw() {
  41.   // first process the queues
  42.   while (killQueue.peek() != null) {
  43.     physics.removeBehavior(killQueue.poll());
  44.   }
  45.   while (addQueue.peek() != null) {
  46.     physics.addBehavior(addQueue.poll());
  47.   }
  48.   background(0, 0, 0);
  49.   noStroke();
  50.   fill(255);
  51.   if (physics.particles.size() < NUM_PARTICLES) {
  52.     addParticle();
  53.   }
  54.   physics.update();
  55.   stroke(255);
  56.   for (VerletParticle2D p : physics.particles) {
  57.     ellipse(p.x, p.y, 10, 10);
  58.   }
  59.   drawTUIO(); // call the TUIO function
  60. }
  61.  
  62. // create a new attractor around the given position, also requires a TUIO cursor ID
  63. void initDrag(long id, int posX, int posY) {
  64.   AttractionBehavior att = new AttractionBehavior(new Vec2D(posX, posY), 250, 0.9f);
  65.   // add to queue
  66.   addQueue.offer(att);
  67.   // and to hash map for future reference
  68.   activeAttractors.put(id, att);
  69. }
  70.  
  71. // updates the attractor position for the given TUIO cursor ID
  72. void dragging(long id, int posX, int posY) {
  73.   AttractionBehavior att=activeAttractors.get(id);
  74.   if (att!=null) {
  75.     att.getAttractor().set(posX, posY);
  76.   }
  77. }
  78.  
  79. // removes the attractor for the given TUIO cursor ID
  80. void removeDrag(long id) {
  81.   AttractionBehavior att=activeAttractors.get(id);
  82.   if (att!=null) {
  83.     // mark for removal
  84.     killQueue.offer(att);
  85.     activeAttractors.remove(id);
  86.   }
  87. }
  88.  
  89. //The TUIO functions calling these initDrag(), dragging() and removeDrag() functions also need to be updated to pass the ID of that cursor, i.e.:
  90.  
  91. //Copy code
  92. // called when a cursor is added to the scene
  93. void addTuioCursor(TuioCursor tcur) {
  94.   initDrag(tcur.getSessionID(), tcur.getScreenX(width), tcur.getScreenY(height) );  // INIT DRAG
  95.   println("add cursor "+tcur.getCursorID()+" ("+tcur.getSessionID()+ ") " +tcur.getX()+" "+tcur.getY());
  96. }
  97.  
  98. // called when a cursor is moved
  99. void updateTuioCursor (TuioCursor tcur) {
  100.   dragging(tcur.getSessionID(), tcur.getScreenX(width), tcur.getScreenY(height) );
  101.   println("update cursor "+tcur.getCursorID()+" ("+tcur.getSessionID()+ ") " +tcur.getX()+" "+tcur.getY()
  102.     +" "+tcur.getMotionSpeed()+" "+tcur.getMotionAccel());
  103. }
  104.  
  105. // called when a cursor is removed from the scene
  106. void removeTuioCursor(TuioCursor tcur) {
  107.   removeDrag(tcur.getSessionID());
  108.   println("remove cursor "+tcur.getCursorID()+" ("+tcur.getSessionID()+")");
  109.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement