Advertisement
Guest User

Untitled

a guest
Jul 21st, 2018
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //turn camera
  2. boolean td=false;
  3. boolean tl=false;
  4. boolean tu=false;
  5. boolean tr=false;
  6.  
  7. //key registers
  8. boolean w,a,s,d;
  9.  
  10. //x,y,z of the FLOOR (z is not used, but could be used if I want to implement jumping or vertical movement)
  11. float x=0;
  12. float y=0;
  13. float z=0;
  14.  
  15. //location of the camera pivot
  16. float x2=1600/2;
  17. float y2=900/2;
  18. //z2 is used to "zoom in" on the camera pivot
  19. float z2=0;
  20.  
  21. //camera rotation (you can drag the mouse buttons to change the view on the camera pivot, try it)
  22. float camRotZ,camRotX;
  23.  
  24. //floor rotation (to create the effect that the character is turning)
  25. float frotZ,frotY;
  26.  
  27. //base speed at which the character moves
  28. float spd=10;
  29.  
  30. void setup(){
  31.   //p3d canvas to allow Z translations
  32.   size(1600,900,P3D);
  33.   background(0);
  34.   frameRate(30);
  35. }
  36.  
  37. void draw(){
  38.  
  39.   moveCam();
  40.   move();
  41.  
  42.   //all the graphics
  43.   textSize(25);
  44.   fill(255);
  45.   stroke(255);
  46.   background(0);
  47.   pushMatrix();
  48.     //the camera is here
  49.     translate(x2,y2,z2);
  50.     rotateX(radians(camRotX+75));
  51.     rotateZ(radians(camRotZ));
  52.     pushMatrix();
  53.       //the floor
  54.       rotateZ(radians(frotZ));
  55.       translate(0,0,-150);
  56.       fill(125);
  57.       rectMode(RADIUS);
  58.       rect(x,y,1000,1000);
  59.     popMatrix();
  60.     pushMatrix();
  61.       //the "player" (just a sphere)
  62.       noFill();
  63.       stroke(255);
  64.       sphere(25);
  65.       //player's "first person viewpoint"
  66.       line(0,0,0,-100);
  67.     popMatrix();
  68.   popMatrix();
  69.   //text to show values I was frequently using and needed to test with
  70.   text("x: "+x+", y: "+y+", z: "+z+"\n"+"x2: "+x2+", y2: "+y2+", z2: "+z2+"\n"+"frotZ: "+frotZ+", to rad: "+radians(frotZ)+"\nsin(rad(frotZ)): "+sin(radians(frotZ)),20,40);
  71. }
  72.  
  73. //increasing z2 to create a zooming in/out effect
  74. void mouseWheel(MouseEvent event) {
  75.   float e = event.getCount();
  76.   if(e != 0) {
  77.     if(e < 0) {
  78.       z2+=10;
  79.     } else if(e > 0) {
  80.       z2-=10;
  81.     }
  82.   }
  83. }
  84.  
  85. //registering keypresses for WASD
  86. void keyPressed(){
  87.   if(key == 'a' || key == 'A'){
  88.     a=true;
  89.   } else if(key == 'd' || key == 'D'){
  90.     d=true;
  91.   } else if(key == 'w' || key == 'W'){
  92.     w=true;
  93.   } else if(key == 's' || key == 'S'){
  94.     s=true;
  95.   }
  96. }
  97.  
  98. //key releases for WASD
  99. void keyReleased(){
  100.   if(key == 'a' || key == 'A'){
  101.     a=false;
  102.   } else if(key == 'd' || key == 'D'){
  103.     d=false;
  104.   } else if(key == 'w' || key == 'W'){
  105.     w=false;
  106.   } else if(key == 's' || key == 'S'){
  107.     s=false;
  108.   }
  109. }
  110.  
  111. //to change the POV (by dragging the mouse)
  112. void mouseDragged(){
  113.   if(pmouseX>mouseX){
  114.     tl=false;
  115.     tr=true;
  116.   } if(pmouseX<mouseX){
  117.     tr=false;
  118.     tl=true;
  119.   } if(pmouseY>mouseY){
  120.     tu=true;
  121.     td=false;
  122.   } if(pmouseY<mouseY){
  123.     td=true;
  124.     tu=false;
  125.   }
  126. }
  127.  
  128. void mouseReleased(){
  129.   tr=false;
  130.   tl=false;
  131.   tu=false;
  132.   td=false;
  133. }
  134.  
  135. //separate function to allow for multiple directions to change at once
  136. //i couldnt make it work with the mouseDragged() function alone
  137. void moveCam(){
  138.   if(tl){
  139.     camRotZ+=(pmouseX-mouseX)/8;
  140.   } else if(tr) {
  141.     camRotZ-=(mouseX-pmouseX)/8;
  142.   }
  143.   if(td){
  144.     camRotX+=(pmouseY-mouseY)/8;
  145.   } else if(tu){
  146.     camRotX-=(mouseY-pmouseY)/8;
  147.   }
  148. }
  149.  
  150. //movement of the "player"
  151. //the floor is the thing that actually moves
  152. //this is where I am stuck, at the moment
  153. //it can only "rotate the floor" to create
  154. //the effect that the player is turning
  155. //left or right.
  156.  
  157. void move(){
  158.   if(a){
  159.     if(frotZ+1>360){
  160.       frotZ=0;
  161.     }
  162.     frotZ+=1;
  163.   } if(d){
  164.     if(frotZ-1<0){
  165.       frotZ=360;
  166.     }
  167.     frotZ-=1;
  168.   }
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement