// library for easy manipulation of cameras // The Obsessive Camera Direction // by Kristian Linn Damkjer // !! you need to download it to run this code !! // find link at http://www.processing.org/reference/libraries/index.html import damkjer.ocd.*; // create camera from OCD library Camera camera1; float camera1Speed = 1; boolean cameraRunning = false; // max nodes, 25*25 for example int nodeAmount = 625; // nodes array: x,y,z,force float[][] nodes = new float[int(nodeAmount)][4]; // rotationZ float[] rotationY = new float[int(nodeAmount)]; // rotationZ Target float[] rotationTargetY = new float[int(nodeAmount)]; // counter for timing int timingCounter = 0; int timingCounterSpeed = 1; float rotationCounter = 0.0; float iterateRotationSpeed = 4.0; // String displayAnimation = "pong"; String shapeRendered = "boxfold"; String FONT_NAME = "TrebuchetMS-12.vlw"; // image loading setup PImage a; PImage b; int imageSize = 25; int[][] aPixels; int currentImage = 1; int maxImage = 20; String animation = "count"; int frameRate = 1; // perlin noise setup int noiseDepth = 1; // pong setup int pongStageX = 6*25; int pongStageZ = 6*25; float ballX = 50; // ball X coordinate float ballZ = 50; // ball Y coordinate float ballSpeed = 10; // ball Speed value float ballDirection = 145; // direction of the ball float ballSpeedX = sin(radians(ballDirection)); float ballSpeedZ = cos(radians(ballDirection)); float ballSize = 20; float racketSizeX = 30; float racketSizeZ = 3; float racketSizeY = 5; float racketX = 60; float racketZ = 150; // array for pixelating pong results int pongFieldSize = 25; float pongFieldPixels[] = new float[pongFieldSize*pongFieldSize]; // setup *********************************************************************************** void setup() { size(800, 500, P3D); colorMode(HSB, 100); framerate(15); cursor(CROSS); generateNodes(); //smooth(); // little buggy camera constructor from OCD library /* Camera(parent, cameraX, cameraY, cameraZ, targetX, targetY, targetZ, upX, upY, upZ, fov, aspect, nearClip, farClip) */ camera1 = new Camera(this, 15.0, -10.0, 130.0, 75.0, -10.0, 75.0, 0.0, 1.0, 0.0, radians(PI/3), (width/height), 1.0, 1000.0); camera1.zoom(radians(130) / 1.7); b = loadImage("background.jpg"); // set up the font we use PFont metaBold; metaBold = loadFont(FONT_NAME); textFont(metaBold); } // main method *********************************************************************************** void draw() { background(20,0,40); //background(b); camera1.feed(); cameraNavigation(); iterateRotation(); // draw shapes if (shapeRendered == "boxfold") { //~ lights(); directionalLight(100, 0, 100, 7, -2, 5); ambientLight(0, 0, 30); drawBoxFold(); } else if (shapeRendered == "cone"){ lights(); drawCone(); } else if (shapeRendered == "stick"){ lights(); drawStick(); } else if (shapeRendered == "umbrella"){ lights(); drawUmbrella(); } else if (shapeRendered == "flower"){ lights(); drawFlower(); } else if (shapeRendered == "wings"){ lights(); drawWings(); } // content timingCounter += 1; if (timingCounter>timingCounterSpeed){ timingCounter = 0; if (displayAnimation == "images"){ loadMyImage(); displayMyImage(); } else if (displayAnimation == "noise"){ displayPerlinNoise(); } else if (displayAnimation == "random"){ // done } else if (displayAnimation == "reset"){ // done } else if (displayAnimation == "pong"){ pongBounceBall(); } } else { if (displayAnimation == "pong"){ pongBounceBall(); } } displayStatus(); } // generate positions ********************************************************** void generateNodes() { float x = 0; float y = 0; float z = 0; int j=0; for (int i=0; i24) { j = 0; x = 0; z += 6; } float humanizer = (random(10)-5)/20; x += 6+humanizer; z += humanizer; nodes[i][0] = x; nodes[i][1] = y; nodes[i][2] = z; // and force nodes[i][3] = 0; } } // Calculate forces ************************************************************ void iterateRotation(){ for(int i=0; i= pongStageX || ballX <= 0){ ballSpeedX = -ballSpeedX; } else if (ballZ <= 0){ ballSpeedZ = -ballSpeedZ; } // reset game after it leaves if (ballZ >= pongStageZ+100){ ballX = 50; // ball X coordinate ballZ = 50; // ball Y coordinate ballSpeed = 5; // ball Speed value ballDirection = 145; // direction of the ball ballSpeedX = sin(radians(ballDirection)); ballSpeedZ = cos(radians(ballDirection)); } // check for racket if (ballX >= racketX - (racketSizeX*1.5) && ballX <= racketX && ballZ >= racketZ - (ballSize / 2) && ballZ <= racketZ){ ballSpeedZ = -ballSpeedZ; } // move ball ballX += ballSpeedX*ballSpeed; ballZ += ballSpeedZ*ballSpeed; // place results in array for (int i = 0; i < pongFieldSize; i++){ for (int j = 0; j < pongFieldSize; j++){ int pixelPos = i + (pongFieldSize * j); float pixelSize = pongStageX / pongFieldSize; if ( abs( (i * pixelSize) - ballX) < ballSize/2 && abs(j * pixelSize - ballZ) < ballSize/2){ //pongFieldPixels[pixelPos] = 1; nodes[pixelPos][3] = 270; } else { //pongFieldPixels[pixelPos] = 0; nodes[pixelPos][3] = 0; } // draw walls // pongFieldPixels[i] = 1; } } // listen for racket moving //~ if(keyPressed) { //~ if(key == 'q' || key == 'Q') { //~ racketX -= 4; //~ } //~ if(key == 'w' || key == 'W') { //~ racketX += 4; //~ } //~ } // auto play racket racketX = ballX; // draw racket / people pushMatrix(); //fill(100,0,100); noFill(); stroke(0,0,100); translate(racketX-racketSizeX/2 + 40, 0, racketZ); //box(racketSizeX, racketSizeY, racketSizeZ); translate(racketSizeX/2+2, -5, 0); for (int men=0; men<6; men++){ noStroke(); translate(-5-(random(1)-0.5)/2,0,(random(1)-0.5)/2); fill(5,50,100); sphere(1); translate(0,-0.2,0.2); fill(1,10,1); sphere(1); translate(0,0.2,-0.2); translate(0,7,0); fill(65,50,70); //stroke(0,0,0); // body box(3,10,1); // hands fill(5,50,100); translate(-2,-3,0); rotateZ(radians(10)); box(0.5,4,0.5); rotateZ(radians(-20)); translate(4,1,0); box(0.5,4,0.5); rotateZ(radians(10)); translate(-2,3,0); translate(0,-7,0); } popMatrix(); } void randomForces(){ for (int i=0; i 9){ zeros = "00"; } if (currentImage > 99){ zeros = "0"; } a = loadImage(animation+zeros+currentImage+".gif"); for(int i=0; i maxImage){ currentImage = 1; if (animation == "count"){ animation = "ripple"; } else if (animation == "ripple"){ animation = "rotation"; } else if (animation == "rotation"){ animation = "hand"; frameRate = 5; maxImage = 128; } else if (animation == "hand"){ animation = "count"; frameRate = 1; maxImage = 20; } } } void displayMyImage(){ float rr, gg, bb, tt; for(int i=0; i0){ nodes[(i*imageSize)+j+fix][3] = tt-50; } } } } void displayPerlinNoise(){ float noiseScale=0.03; noiseDepth += 1; for(int i=0; i0){ nodes[(i*imageSize)+j][3] = noise(i*noiseScale, j*noiseScale, noiseDepth*noiseScale*2)*3000; } } } } // keyboardListener *************************************************************************************************************** void keyReleased(){ cameraRunning = false; // scroll trough content if(key == 'c' || key == 'C') { if (displayAnimation == "noise"){ displayAnimation = "images"; timingCounterSpeed = 5; } else if (displayAnimation == "images"){ timingCounterSpeed = 1; displayAnimation = "pong"; } else if (displayAnimation == "pong"){ displayAnimation = "noise"; randomForces(); } else if (displayAnimation == "random"){ displayAnimation = "reset"; resetForces(); } else if (displayAnimation == "reset"){ displayAnimation = "noise"; } } // scroll trough shapes //~ if(key == 's' || key == 'S') { //~ if (shapeRendered == "boxfold"){ //~ shapeRendered = "cone"; //~ } else if (shapeRendered == "cone"){ //~ shapeRendered = "stick"; //~ } else if (shapeRendered == "stick"){ //~ shapeRendered = "umbrella"; //~ iterateRotationSpeed = 70; //~ } else if (shapeRendered == "umbrella"){ //~ shapeRendered = "flower"; //~ iterateRotationSpeed = 4; //~ } else if (shapeRendered == "flower"){ //~ shapeRendered = "wings"; //~ } else if (shapeRendered == "wings"){ //~ shapeRendered = "boxfold"; //~ } //~ } } // 3d navigation *************************************************************************************************************** void keyPressed(){ cameraRunning = true; } void cameraNavigation() { //~ camera1.dolly(-0.3); //~ camera1.track(-0.3,-0.0); camera1.look(radians(mouseX - pmouseX) / 3.0, radians(mouseY - pmouseY) / 3.0); if (!mousePressed){ // } else if (mouseButton == LEFT) { //~ camera1.circle(radians(mouseX - pmouseX) / -2.0); //~ camera1.arc(radians(mouseY - pmouseY) / -2.0); camera1.look(radians(mouseX - pmouseX) / 6.0, radians(mouseY - pmouseY) / 2.0); } else if (mouseButton == RIGHT) { camera1.zoom(radians(mouseY - pmouseY) / 2.0); } else if (mouseButton == CENTER) { camera1.look(radians(mouseX - pmouseX) / 6.0, radians(mouseY - pmouseY) / 2.0); } if (keyCode == UP || key == 'w') { camera1.dolly(-camera1Speed / 2); } else if (keyCode == DOWN || key == 's') { camera1.dolly(camera1Speed / 2); } if (keyCode == LEFT || key == 'a') { camera1.track(-camera1Speed / 2,0); } else if (keyCode == RIGHT || key == 'd') { camera1.track(camera1Speed / 2,0); } if (!cameraRunning) { camera1Speed = camera1Speed / 1.2; } else { camera1Speed = 1; } } // display info void displayStatus() { textMode(SCREEN); textAlign(LEFT); fill(0, 0, 100); // display the time //~ text("shape: ", 8, 480); //~ text(shapeRendered, 60, 480); text("content: ", 8, 10); text(displayAnimation, 60, 10); }