Interactive Particle System using Kinect, Pbox2d and edge detection
in
Contributed Library Questions
•
1 year ago
Hiii, I am doing this project where I am using kinect(SimpleOpenNI library) and pbox2d library to get the interactive particles which is similar to this (
this). I am using kinect along with the other blobdetection library(blobscanner) for edge Detection and pbox2d to create particle world. The particles will fall from the the above and if the particle falls on the detected edge it will move in other direction. So far I have created the particle system and the kinect depthImage with contour around the depthImage by kinect.
Below is the code. Now I want to add edge detection and use the interactivity to the particles. Please guide me.
- import Blobscanner.*;
- import SimpleOpenNI.*;
- import pbox2d.*;
- import org.jbox2d.collision.shapes.*;
- import org.jbox2d.common.*;
- import org.jbox2d.dynamics.*;
- // A reference to our box2d world
- PBox2D box2d;
- // An ArrayList of particles that will fall on the surface
- ArrayList<Particle> particles;
- float noiseIter = 0.0;
- int ballSize = 10;
- SimpleOpenNI context;
- int a = 30;
- PImage img;
- Detector bs;
- // NITE
- XnVSessionManager sessionManager;
- XnVFlowRouter flowRouter;
- void setup()
- {
- size(640, 480);
- context = new SimpleOpenNI(this);
- // mirror is by default enabled
- context.setMirror(true);
- // enable depthMap generation
- context.enableDepth();
- // enable the hands + gesture
- context.enableGesture();
- context.enableHands();
- box2d = new PBox2D(this);
- box2d.createWorld();
- // We are setting a custom gravity
- box2d.setGravity(0, -40);
- // Create the empty list
- particles = new ArrayList<Particle>();
- bs = new Detector( this, 0, 0, width, height, 255 );
- // setup NITE
- sessionManager = context.createSessionManager("Click, Wave", "RaiseHand");
- flowRouter = new XnVFlowRouter();
- sessionManager.AddListener(flowRouter);
- //size(context.depthWidth(), context.depthHeight());
- }
- void draw()
- {
- background(200, 0, 0);
- // update the cam
- context.update();
- // update nite
- context.update(sessionManager);
- img = context.depthImage();
- img.loadPixels();
- // draw depthImageMap
- img.filter(THRESHOLD);
- image(context.depthImage(), 0, 0);
- bs.findBlobs(img.pixels, img.width, img.height);
- // bs.loadBlobsFeatures();
- bs.drawContours(color(0, 0, 255), 2);
- /* if (random(1) < 0.2) {
- float sz = random(4,8);
- particles.add(new Particle(width/2,-20,sz));
- }
- */
- particles.add(new Particle(noise(noiseIter)*width,
- 0,random(4,8)));
- noiseIter += 0.01;
- // We must always step through time!
- box2d.step();
- // Look at all particles
- for (int i = particles.size()-1; i >= 0; i--) {
- Particle p = particles.get(i);
- p.display();
- // Particles that leave the screen, we delete them
- // (note they have to be deleted from both the box2d world and our list
- if (p.done()) {
- particles.remove(i);
- }
- }
- }
- class Particle {
- // We need to keep track of a Body and a radius
- Body body;
- float r;
- color col;
- Particle(float x, float y, float r_) {
- r = r_;
- // This function puts the particle in the Box2d world
- makeBody(x,y,r);
- body.setUserData(this);
- col = color(255,0,0);
- }
- // This function removes the particle from the box2d world
- void killBody() {
- box2d.destroyBody(body);
- }
- // Change color when hit
- void change() {
- col = color(0,0,255);
- }
- // Is the particle ready for deletion?
- boolean done() {
- // Let's find the screen position of the particle
- Vec2 pos = box2d.getBodyPixelCoord(body);
- // Is it off the bottom of the screen?
- if (pos.y > height+r*2) {
- killBody();
- return true;
- }
- return false;
- }
- //
- void display() {
- // We look at each body and get its screen position
- Vec2 pos = box2d.getBodyPixelCoord(body);
- // Get its angle of rotation
- float a = body.getAngle();
- pushMatrix();
- translate(pos.x,pos.y);
- rotate(a);
- fill(col);
- stroke(0);
- strokeWeight(1);
- ellipse(0,0,r*2,r*2);
- // Let's add a line so we can see the rotation
- line(0,0,r,0);
- popMatrix();
- }
- // Here's our function that adds the particle to the Box2D world
- void makeBody(float x, float y, float r) {
- // Define a body
- BodyDef bd = new BodyDef();
- // Set its position
- bd.position = box2d.coordPixelsToWorld(x,y);
- body = box2d.world.createBody(bd);
- // Make the body's shape a circle
- CircleDef cd = new CircleDef();
- cd.radius = box2d.scalarPixelsToWorld(r);
- cd.density = 1.0f;
- cd.friction = 0.01f;
- cd.restitution = 0.3f; // Restitution is bounciness
- body.createShape(cd);
- // Always do this at the end
- body.setMassFromShapes();
- // Give it a random initial velocity (and angular velocity)
- //body.setLinearVelocity(new Vec2(random(-10f,10f),random(5f,10f)));
- body.setAngularVelocity(random(-10,10));
- }
- }
1