Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- W,A,S,D -> Bewegung
- Space -> UP
- Shift -> Down
- MouseMovment -> Umschauen
- */
- //Importiert Bibliotheken #nichtWichtig
- import java.awt.Robot;
- import java.awt.AWTException;
- Robot robot; //Object robot zum Verschieben der Maus
- Box b[]; //Feld der Klasse Box
- float cX, cY, cZ; //Kamera-Position
- float zX, zY, zZ; //Betrachtungspunkt-Position
- float degreeX; //Winkel zw. Z-Achse und Blickrichtung(betrachtet in der X-Z-Ebene) der Kamera in der X-Z-Ebene
- float degreeY; //Winkel zw. Blickrichtung(betrachtet in der X-Z-Ebene) und der "wirklichen" Blickrichtung(alle Dimensionen beachtet) in der Blickrichtung-Y-Ebene
- void setup() {
- fullScreen(P3D);
- cX=width/2;
- cY=0;
- b=new Box[4]; //vier Boxen
- b[0]=new Box(width/2, height/2, 0, 100, color(random(255), random(255), random(255)));
- b[1]=new Box(-200, 0, 300, 100, color(random(255), random(255), random(255)));
- b[2]=new Box( 50, -80, -500, 100, color(random(255), random(255), random(255)));
- b[3]=new Box(-630, -400, 150, 100, color(random(255), random(255), random(255)));
- textSize(100);
- try {
- robot = new Robot();
- }
- catch (AWTException e) {
- e.printStackTrace();
- }
- noCursor();
- robot.mouseMove(width/2, height/2); //Maus zum Bildschirm Mittelpunkt
- //manuelles Setzen der Maus-Variablen, um anfangs Blickrichtung zu bewahren und Bugs zum verhindern
- mouseX=width/2;
- mouseY=height/2;
- }
- void draw() {
- background(255);
- //Zeichnen aller Boxen, spezielle for-Schleife (https://processing.org/reference/for.html)
- for (Box box : b) {
- box.zeichne();
- }
- float dy=(mouseY-height/2); //Distanz zwischen Y-Mittelpunkt und Maus Y-Koordinate
- degreeY+=map(dy, 0, height/2, 0, 90); //Verhältnis von dy im Bereich 0,height/2 auf Bereich 0,90 projezieren -> Addition zu Winkel degreeY (https://processing.org/reference/map_.html)
- zY=cos(radians(degreeY)); //Zuweisung des Kosinus des Winkels als zY
- float dx=mouseX-width/2; //Distanz zwischen X-Mittelpunkt und Maus X-Koordinate
- degreeX+=map(dx, 0, width/2, 0, 90); //Verhältnis von dx im Bereich 0,width/2 auf Bereich 0,90 projeczieren -> Addition zu Winkel degreeX
- zX=cos(radians(degreeX)); //Zuweisung des Kosinus des Winkels als zX (Tipp:Einheitskreis)
- zZ=sin(radians(degreeX)); //Zuweisung des Sinus des Winkels als zZ (Tipp:Einheitskreis)
- robot.mouseMove(width/2, height/2); //Bewegen der Maus in den Mittelpunkt des Bildschirms
- camera(cX, cY, cZ, zX+cX, zY+cY, zZ+cZ, 0, 1, 0); //Kamera setzen mit Position der Kamera,Position des Betrachungspunkt und der Y-Achse als Achse nach unten
- //Einzeichen der Koordinatenachsen
- strokeWeight(5);
- stroke(255, 0, 0);
- line(0, 0, 10000, 0);
- text("X", 10000, 0, 0);
- stroke(0, 255, 0);
- line(0, 0, 0, 0, 10000, 0);
- text("Y", 0, 10000, 0);
- stroke(0, 0, 255);
- line(0, 0, 0, 0, 0, 10000);
- text("Z", 0, 0, 10000);
- //Abfragen der Tasten zur Bewegung
- if (keyPressed) {
- if (key != CODED) {
- switch(key) {
- case 'w': //Vorwärts
- cX=cX+cos(radians(degreeX))*10;
- cZ=cZ+sin(radians(degreeX))*10;
- break;
- case 'a': //Links
- cX=cX+cos(radians(degreeX-90))*10;
- cZ=cZ+sin(radians(degreeX-90))*10;
- break;
- case 's': //Rückwerts
- cX=cX-cos(radians(degreeX))*10;
- cZ=cZ-sin(radians(degreeX))*10;
- break;
- case 'd': //Rechts
- cX=cX+cos(radians(degreeX+90))*10;
- cZ=cZ+sin(radians(degreeX+90))*10;
- break;
- case ' ': //Oben
- cY-=10;
- break;
- }
- } else {
- if (keyCode==SHIFT) { //Unten
- cY+=10;
- }
- }
- }
- }
- //Klasse Box
- class Box {
- PVector pos; //Position als Vector
- float g; //Kantenlänge des Würfels
- color f; //Farbe
- //Konstruktor1
- Box(float x, float y, float z, float g, color f) {
- pos=new PVector(x, y, z);
- this.g=g;
- this.f=f;
- }
- //Konstruktor2
- Box(PVector pos, float g, color f) {
- this.pos=pos;
- this.g=g;
- this.f=f;
- }
- void zeichne() {
- fill(f);
- stroke(0);
- strokeWeight(1);
- pushMatrix();
- translate(pos.x, pos.y, pos.z); //Position
- box(g); //Box mit Kantenlänge g
- popMatrix();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement