Need particles to track center of mass rather than mouse

edited May 2014 in Kinect

So my problem is hopefully an easy fix I'm just really new to processing and pretty much have no idea what I'm doing. I'm trying to combine the Punktiert library flocking and Tuio library with Kinect to track the center of mass. The Punktiert is originally mouse tracked but I want it to track the center of mass being tracked through Tuio. I've tried replacing mouseX, mouseY to cur.getScreenX(width), cur.getScreenY(height) but that doesn't seem to work. Right now with the code has the flock just going to the bottom corner, I think because of mouse = new Vec(width, height); in Setup, because when I change width, height to a pixel location it changes where the swarm goes. This could be completely wrong though. I'd appreciate any help!

    // Punktiert is a particle engine based and thought as an extension of Karsten Schmidt's toxiclibs.physics code. 
    // This library is developed through and for an architectural context. Based on my teaching experiences over the past    couple years. (c) 2012 Daniel Köhler, daniel@lab-eds.org

    //here: seek (part of flocking) function as behavior

    import punktiert.math.Vec;
    import punktiert.physics.*;

    import java.util.Vector;
    import TUIO.*;

    TuioProcessing tuioClient;



    import toxi.geom.*;
    import toxi.physics2d.*;
    import toxi.physics2d.behaviors.*;
    //world object
    VPhysics physics;

    VerletPhysics2D physics2;
    AttractionBehavior mouseAttractor;

    Vec mouse;
    Vec2D mousePos;
    //number of particles in the scene
    int amount = 1000;

    public void setup() {
      size(800, 600);
      smooth();
      fill(255, 255);

      physics = new VPhysics();
      physics.setfriction(2f);



        mouse = new Vec(width, height);




      loop();
      frameRate(60);

      tuioClient = new TuioProcessing(this);

      for (int i = 0; i < amount; i++) {
        //val for arbitrary radius
        float rad = random(2, 10);
        //vector for position
        Vec pos = new Vec (random(rad, width-rad), random(rad, height-rad));
        //create particle (Vec pos, mass, radius)
        VParticle particle = new VParticle(pos, 5, rad);
        //add Collision Behavior
        particle.addBehavior(new BCollision());

        particle.addBehavior(new BSeek(mouse));
        //add particle to world
        physics.addParticle(particle);
      }

    }

    void addParticle() {
      VerletParticle2D p = new VerletParticle2D(Vec2D.randomVector().scale(5).addSelf(width / 2, 0));
      physics2.addParticle(p);
      // add a negative attraction force field around the new particle
      physics2.addBehavior(new AttractionBehavior(p, 20, -3.2f, 0.01f));
    }



    public void draw() {
      background(0);

      physics.update();



       Vector<TuioCursor> tuioCursorList = tuioClient.getTuioCursors();

      for (int i = 0; i < tuioCursorList.size(); i++)
      {
        TuioCursor cur = tuioCursorList.elementAt(i);
        fill(255);
        ellipse (cur.getScreenX(width), cur.getScreenY(height), 15, 15);
          mouse.set(cur.getScreenX(width), cur.getScreenY(height));
      }

       Vector<TuioObject> tuioObjectList = tuioClient.getTuioObjects();
      for (int i = 0; i < tuioObjectList.size(); i++)
      {
        TuioObject obj = tuioObjectList.elementAt(i);
        fill (255);
        rect (obj.getScreenX(width) - 30, obj.getScreenY(height) - 30, 30, 30, 10);
        mouse.set(obj.getScreenX(width), obj.getScreenY(height));
      }

      //mouse.set(cur.getScreenX(width), cur.getScreenY(height));

      for (VParticle p : physics.particles) {
        drawRectangle(p);
        mouse.set(width, height);
      }


    }


    void drawRectangle(VParticle p) {

      float deform = p.getVelocity().mag();
      //println(p.getPreviousPosition());
      //println(p);
      float rad = p.getRadius();
      deform = map(deform, 0, 1.5f, rad, 0);
      deform = max (rad *.2f, deform);

      float rotation = p.getVelocity().heading();    

      pushMatrix();
      translate(p.x, p.y);
      rotate(HALF_PI*.5f+rotation);
      beginShape();
      vertex(-rad, +rad);
      vertex(deform, deform);
      vertex(rad, -rad);
      vertex(-deform, -deform);
      endShape(CLOSE);
      popMatrix();
    }

    // called when an object is added to the scene
    void addTuioObject(TuioObject tobj) {
      println("add object "+tobj.getSymbolID()+" ("+tobj.getSessionID()+") "+tobj.getX()+" "+tobj.getY()+" "+tobj.getAngle());
    }

    // called when an object is removed from the scene
    void removeTuioObject(TuioObject tobj) {
      println("remove object "+tobj.getSymbolID()+" ("+tobj.getSessionID()+")");
    }

    // called when an object is moved
    void updateTuioObject (TuioObject tobj) {
      println("update object "+tobj.getSymbolID()+" ("+tobj.getSessionID()+") "+tobj.getX()+" "+tobj.getY()+" "+tobj.getAngle()
              +" "+tobj.getMotionSpeed()+" "+tobj.getRotationSpeed()+" "+tobj.getMotionAccel()+" "+tobj.getRotationAccel());
    }

    // called when a cursor is added to the scene
    void addTuioCursor(TuioCursor tcur) {
      println("add cursor "+tcur.getCursorID()+" ("+tcur.getSessionID()+ ") " +tcur.getX()+" "+tcur.getY());
    }

    // called when a cursor is moved
    void updateTuioCursor (TuioCursor tcur) {
      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) {
      println("remove cursor "+tcur.getCursorID()+" ("+tcur.getSessionID()+")");
    }

    // called after each message bundle
    // representing the end of an image frame
    void refresh(TuioTime bundleTime) { 
    }
Sign In or Register to comment.