Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- PVector globalPos;
- PVector mouseOld;
- PVector mouseNew;
- PVector rotator;
- void setup() {
- fullScreen(P3D);
- initializeCamera();
- }
- void draw() {
- background(220);
- updateCamera();
- drawGridLines();
- drawOrigin();
- drawCurrentPos();
- }
- void initializeCamera(){
- globalPos = new PVector(0, 0, 0);
- mouseNew = new PVector(0,0);
- mouseOld = new PVector(0,0);
- rotator = new PVector(0,0,0);
- }
- void updateCamera() {
- mouseOld = new PVector(mouseNew.x, mouseNew.y);
- mouseNew = new PVector(mouseX, mouseY);
- if (mousePressed) {
- if (mouseButton == LEFT) { //drag the world
- PVector temp = new PVector(mouseNew.x - mouseOld.x, mouseNew.y - mouseOld.y).rotate(-rotator.z);
- globalPos.x += temp.x;
- globalPos.y += temp.y;
- }
- if (mouseButton == RIGHT) { //rotate the world
- rotator.z -= (mouseNew.x - mouseOld.x)*0.01;
- rotator.x -= (mouseNew.y - mouseOld.y)*0.01;
- rotator.x = constrain(rotator.x, 0, HALF_PI);
- }
- }
- translate(width/2, height/2, 0);
- rotateX(rotator.x);
- rotateY(rotator.y);
- rotateZ(rotator.z);
- translate(globalPos.x, globalPos.y, globalPos.z);
- }
- void drawGridLines() {
- stroke(35);
- for (int x = int((-4000-globalPos.x)/100)*100; x < int((4000-globalPos.x)/100)*100; x+= 100) {
- if(x == 0){ //if x gridline is the origin line, make it thick
- strokeWeight(3);
- }else{
- strokeWeight(1);
- }
- line(x, -4000-globalPos.y, x, 4000-globalPos.y);
- }
- for (int y = int((-4000-globalPos.y)/100)*100; y < int((4000-globalPos.y)/100)*100; y+= 100) {
- if(y == 0){ //if y gridline is the origin line, make it thick
- strokeWeight(3);
- }else{
- strokeWeight(1);
- }
- line(-4000-globalPos.x, y, 4000-globalPos.x, y);
- }
- }
- void drawOrigin(){
- noStroke();
- fill(255,0,0);
- translate(50,0,0);
- box(100,10,10);
- translate(-50,0,0);
- fill(0,255,0);
- translate(0,50,0);
- box(10,100,10);
- translate(0,-50,0);
- fill(0,0,255);
- translate(0,0,50);
- box(10,10,100);
- translate(0,0,-50);
- fill(0);
- box(25);
- }
- void drawCurrentPos(){
- strokeWeight(25);
- stroke(0,255,0);
- point(-globalPos.x,-globalPos.y,-globalPos.z);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement