naurril / SUSTechPOINTS

3D Point Cloud Annotation Platform for Autonomous Driving
GNU General Public License v3.0
785 stars 206 forks source link

Add "wasd" in the main view, it works but the scene behaves weirdly when click and drag the left mouse #153

Open daigz1224 opened 1 year ago

daigz1224 commented 1 year ago

I add key shortcuts "wasd" in the main view, to change the position (e.g, only x, y for convenience) of this.viewManager.mainView.camera, with respect to its quaternion. It works but still has a problem.

    const wasd = ["w", "a", "s", "d"];
    if (this.selected_box === null && wasd.includes(ev.key)) {
      // move camera
      const camera = this.viewManager.mainView.camera;
      const direction = new THREE.Vector3(0, 1, 0);
      const moveForward = new THREE.Vector3();
      const moveLeft = new THREE.Vector3();
      const moveRight = new THREE.Vector3();
      function updateDirectionVectors() {
        direction.applyQuaternion(camera.quaternion);
        moveForward.set(direction.x, direction.y, 0).normalize();
        moveLeft.set(-moveForward.y, moveForward.x, 0).normalize();
        moveRight.set(moveForward.y, -moveForward.x, 0).normalize();
      }
      updateDirectionVectors();

      switch (ev.key) {
        case "w":
          camera.position.add(moveForward);
          // camera.position.y += 1;
          break;
        case "s":
          camera.position.sub(moveForward);
          // camera.position.y -= 1;
          break;
        case "a":
          camera.position.add(moveLeft);
          // camera.position.x -= 1;
          break;
        case "d":
          camera.position.add(moveRight);
          // camera.position.x += 1;
          break;
        default:
          break;
      }
      this.render();
    }

The problem is that if I click and drag the left mouse button immediately after I press "wasd", the scene quickly deflects. I think this problem may be related to the behavior of mouse and the change of camera.quaternion, any suggestion? many thx🫡

naurril commented 1 year ago

the main view camera is controlled by editor.viewManager.mainView.orbitPerspective, which has a keydown handler doing exactly what you are implementing. maybe you can call it directly in your code (by first exposing the keydown handler). or try to make the keydown handler of orbitPerspective work again.

daigz1224 commented 1 year ago

@naurril thx, I found the orbitPerspective in the fusion branch, this branch is the latest dev branch, isn't it?

naurril commented 1 year ago

it is orbit_perspective in other branches