Loading...
Logo
Processing Forum

3D Control with Mouse

in Programming Questions  •  8 months ago  
Hello,

I'm trying to create a camera in a 3D environment that is controlled by a mouse and the WASD keys. So far i can get it to move, but I tried using spherical coordinates to help me control the direct the camera points with the mouse but it is really glitchy. What am I doing wrong? I also wanted to use the robot class to keep the mouse centered in the screen but that is not working either.

Copy code
  1. import java.awt.AWTException;
  2. import java.awt.Robot;

  3. Robot robot;

  4. void setup()  {
  5.   size(640, 360, P3D);
  6.   noStroke();
  7.   fill(204);
  8.   try{
  9.     robot = new Robot();
  10.   } catch (AWTException e){
  11.     e.printStackTrace();
  12.   }
  13.   noCursor();
  14. }
  15. float playerX = 0;
  16. float playerY = -160;
  17. float playerZ = 0;
  18. float focusX = 0;
  19. float focusY = 0;
  20. float focusZ = 0;
  21. long count = 0;
  22. float currentMouseX = 320;
  23. float currentMouseY = 180;
  24. float gamma = 0;
  25. float phi = 0;
  26. void draw()  {
  27.   //if(count%3 == 0){
  28.      gamma += (currentMouseX - mouseX)/300;
  29.      gamma = (gamma + PI)%(2*PI) - PI;
  30.      phi += -(currentMouseY - mouseY)/30;
  31.      phi = (phi + PI)%(2*PI) - PI;
  32.      focusX = playerX + 10.0*sin(gamma)*cos(phi);
  33.      focusY = playerY + 10.0*sin(gamma)*sin(phi);
  34.      focusZ = playerZ + 10.0*cos(gamma);
  35.     // robot.mouseMove(frame.getLocation().x + width/2, frame.getLocation().y + height/2);
  36.      currentMouseX = mouseX;
  37.      currentMouseY = mouseY;
  38.   //}
  39.   println(gamma + " " + phi);
  40.   count++;
  41.   background(0);
  42.   lights();
  43.   pointLight(256, 256, 256, playerX, playerY, playerZ);
  44.   //spotLight(256, 256, 256, playerX, playerY, playerZ, focusX, focusY, focusZ, PI/2, 2); 
  45.   camera(playerX, playerY, playerZ, focusX, focusY, focusZ, 0, 1, 0);
  46.   float distanceX = playerX - focusX;
  47.   float distanceZ = playerZ - focusZ;
  48.   float distanceXZ = distance2D(playerX, playerZ, focusX, focusZ);
  49.   if(keyPressed){
  50.     if(key == 'w' || key == 'W'){
  51.       playerX -= (distanceX*10)/distanceXZ;
  52.       playerZ -= (distanceZ*10)/distanceXZ;
  53.     }
  54.     if(key == 's' || key == 'S'){
  55.       playerX += (distanceX*10)/distanceXZ;
  56.       playerZ += (distanceZ*10)/distanceXZ;
  57.     }
  58.     if(key == 'a' || key == 'A'){
  59.       playerX -= (distanceZ*10)/distanceXZ;
  60.       playerZ += (distanceX*10)/distanceXZ;
  61.     }
  62.     if(key == 'd' || key == 'D'){
  63.       playerX += (distanceZ*10)/distanceXZ;
  64.       playerZ -= (distanceX*10)/distanceXZ;
  65.     }
  66.   }
  67.   
  68.   pushMatrix();
  69.   translate(0, -80, 0);
  70.   box(160);
  71.   popMatrix();
  72.   
  73.   pushMatrix();
  74.   translate(160, -240, 0);
  75.   box(160);
  76.   popMatrix();
  77.   
  78.   pushMatrix();
  79.   translate(-5000, 0, -5000);
  80.   rotateX(PI/2);
  81.   rect(0,0,10000,10000); 
  82.   popMatrix();
  83.   
  84.   /*pushMatrix();
  85.   translate(playerX + 10,playerY + 80 ,playerZ);
  86.   rotateZ(PI/3);
  87.   box(10,160,10);
  88.   popMatrix();*/
  89. }

  90. float distance3D(float xa, float ya, float za, float xb, float yb, float zb){
  91.   return sqrt(square(xa-xb) + square(ya-yb) + square(za-zb));
  92. }
  93. float distance2D(float xa, float ya, float xb, float yb){
  94.   return sqrt(square(xa-xb) + square(ya-yb));
  95. }
  96. float square(float a){
  97.   return a*a;
  98. }

Replies(1)

please try search first person game

first person shooter in www.openprocessing.org