23. November 2011
Pixel-Guy 3D
Um den Accelerometer das erste Mal mit Processing zu verbinden habe ich mir eine 3D-Figur aus Würfeln erstellt, die mit dem Input entsprechend gedreht werden kann.Processing-Code
import processing.serial.*; // Import the Processing Serial Library for communicating with arduino Serial myPort; // The used Serial Port float xVal, yVal, zVal; // fourthValue, fifthValue, ... // add more if needed int xPosition = 0; float xRotation, yRotation; float kRotX, kRotY; int cOut = 0; int z = 0; char[][][] out2 = { {{0,0,0,0,0,0}, {0,0,1,1,0,0}, {0,1,1,1,1,0}, {0,1,1,1,1,0}, {0,0,1,1,0,0}, {0,0,0,0,0,0} }, {{0,0,0,0,0,0}, {0,1,1,1,1,0}, {0,1,1,1,1,0}, {0,1,1,1,1,0}, {0,1,1,1,1,0}, {0,0,0,0,0,0} }, {{0,0,0,0,0,0}, {0,1,1,1,1,0}, {0,1,1,1,1,0}, {0,1,1,1,1,0}, {0,1,1,1,1,0}, {0,0,0,0,0,0} }, {{0,0,1,1,0,0}, {0,1,1,1,1,0}, {0,1,1,1,1,0}, {0,1,1,1,1,0}, {0,1,1,1,1,0}, {0,0,0,0,0,0} }, {{0,0,0,0,0,0}, {0,0,1,1,0,0}, {0,1,1,1,1,0}, {0,1,1,1,1,0}, {0,0,1,1,0,0}, {0,0,0,0,0,0} }, {{0,0,0,0,0,0}, {0,0,0,0,0,0}, {0,0,1,1,0,0}, {0,0,1,1,0,0}, {0,0,0,0,0,0}, {0,0,0,0,0,0} }, {{0,0,0,0,0,0}, {0,1,1,1,1,0}, {1,1,1,1,1,1}, {1,1,1,1,1,1}, {0,1,1,1,1,0}, {0,0,0,0,0,0} }, {{0,0,0,0,0,0}, {0,1,1,1,1,0}, {1,1,1,1,1,1}, {1,1,1,1,1,1}, {0,1,1,1,1,0}, {0,0,0,0,0,0} }, {{0,0,0,0,0,0}, {0,1,1,1,1,1}, {0,1,1,1,1,1}, {1,1,1,1,1,0}, {1,1,1,1,1,0}, {0,0,0,0,0,0} }, {{0,0,0,0,0,0}, {0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,1,0}, {0,0,0,1,1,0}, {0,0,0,1,1,0}, {0,1,1,0,0,0}, {0,1,1,0,0,0}, {0,1,1,0,0,0} } }; void setup() { background(0); size(1024, 768, P3D); println(Serial.list()); // Prints the list of serial available devices (Arduino should be on top of the list) myPort = new Serial(this, Serial.list()[0], 9600); // Open a new port and connect with Arduino at 9600 baud } void draw() { background(0,0,0,0.5); directionalLight(153, 153, 153, 0, 0, -1); translate(width/2, height/2); float xValCorrected = (xVal)/64; float yValCorrected = (yVal)/64; float zValCorrected = (zVal)/64; println(whichPlaneTop(xValCorrected,yValCorrected, zValCorrected)); float angleX = atan(xVal / sqrt(sq(yVal) + sq(zVal))); float angleY = atan(yVal / sqrt(sq(xVal) + sq(zVal))); float angleZ = atan(sqrt(sq(xVal) + sq(zVal)) / zVal); if(angleZ>0) { xRotation = radians(map(xVal, -64, 64, 0, 180)); yRotation = radians(map(yVal, -64, 64, 180, 360)); } else { xRotation = radians(map(xVal, -64, 64, 180, 0)); yRotation = radians(map(yVal, -64, 64, 0, 180)); } //rotateX(xRotation); //rotateY(yRotation); kRotX+=2; kRotY+=2; rotateX(radians(kRotX)); rotateY(radians(kRotY)); int wBox = 15; int dBox = mouseX/5; if (dBox<wBox) wBox=dBox; pushMatrix(); translate(dBox*6/2*1,dBox*out2.length/2*-1,dBox*6/2*-1); rotateZ(radians(90)); float cRandomGoggles = random(30,120); for(int n=0; n<out2.length; n++) { for(int m=0; m<out2[cOut].length; m++) { for(int l=0; l<out2[cOut][m].length;l++) { if (out2[cOut][m][l]==1) { pushMatrix(); fill(102,204,102); if (n>11 && l>0 && l<out2[cOut][m].length-1) fill(75,75,75); if (n>12 && l>0 && l<out2[cOut][m].length-1) fill(102,102,102); if (n>7 && (l<1 || l>=out2[cOut][m].length-1)) fill(153,204,153); if (n<6) fill(153,204,153); if (n<5 && (l==1 || l==4) && m>2) fill(204,102,102); if (n<5 && m>3) fill(204,102,102); if (n==0) fill(204,102,102); if (n==2 && m<=2) fill(cRandomGoggles); //rotate(radians((360/12)*i)); translate(0+dBox*n,0+dBox*m,0+dBox*l); pushStyle(); noStroke(); box(wBox); popStyle(); popMatrix(); } } } cOut++; if (cOut==out2.length) cOut=0; } popMatrix(); } void serialEvent(Serial myPort) // Is called everytime there is new data to read { if (myPort.available() > 0) { String completeString = myPort.readStringUntil(10); // Read the Serial port until there is a linefeed/carriage return if (completeString != null) // If there is valid data insode the String { trim(completeString); // Remove whitespace characters at the beginning and end of the string String seperateValues[] = split(completeString, "\t"); // Split the string everytime a delimiter is received xVal = int(seperateValues[0]); yVal = int(seperateValues[1]); zVal = int(seperateValues[2]); } } } String whichPlaneTop(float x, float y, float z) { //println(x+"\t"+y+"\t"+z); String sideTop = ""; if (x<0.2 && y<0.2 && z>0.8) { sideTop = "top"; } else if (x<0.2 && y<0.2 && z<-0.8) { sideTop = "bottom"; } else if (x<0.2 && y<-0.8 && z<0.2) { sideTop = "right"; } else if (x<0.2 && y>0.8 && z<0.2) { sideTop = "left"; } else if (x>0.8 && y<0.2 && z<0.2) { sideTop = "front"; } else if (x<-0.8 && y<0.2 && z<0.2) { sideTop = "back"; } return sideTop; } void keyPressed() { switch(key) { case 'p': save("screens/cubeguy_"+hour()+"-"+minute()+"-"+second()+".jpg"); break; } }