Code Examples
Osceleton Part of sketch Code
//import libraries so communication can happen with Kinect as well as be able to handle
//movie files
import processing.opengl.*;
import oscP5.*;
import netP5.*;
PFont f;
OscP5 oscP5;
int userCount=6; //kinect max 7
float X;
float Y;
float Z;
float xmap;
float ymap;
float zmap;
float maxZ = 100.0;
//make an array of users
User [] myUsers = new User[userCount];//make array of users to hold a string 0-6
int closestUser=1000;
void setup() {
size(screen.width, screen.height);
f= loadFont(“Serif-48.vlw”);
//load font for de-bugging
oscP5 = new OscP5(this, “127.0.0.1″, 7110);
//check each user in turn, first user 0 then user 1 etc
for (int i=0;i<myUsers.length;i++) {
myUsers[i]= new User(0, 0, 0);
}
frameRate(30);
}
void oscEvent(OscMessage msg) {
//println(“msg results “+msg.checkAddrPattern(“/user/”+str(1)) );
//closestZ = maxZ;//resetting Z
for ( int i=1; i <= userCount; i++ ) {
if (msg.checkAddrPattern(“/user/”+str(i))) {
//i must be out as if not it will bugger up message)
//need to create variable of maximum users
X = (msg.get(0).floatValue());
Y= (msg.get(1).floatValue());
Z = (msg.get(2).floatValue());
xmap = map(X, 0, 1, 0, width);
ymap = map(Y, 0, 1, 0, height);
zmap = map(Z, 0, 3.5, 0, maxZ);
myUsers[i-1].update(xmap, ymap, zmap);
//beneath not needed but kept in case
/* if ( zmap > 0.0 && zmap < closestZ ) {//using greater than zero as when kinect loses user it prints 0.0
// if( i != closestUser ) { println(“Found new user ” + i ); }
closestUser = i;//will update with loop
closestZ = zmap;//closestUser z mapped to 100 for ease
myUsers[i-1].update(xmap, ymap, zmap);
*/
}
}
}
void draw() {
background(0);
textFont(f);
returnClosestUser();
int yPos = 50;
for (int i=0;i<myUsers.length;i++) {
if (i == closestUser) {//-1 because an array so User 1 is actually 0
fill (125);
}
else {
fill(255);
}
//print zvalues to screen
//text(i + ” = ” + myUsers[i].z, 10, yPos);
text(i+” “+myUsers[i].isAlive,10,yPos);
yPos += 50;
myUsers[i].drawUserEllipse();
}
yPos += 50;
text(“closest User is “+closestUser, 10, yPos);
}
//a function which checks which user is closest and returns the index of that user eg.
//which number in the array of users they are
void returnClosestUser() {
//int indexOfClosestUser=100;
//go throught the list of users
//int localClosestUser=100;
float closestZ=1000;
for (int i=0;i<myUsers.length;i++) {
//check their zpos against the current closestZ
//if it’s less than this but isn’t 0 then…
if (myUsers[i].z<closestZ&&myUsers[i].z!=0) {
println(“got closest Users which is now ” + i);
//make this the new closest z
closestZ=myUsers[i].z;
//and this is the index of the new closest user
closestUser=i;
}
}
}
User Class Code
class User {
float x, y, z;
//previous values of x,y,z – we will use these to see if this person has moved since last
//frame or not
float px, py, pz;
int timer;
boolean isClosestUser;
boolean isAlive;
User (float lx, float ly, float lz) {
px=x=lx;
py=y=ly;
pz=z=lz;
isAlive=false;
timer=0;
}
void update(float lx, float ly, float lz) {
x=lx;
y=ly;
z=lz;
//if nothing has changed since the last frame
if (px==x&&py==y&&pz==z) {
timer++;
}
//if we are moving we are also alive
else{
timer=0;
isAlive=true;
}
//how many frames someone needs to stay still for before we consider them dead
int timeOutThreshold=60;
if (timer>=timeOutThreshold) {
isAlive=false;
timer=0;
}
px=x;
py=y;
pz=z;
}
void drawUserEllipse() {
ellipse(x, y, z, z);
//println(“values of user data are : “+x+” “+y);
}
}
Pimage Code for Video Transitions
//getting invalid memory error or set a breakpoint in malloc_error_break to debug
import processing.video.*;
Movie movieBottom;
Movie movieMiddle;
Movie movieTop;
PImage TopAlpha ;
PImage MiddleAlpha;
int transparencyTop;
int transparencyMiddle;
float playheadBottom;
float playheadTop;
float playheadMiddle;
void setup() {
size(1024, 576, P3D);
TopAlpha = createImage(width, height, ARGB);
MiddleAlpha = createImage(width, height, ARGB);
movieTop = new Movie(this, “Close1-MAgicTest PhotoGood.mov”);
movieMiddle = new Movie(this, “trimmedTest-MAgicTest PhotoGood.mov”);
movieBottom = new Movie(this, “DMTimeLapse2-MAgicTest PhotoGood-MAgicTest PhotoGood.mov”);
movieBottom.play();
movieTop.play();
movieMiddle.play();
}
void draw() {
println (mouseY);
background(0);
if(mouseY<101){
image (movieTop, 0, 0);
playheadTop = map(mouseX, 0, width, 0, movieTop.duration());
movieTop.jump(playheadTop);
println (“playingTop”);
}
else if ((mouseY<170)&&(mouseY>100)) {//next zone <(height/3) && <(height/3)*2
println(“blending”);
transparencyTop = (int) map(mouseY, 170, 100, 0.0, 255.0);
movieTop.loadPixels();
TopAlpha.loadPixels();
int x=0;
int y=0;
for (int i = 0; i < TopAlpha.pixels.length; i++) {
color myColor = movieTop.get(x, y);
TopAlpha.pixels[i]= color(myColor, transparencyTop);
x++;
if (x>=width) {
x=0;
y++;
}
}
updatePixels();
image (movieMiddle, 0, 0);
image(TopAlpha, 0, 0);
playheadMiddle = map(mouseX, 0, width, 0, movieMiddle.duration());
playheadTop = map(mouseX, 0, width, 0, movieTop.duration());
movieMiddle.jump(playheadMiddle);
movieTop.jump(playheadTop);
}
else if ((mouseY>150)&& (mouseY<251)){
image (movieMiddle, 0, 0);
playheadMiddle = map(mouseX, 0, width, 0, movieMiddle.duration());
movieMiddle.jump(playheadMiddle);
println(“playingMiddle”);
}
else if ((mouseY > 250 ) && (mouseY<385)) {
println(“blending2″);
transparencyMiddle = (int) map(mouseY, 385, 250, 0.0, 255.0);
movieMiddle.loadPixels();
MiddleAlpha.loadPixels();
int x=0;
int y=0;
for (int i = 0; i < MiddleAlpha.pixels.length; i++) {
color myColor = movieMiddle.get(x, y);
MiddleAlpha.pixels[i]= color(myColor, transparencyMiddle);
x++;
if (x>=width) {
x=0;
y++;
}
}
updatePixels();
image (movieBottom, 0, 0);
image(MiddleAlpha, 0, 0);
playheadBottom = map(mouseX, 0, width, 0, movieBottom.duration());
playheadMiddle = map(mouseX, 0, width, 0, movieMiddle.duration());
movieBottom.jump(playheadBottom);
movieMiddle.jump(playheadMiddle);
}
else if (mouseY >384) {
println (“playing bottom”);
image (movieBottom, 0, 0);
playheadBottom = map(mouseX, 0, width, 0, movieBottom.duration());
movieBottom.jump(playheadBottom);
}
}
/*still working on this to develop states according to z so that the system knows what to do and will save it getting confused/ memory leaks. Also still developing the if esle and true false statements to allow the system to access video library in correct order*/
Experimentations with 3 layers have now been left as memory and processer “heaviness” have caused numerous obstacles to overcome that with time and coding ability I believe are beyond me and detrimental to progression.