Advertisement
MrMusAddict

Game Engine

Nov 3rd, 2018
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.19 KB | None | 0 0
  1. PVector globalPos;
  2. PVector mouseOld;
  3. PVector mouseNew;
  4. PVector rotator;
  5.  
  6. void setup() {
  7.   fullScreen(P3D);
  8.   initializeCamera();
  9. }
  10.  
  11. void draw() {
  12.   background(220);
  13.   updateCamera();
  14.  
  15.   drawGridLines();
  16.   drawOrigin();
  17.   drawCurrentPos();
  18. }
  19.  
  20. void initializeCamera(){
  21.   globalPos = new PVector(0, 0, 0);
  22.   mouseNew = new PVector(0,0);
  23.   mouseOld = new PVector(0,0);
  24.   rotator = new PVector(0,0,0);
  25. }
  26.  
  27. void updateCamera() {
  28.   mouseOld = new PVector(mouseNew.x, mouseNew.y);
  29.   mouseNew = new PVector(mouseX, mouseY);
  30.  
  31.   if (mousePressed) {
  32.     if (mouseButton == LEFT) { //drag the world
  33.       PVector temp = new PVector(mouseNew.x - mouseOld.x, mouseNew.y - mouseOld.y).rotate(-rotator.z);
  34.       globalPos.x += temp.x;
  35.       globalPos.y += temp.y;
  36.     }
  37.     if (mouseButton == RIGHT) { //rotate the world
  38.       rotator.z -= (mouseNew.x - mouseOld.x)*0.01;
  39.       rotator.x -= (mouseNew.y - mouseOld.y)*0.01;
  40.       rotator.x = constrain(rotator.x, 0, HALF_PI);
  41.     }
  42.   }
  43.  
  44.   translate(width/2, height/2, 0);
  45.   rotateX(rotator.x);
  46.   rotateY(rotator.y);
  47.   rotateZ(rotator.z);
  48.   translate(globalPos.x, globalPos.y, globalPos.z);
  49. }
  50.  
  51.  
  52.  
  53. void drawGridLines() {
  54.   stroke(35);
  55.   for (int x = int((-4000-globalPos.x)/100)*100; x < int((4000-globalPos.x)/100)*100; x+= 100) {
  56.     if(x == 0){ //if x gridline is the origin line, make it thick
  57.       strokeWeight(3);
  58.     }else{
  59.       strokeWeight(1);
  60.     }
  61.     line(x, -4000-globalPos.y, x, 4000-globalPos.y);
  62.   }
  63.   for (int y = int((-4000-globalPos.y)/100)*100; y < int((4000-globalPos.y)/100)*100; y+= 100) {
  64.     if(y == 0){ //if y gridline is the origin line, make it thick
  65.       strokeWeight(3);
  66.     }else{
  67.       strokeWeight(1);
  68.     }
  69.     line(-4000-globalPos.x, y, 4000-globalPos.x, y);
  70.   }
  71. }
  72.  
  73. void drawOrigin(){
  74.   noStroke();
  75.   fill(255,0,0);
  76.   translate(50,0,0);
  77.   box(100,10,10);
  78.   translate(-50,0,0);
  79.  
  80.   fill(0,255,0);
  81.   translate(0,50,0);
  82.   box(10,100,10);
  83.   translate(0,-50,0);
  84.  
  85.   fill(0,0,255);
  86.   translate(0,0,50);
  87.   box(10,10,100);
  88.   translate(0,0,-50);
  89.  
  90.   fill(0);
  91.   box(25);
  92. }
  93.  
  94. void drawCurrentPos(){
  95.   strokeWeight(25);
  96.   stroke(0,255,0);
  97.   point(-globalPos.x,-globalPos.y,-globalPos.z);
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement