Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //turn camera
- boolean td=false;
- boolean tl=false;
- boolean tu=false;
- boolean tr=false;
- //key registers
- boolean w,a,s,d;
- //x,y,z of the FLOOR (z is not used, but could be used if I want to implement jumping or vertical movement)
- float x=0;
- float y=0;
- float z=0;
- //location of the camera pivot
- float x2=1600/2;
- float y2=900/2;
- //z2 is used to "zoom in" on the camera pivot
- float z2=0;
- //camera rotation (you can drag the mouse buttons to change the view on the camera pivot, try it)
- float camRotZ,camRotX;
- //floor rotation (to create the effect that the character is turning)
- float frotZ,frotY;
- //base speed at which the character moves
- float spd=10;
- void setup(){
- //p3d canvas to allow Z translations
- size(1600,900,P3D);
- background(0);
- frameRate(30);
- }
- void draw(){
- moveCam();
- move();
- //all the graphics
- textSize(25);
- fill(255);
- stroke(255);
- background(0);
- pushMatrix();
- //the camera is here
- translate(x2,y2,z2);
- rotateX(radians(camRotX+75));
- rotateZ(radians(camRotZ));
- pushMatrix();
- //the floor
- rotateZ(radians(frotZ));
- translate(0,0,-150);
- fill(125);
- rectMode(RADIUS);
- rect(x,y,1000,1000);
- popMatrix();
- pushMatrix();
- //the "player" (just a sphere)
- noFill();
- stroke(255);
- sphere(25);
- //player's "first person viewpoint"
- line(0,0,0,-100);
- popMatrix();
- popMatrix();
- //text to show values I was frequently using and needed to test with
- 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);
- }
- //increasing z2 to create a zooming in/out effect
- void mouseWheel(MouseEvent event) {
- float e = event.getCount();
- if(e != 0) {
- if(e < 0) {
- z2+=10;
- } else if(e > 0) {
- z2-=10;
- }
- }
- }
- //registering keypresses for WASD
- void keyPressed(){
- if(key == 'a' || key == 'A'){
- a=true;
- } else if(key == 'd' || key == 'D'){
- d=true;
- } else if(key == 'w' || key == 'W'){
- w=true;
- } else if(key == 's' || key == 'S'){
- s=true;
- }
- }
- //key releases for WASD
- void keyReleased(){
- if(key == 'a' || key == 'A'){
- a=false;
- } else if(key == 'd' || key == 'D'){
- d=false;
- } else if(key == 'w' || key == 'W'){
- w=false;
- } else if(key == 's' || key == 'S'){
- s=false;
- }
- }
- //to change the POV (by dragging the mouse)
- void mouseDragged(){
- if(pmouseX>mouseX){
- tl=false;
- tr=true;
- } if(pmouseX<mouseX){
- tr=false;
- tl=true;
- } if(pmouseY>mouseY){
- tu=true;
- td=false;
- } if(pmouseY<mouseY){
- td=true;
- tu=false;
- }
- }
- void mouseReleased(){
- tr=false;
- tl=false;
- tu=false;
- td=false;
- }
- //separate function to allow for multiple directions to change at once
- //i couldnt make it work with the mouseDragged() function alone
- void moveCam(){
- if(tl){
- camRotZ+=(pmouseX-mouseX)/8;
- } else if(tr) {
- camRotZ-=(mouseX-pmouseX)/8;
- }
- if(td){
- camRotX+=(pmouseY-mouseY)/8;
- } else if(tu){
- camRotX-=(mouseY-pmouseY)/8;
- }
- }
- //movement of the "player"
- //the floor is the thing that actually moves
- //this is where I am stuck, at the moment
- //it can only "rotate the floor" to create
- //the effect that the player is turning
- //left or right.
- void move(){
- if(a){
- if(frotZ+1>360){
- frotZ=0;
- }
- frotZ+=1;
- } if(d){
- if(frotZ-1<0){
- frotZ=360;
- }
- frotZ-=1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement