Instead of AdBlock, enjoy ad-free CAN by becoming a member. Everybody wins!

Kinect Physics Tutorial for Processing

In this hands-on tutorial I will walk you through the steps needed to turn YOU into an interactive virtual polygon. How awesome is that? To realise our creative end goal we will be using Processing and several of it’s contributed libraries. Obviously I will not be covering all the aspects or possibilities of these libraries. Rather this tutorial focuses on the specific steps needed to reach our proposed creative goal: letting you interact with the geometry on your screen in realtime. I will share three fully commented code examples of increasing complexity that’ll give you a flying start. Of course from there, many more opportunities for further exploration will arise.

Get kinected
You will need a Kinect for this tutorial. While it is definitely possible to distill blobs from the image of a regular webcam, the Kinect makes separating a person from the background really easy. There are a lot of fun things you can do with a Kinect, from point clouds to 3D skeletons to hand-tracking… but those have to wait for another time! ;-) In this tutorial we will be using the Kinect as a very effective silhouette generator.

Installing Processing & libraries
There are over 100 libraries that extend the capabilities of Processing in areas like sound, video, computer vision, data import/export, math, geometry, physics and many more things. Processing 2.0 will feature a brand new system that facilitates a one-click method to install and update libraries. However for now, installation of libraries is still done manually. In most cases this is fairly straightforward though. For detailed instruction on how to install contributed libraries, check this page. Below is the list of all the libraries we will be using in this tutorial. Click on the names to go to the respective download locations. Each code example also indicates which libraries are required to run it. The easiest way to check if a library has been succesfully installed, is to go to Sketch > Import Library and see if the library shows up on the list of contributed libraries. Make sure to restart the PDE after installing a library, because this list is only refreshed at startup. If somehow problems persist, please use the Processing forum for support. Beyond this point I will be assuming necessary libraries have been installed.

- Processing 2.0b3 or 1.5.1 (examples tested & working with both)
- SimpleOpenNI: A simple OpenNI wrapper for processing
- v3ga blob detection library
- Toxiclibs 020 (examples tested & working with 021 as well)
- PBox2D: A simple JBox2D wrapper library

Example 1: Kinect Silhouette (libs: SimpleOpenNI)
Let’s start with a very basic example that only requires the SimpleOpenNI library, which might be the most problematic to install given the external depencies. I can recommend following the provided installation instruction closely. This is a very short and very basic code example to make sure the Kinect and the SimpleOpenNI library are both installed and working correctly. If everything works as intended, when running the first code example you should see a colored silhouette of yourself as you stand in front of the Kinect camera (as shown above).


Example 2: Kinect Flow (libs: SimpleOpenNI, v3ga)
This second example takes things a little further. To turn a colored silhouette into a polygon, we need to first find it’s outer contour points and second find the correct order in which they form a polygon. To find the contour points of shapes, we can use blob detection. I’ve looked at several blob detection libraries and found v3ga to work quite well. First, it’s fast, which is important because you want things to run in real-time. Second, it returns an array of ordered points. While not polygon-worthy yet, it’s an important step towards a correctly formed polygon. I’ve compressed all the relevant code into the createPolygon() method of the PolygonBlob class. I’ve found the code to work quite well in turning a blob into points into a correct polygon. But of course, there may be room for improvement. Note that both the second and third example have been written for one person in front of a Kinect, but quick tests show that the code holds up reasonably well (although not perfect) for multiple persons.

There are many things you could do (besides 2d physics, we’ll get to that in a minute) once you have turned yourself into a quality polygon. I added a few experiments of mine at the end of the video. Some of the things you see there are triangulating the shape, voronoi-ifying the screen and using myself as a mask between two full HD videos (the originals can be found on my vimeo account). These preliminary experiments are just the tiny tip of the iceberg. There are so many things you could do. Which makes it all the more important that you think about what you want to create and which visual style you are after. In this code example the polygon shape is used to control the boundaries of a noise-based flow of particles. The code is simplified as much as possible and fully commented, so feel free to run and read the code to see what it does exactly. You will need the main sketch and the two classes.

Main Sketch

Particle class

PolygonBlob class


Example 3: Kinect Physics (libs: SimpleOpenNI, v3ga, Toxiclibs, PBox2D)
All right, now for the pièce de résistance of this tutorial. Realtime interaction with onscreen geometry. To do this we will be using Daniel Shiffman’s PBox2D library, which is a Processing wrapper library for the Jbox2D library, which is a port of the C++ Box2D library. Gotta love open source! ;-) This code example builds on the work done in the previous example. The createPolygon() method is nearly identical. The PolygonBlob class is now extending Toxiclibs’ Polygon2D class. The main difference is that code has been added to create and destroy box2d bodies (which will allow 2D physics).

What this code does in words: create a silhouette from a person, turn this silhouette into points, turn these points into a polygon, turn this polygon into a deflective shape in the box2d physics world. Because all the other geometry is transferred into the same physics world, interaction between person and onscreen geometry becomes possible. Of course there are some challenges, in addition to the mentioned steps. For example, while the original virtual geometry is pretty solid, the person’s contours are ever changing, especially when the person is moving around a lot. This will result in errors in the collision detection (aka geometry will end up inside a person). That is also one of the reasons to use Toxiclibs, because it has methods that help us check if a point is inside a polygon and if so, to find the closest point on the outer contours and move the geometry to that point. It’s a bit of a quick workaround, but it works quite well. When moving slowly or not at all, this workaround isn’t used anyway, since the regular collision detection will be effective under those conditions. However if a person is moving around quickly and you don’t want geometry inside the person nor delete it, then this shortcut is useful. Although at times it may cause some non-physically correct overlapping of shapes near the contours. All in all, you can see in the video that using this combination of techniques allows you to interact with the falling shapes on your screen. Again, to run the code you will need the main sketch and the two classes.

Main Sketch

CustomShape class

PolygonBlob class

This is a hands-on tutorial so feel free to run the code, tweak it, read the comments to learn what it does exactly and perhaps make some other cool stuff with it. Either physics-based or just using the polygon itself to create some interesting visual experiments as I did at the end of my video. Good luck and happy coding! :D

  • Amnon P5

    Replacing the background with a video feed is easy, just place the incoming image at the beginning of draw after/instead of the background call. Replacing the blob/person with a video feed is a bit harder, because you have to use masking. At a higher resolution, this is best done with GLSL shaders to maintain a good framerate (see the GLGraphics library).

  • Amnon P5

    The first sketch consists only of a main sketch. The other two examples consist of a main sketch plus two classes, one of which is the PolygonBlob class. As stated in the blog post, you will need the main sketch and the two classes to run the code. The two classes are also posted as source code above. So for the last two examples you will need to copy-paste three things (the main sketch and two classes) into the PDE.

  • Gabriel Axel

    Yes, thank you :]

    The Physics sketch works perfectly. However, on my end the Flow sketch does not reset the background; in effect, the background remains colored with particles and therefore it is difficult to see the figure. The sketch and the two classes were copied. Any ideas?

  • PokkaGT

    I looked through the examples in the GLGraphics library. Isn’t masking done using GLTexture?

    Alternatively, if using a video feed is too hard for me, the Kinect’s RGB camera is not THAT bad…

  • Amnon P5

    Yes. See the example GLGraphics > Textures > DynamicMask

  • Amnon P5

    What version of Processing are you using?

  • Gabriel Axel

    2.0b6. When I use 2.0b3, I get: “Error: This Java instance does not support a 32-bit JVM.”

  • Gabriel Axel

    2.0b6. When I use 2.0b3, I get : “Error: This Java instance does not support a 32-bit JVM.” I hope there is a way to fix this on b6…

  • gabrielaxel

    I am using Mac OSX 10.8.2. I installed Oracle Java 7, then uninstalled it and installed Java for OSX 2012-006 using this guide (http://support.apple.com/kb/HT5559?viewlocale=en_US) . Not sure if something may have gone awry in this process.

  • gabrielaxel

    …Although when I type “java -version” into Terminal, I get:
    Java(TM) SE Runtime Environment (build 1.7.0_09-b05)
    Java HotSpot(TM) 64-Bit Server VM (build 23.5-b02, mixed mode)

  • gabrielaxel

    When enter “java – version” in Terminal, I get:
    Java(TM) SE Runtime Environment (build 1.7.0_09-b05)
    Java HotSpot(TM) 64-Bit Server VM (build 23.5-b02, mixed mode)

  • Brendan Flynn

    cheers dude had a good look through program, and love all that you can change, blobbing effect is fun. Can I ask you a question, I will just copy and paste the question how i wrote it on other forums “if i wanted to create the aesthetics of this video http://www.youtube.com/watch?v=H5dh0FGbIgg&list=PLE6A2F3ACDDA10C28&index=19 does anyone have any learned ideas of how I could?” dude, dont worry too much if you dont know.

  • Amnon P5

    Well, you have a couple of options. The easiest is to use Processing 1.5.1. since this tutorial was tested + working with that version. Another is to figure out what goes wrong with 2.0b6. At the top of draw() is the fading background code that is apparantly not working for you in 2.0b6. I can’t reproduce your problem. The fading background code works for me in 2.0b6.

  • Amnon P5

    There are a lot of different effects in that video, but since your question has no relation to this tutorial my advice is to ask elsewhere. If you want to recreate the aesthetics in Processing, then the general discussion section of the forum may be a good place.

  • brendan flynn

    cool dude, fair answer and have, i actually already know somewhere in the brain, just must search it tomorrow when, its empty of “bushmills”. Also have done so in the general discussion section. thanks man.

  • Alex

    This is the most helpful source for using processing and Kinect that I have found! Thank you so much! It’s really hard to find useful, well documented code like this.

    I have a question, though. I am really, really new to processing, and I’d like to just be able to create a silhouette polygon like in the second example but not have the particles and just fill the polygon with a solid color. I’m having a lot of trouble trying to figure out how everything links together, the classes and the functions.

    I’m sure it’s just a matter of calling like a couple of different functions that don’t use the particle class but I have no idea how to do it. Every time I change something the program doesn’t work.

    How do I delete the particles and just create the polygon?

    Sorry for the really basic question, it’s probably really obvious but I feel extremely lost not having that much experience with processing.

    Thanks,
    Alex

  • Amnon P5

    Add this method to the PolygonBlob class:


    void display() {
    stroke(0);
    fill(255);
    beginShape();
    for (int i=0; i<npoints; i++) { vertex(xpoints[i], ypoints[i]); }
    endShape(CLOSE);
    }

    Then in the drawFlowfield() method remove the code to “update and display all particles” and replace it by: poly.display();

  • augustoamaral

    My kinect is eat my whole memory… what can i do ?

  • AndrewR

    Were you able to replace the background with a video capture? I’m trying to do the same thing and don’t seem to be able to get it to work…

  • AndrewR

    How would I go about replacing the background with a live video feed for the last example?

  • Lucianio

    i have a problem, when run processing in the Example 1: Kinect Silhouette (libs: SimpleOpenNI) say “SimpleOpenNI Version 0.27
    Kinect not connected! ”
    I tried everything, I have xbox kinect, works well with other programs such as kinectpaint but the sketch of the first example throws me the error that I do not recognize the kinect, is because it is a xbox kinect? thank you very much would appreciate information, I am very excited about this but I can not continue because I do not recognize my kinect

  • http://www.facebook.com/claudia.gaete2 Claudia Gaete

    Thanks for the code…
    Can you make an example using geomerative??..

  • http://www.facebook.com/claudia.gaete2 Claudia Gaete

    Thanks for the code…
    Can you make an example using geomerative??..

  • Hali

    awesome!
    thanks

  • Sander

    Somebody else having problems with memory ? I gave sketchbook 1gig shouldn’t that be enough ?

  • Sander

    I mean I gave processing 1gig.

  • mouzone

    hey Amnon! Thanks for sharing this awesome tutorial!

    Could you help me out with the 2nd example: All i want is to set the bg as just 1 color without the fading — (let’s take black for example, but all the “flying” particles should stay in different colors, like the example. How can this be done?

    2nd question: what’s that for a color scheme? or can i just use hex colors?

  • http://www.facebook.com/fernandorosenblit Fernando Rosenblit

    hi! is it posible to attach an image to a body? like fisica library. How can I do it? thank you

  • Sascha

    Thanks for this tutorial! I just started playing around with the kinect sensor.
    I want to record the movements of the tracked persons. Do you think there will be an advantage to transfer the Pixels generated from the getUserPixel function into a polygon before storing these data to disk or would it be faster the store it on the pixel level?
    Is there also a way to identify the different persons in the blobs? The SimpleOpenNI library can create me an array with different identifier for the different objects.

    My first steps with the pixel based approach seems to end in a performance problem on my current Core2Duo Machine…

  • Hanjo

    Had the same Problem with SimpleOpenNI and Avins SensorKinectDriver using a Kinect for Windows(!). make sure you are using either x86/x64 Versions of all software involved (Driver,OpenNI,NITE,Processing and of course Java). the 64bit versions run like a charm on my win7-machine and processing 2.06b