Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import processing.opengl.*;
- import toxi.geom.*;
- import toxi.physics2d.*;
- import toxi.physics2d.behaviors.*;
- import java.util.concurrent.*;
- int NUM_PARTICLES = 500;
- VerletPhysics2D physics;
- // these queues are needed to add/remove behaviors when working with multiple threads
- AbstractQueue<ParticleBehavior2D> addQueue=new ConcurrentLinkedQueue<ParticleBehavior2D>();
- AbstractQueue<ParticleBehavior2D> killQueue=new ConcurrentLinkedQueue<ParticleBehavior2D>();
- // this map will link attractors to their TUIO cursor IDs
- HashMap<Long, AttractionBehavior> activeAttractors=new HashMap<Long, AttractionBehavior>();
- void setup() {
- size(680, 382, OPENGL);
- // setup physics with 10% drag
- physics = new VerletPhysics2D();
- physics.setDrag(0.05f);
- physics.setWorldBounds(new Rect(0, 0, width, height));
- // the NEW way to add gravity to the simulation, using behaviors
- // physics.addBehavior(new GravityBehavior(new Vec2D(0, 0)));
- // init the TUIO protocol
- setupTUIO();
- }
- void addParticle() {
- VerletParticle2D p = new VerletParticle2D(Vec2D.randomVector().scale(5).addSelf(width / 2, height/2));
- physics.addParticle(p);
- // add a negative attraction force field around the new particle
- physics.addBehavior(new AttractionBehavior(p, 20, -1.2f, 0.01f));
- }
- void draw() {
- // first process the queues
- while (killQueue.peek() != null) {
- physics.removeBehavior(killQueue.poll());
- }
- while (addQueue.peek() != null) {
- physics.addBehavior(addQueue.poll());
- }
- background(0, 0, 0);
- noStroke();
- fill(255);
- if (physics.particles.size() < NUM_PARTICLES) {
- addParticle();
- }
- physics.update();
- stroke(255);
- for (VerletParticle2D p : physics.particles) {
- ellipse(p.x, p.y, 10, 10);
- }
- drawTUIO(); // call the TUIO function
- }
- // create a new attractor around the given position, also requires a TUIO cursor ID
- void initDrag(long id, int posX, int posY) {
- AttractionBehavior att = new AttractionBehavior(new Vec2D(posX, posY), 250, 0.9f);
- // add to queue
- addQueue.offer(att);
- // and to hash map for future reference
- activeAttractors.put(id, att);
- }
- // updates the attractor position for the given TUIO cursor ID
- void dragging(long id, int posX, int posY) {
- AttractionBehavior att=activeAttractors.get(id);
- if (att!=null) {
- att.getAttractor().set(posX, posY);
- }
- }
- // removes the attractor for the given TUIO cursor ID
- void removeDrag(long id) {
- AttractionBehavior att=activeAttractors.get(id);
- if (att!=null) {
- // mark for removal
- killQueue.offer(att);
- activeAttractors.remove(id);
- }
- }
- //The TUIO functions calling these initDrag(), dragging() and removeDrag() functions also need to be updated to pass the ID of that cursor, i.e.:
- //Copy code
- // called when a cursor is added to the scene
- void addTuioCursor(TuioCursor tcur) {
- initDrag(tcur.getSessionID(), tcur.getScreenX(width), tcur.getScreenY(height) ); // INIT DRAG
- println("add cursor "+tcur.getCursorID()+" ("+tcur.getSessionID()+ ") " +tcur.getX()+" "+tcur.getY());
- }
- // called when a cursor is moved
- void updateTuioCursor (TuioCursor tcur) {
- dragging(tcur.getSessionID(), tcur.getScreenX(width), tcur.getScreenY(height) );
- println("update cursor "+tcur.getCursorID()+" ("+tcur.getSessionID()+ ") " +tcur.getX()+" "+tcur.getY()
- +" "+tcur.getMotionSpeed()+" "+tcur.getMotionAccel());
- }
- // called when a cursor is removed from the scene
- void removeTuioCursor(TuioCursor tcur) {
- removeDrag(tcur.getSessionID());
- println("remove cursor "+tcur.getCursorID()+" ("+tcur.getSessionID()+")");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement