Advertisement
Explosiontime202

3D-Movement-Programm (Processing)

Apr 18th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.16 KB | None | 0 0
  1. /*
  2. W,A,S,D -> Bewegung
  3.  Space -> UP
  4.  Shift -> Down
  5.  MouseMovment -> Umschauen
  6.  */
  7.  
  8. //Importiert Bibliotheken #nichtWichtig
  9. import java.awt.Robot;
  10. import java.awt.AWTException;
  11.  
  12. Robot robot; //Object robot zum Verschieben der Maus
  13.  
  14. Box b[]; //Feld der Klasse Box
  15. float cX, cY, cZ; //Kamera-Position
  16. float zX, zY, zZ; //Betrachtungspunkt-Position
  17. float degreeX; //Winkel zw. Z-Achse und Blickrichtung(betrachtet in der X-Z-Ebene) der Kamera in der X-Z-Ebene
  18. float degreeY; //Winkel zw. Blickrichtung(betrachtet in der X-Z-Ebene) und der "wirklichen" Blickrichtung(alle Dimensionen beachtet) in der Blickrichtung-Y-Ebene
  19. void setup() {
  20.   fullScreen(P3D);
  21.   cX=width/2;
  22.   cY=0;
  23.   b=new Box[4]; //vier Boxen
  24.   b[0]=new Box(width/2, height/2, 0, 100, color(random(255), random(255), random(255)));
  25.   b[1]=new Box(-200, 0, 300, 100, color(random(255), random(255), random(255)));
  26.   b[2]=new Box( 50, -80, -500, 100, color(random(255), random(255), random(255)));
  27.   b[3]=new Box(-630, -400, 150, 100, color(random(255), random(255), random(255)));
  28.   textSize(100);
  29.   try {
  30.     robot = new Robot();
  31.   }
  32.   catch (AWTException e) {
  33.     e.printStackTrace();
  34.   }
  35.   noCursor();
  36.   robot.mouseMove(width/2, height/2); //Maus zum Bildschirm Mittelpunkt
  37.  
  38.   //manuelles Setzen der Maus-Variablen, um anfangs Blickrichtung zu bewahren und Bugs zum verhindern
  39.   mouseX=width/2;
  40.   mouseY=height/2;
  41. }
  42. void draw() {
  43.   background(255);
  44.   //Zeichnen aller Boxen, spezielle for-Schleife (https://processing.org/reference/for.html)
  45.   for (Box box : b) {
  46.     box.zeichne();
  47.   }
  48.   float dy=(mouseY-height/2); //Distanz zwischen Y-Mittelpunkt und Maus Y-Koordinate
  49.   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)
  50.   zY=cos(radians(degreeY)); //Zuweisung des Kosinus des Winkels als zY
  51.   float dx=mouseX-width/2; //Distanz zwischen X-Mittelpunkt und Maus X-Koordinate
  52.   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
  53.   zX=cos(radians(degreeX)); //Zuweisung des Kosinus des Winkels als zX (Tipp:Einheitskreis)
  54.   zZ=sin(radians(degreeX)); //Zuweisung des Sinus des Winkels als zZ (Tipp:Einheitskreis)
  55.   robot.mouseMove(width/2, height/2); //Bewegen der Maus in den Mittelpunkt des Bildschirms
  56.   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
  57.   //Einzeichen der Koordinatenachsen
  58.   strokeWeight(5);
  59.   stroke(255, 0, 0);
  60.   line(0, 0, 10000, 0);
  61.   text("X", 10000, 0, 0);
  62.   stroke(0, 255, 0);
  63.   line(0, 0, 0, 0, 10000, 0);
  64.   text("Y", 0, 10000, 0);
  65.   stroke(0, 0, 255);
  66.   line(0, 0, 0, 0, 0, 10000);
  67.   text("Z", 0, 0, 10000);
  68.   //Abfragen der Tasten zur Bewegung
  69.   if (keyPressed) {
  70.     if (key != CODED) {
  71.       switch(key) {
  72.       case 'w': //Vorwärts
  73.         cX=cX+cos(radians(degreeX))*10;
  74.         cZ=cZ+sin(radians(degreeX))*10;
  75.         break;
  76.       case 'a': //Links
  77.         cX=cX+cos(radians(degreeX-90))*10;
  78.         cZ=cZ+sin(radians(degreeX-90))*10;
  79.         break;
  80.       case 's': //Rückwerts
  81.         cX=cX-cos(radians(degreeX))*10;
  82.         cZ=cZ-sin(radians(degreeX))*10;
  83.         break;
  84.       case 'd': //Rechts
  85.         cX=cX+cos(radians(degreeX+90))*10;
  86.         cZ=cZ+sin(radians(degreeX+90))*10;
  87.         break;
  88.       case ' ': //Oben
  89.         cY-=10;
  90.         break;
  91.       }
  92.     } else {
  93.       if (keyCode==SHIFT) { //Unten
  94.         cY+=10;
  95.       }
  96.     }
  97.   }
  98. }
  99.  
  100. //Klasse Box
  101. class Box {
  102.   PVector pos; //Position als Vector
  103.   float g; //Kantenlänge des Würfels
  104.   color f; //Farbe
  105.   //Konstruktor1
  106.   Box(float x, float y, float z, float g, color f) {
  107.     pos=new PVector(x, y, z);
  108.     this.g=g;
  109.     this.f=f;
  110.   }
  111.   //Konstruktor2
  112.   Box(PVector pos, float g, color f) {
  113.     this.pos=pos;
  114.     this.g=g;
  115.     this.f=f;
  116.   }
  117.   void zeichne() {
  118.     fill(f);
  119.     stroke(0);
  120.     strokeWeight(1);
  121.     pushMatrix();
  122.     translate(pos.x, pos.y, pos.z); //Position
  123.     box(g); //Box mit Kantenlänge g
  124.     popMatrix();
  125.   }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement