Advertisement
Explosiontime202

3D Movement

Feb 1st, 2020
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.57 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3.  
  4. Robot robot;
  5.  
  6. Player p;
  7. void setup() {
  8.   fullScreen(P3D);
  9.   p=new Player(0, 100, 0);
  10.   try {
  11.     robot=new Robot();
  12.   }
  13.   catch(Exception e) {
  14.     e.printStackTrace();
  15.   }
  16.   noCursor();
  17. }
  18.  
  19. void draw() {
  20.   background(255);
  21.   fill(0);
  22.   line(0, 0, 0, 10000, 0, 0);
  23.   line(0, 0, 0, 0, 10000, 0);
  24.   line(0, 0, 0, 0, 0, 10000);
  25.   fill(#ff0000);
  26.   pushMatrix();
  27.   translate(250, 0, 250);
  28.   box(500, -2, 500);
  29.   popMatrix();
  30.   p.draw();
  31. }
  32.  
  33.  
  34. class Player {
  35.   PVector pos;
  36.   float moveSpeed;
  37.   PVector viewCenter;
  38.   float angle;
  39.   float viewDistance;
  40.   boolean isW=false, isS=false, isA=false, isD=false;
  41.   Player(float x, float y, float z) {
  42.     pos=new PVector(x, y, z);
  43.     viewCenter=new PVector(0, 0, 0);
  44.     viewDistance=sqrt(sq(40)*20);
  45.     moveSpeed=20;
  46.   }
  47.  
  48.   void draw() {
  49.     camera(pos.x, pos.y, pos.z, viewCenter.x, viewCenter.y, viewCenter.z, 0, -1, 0);
  50.     movement();
  51.   }
  52.   void movement() {
  53.     PVector direction=new PVector(viewCenter.x-pos.x, viewCenter.z-pos.z);
  54.     direction.setMag(moveSpeed);
  55.     if (isW) {
  56.       pos.add(direction.x, 0, direction.y);
  57.       viewCenter.add(direction.x, 0, direction.y);
  58.     }
  59.     if (isS) {
  60.       direction.rotate(radians(180));
  61.       pos.add(direction.x, 0, direction.y);
  62.       viewCenter.add(direction.x, 0, direction.y);
  63.     }
  64.     if (isA) {
  65.       direction.rotate(radians(90));
  66.       pos.add(direction.x, 0, direction.y);
  67.       viewCenter.add(direction.x, 0, direction.y);
  68.     }
  69.     if (isD) {
  70.       direction.rotate(radians(270));
  71.       pos.add(direction.x, 0, direction.y);
  72.       viewCenter.add(direction.x, 0, direction.y);
  73.     }
  74.   }
  75.  
  76.   void keyReleased(char k, int code) {
  77.     setMove(k, code, false);
  78.   }
  79.   void keyPressed(char k, int code) {
  80.     setMove(k, code, true);
  81.   }
  82.   void setMove(char k, int code, boolean b) {
  83.     switch(k) {
  84.     case 'w':
  85.       isW=b;
  86.       break;
  87.     case 's':
  88.       isS=b;
  89.       break;
  90.     case 'a':
  91.       isA=b;
  92.       break;
  93.     case 'd':
  94.       isD=b;
  95.       break;
  96.     default:
  97.       switch(code) {
  98.       }
  99.       break;
  100.     }
  101.   }
  102.   void mouseMoved(float dx, float dy) {
  103.     angle-=dx/10;
  104.     angle%=360;
  105.     viewCenter=new PVector(pos.x+cos(radians(angle))*viewDistance, pos.y, pos.z+sin(radians(angle))*viewDistance);
  106.   }
  107. }
  108.  
  109. void keyPressed() {
  110.   p.keyPressed(key, keyCode);
  111. }
  112.  
  113. void keyReleased() {
  114.   p.keyReleased(key, keyCode);
  115. }
  116.  
  117. void mouseMoved() {
  118.   float dx=mouseX-width/2;
  119.   float dy=mouseY-height/2;
  120.   p.mouseMoved(dx, dy);
  121.   robot.mouseMove(width/2, height/2);
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement