1

I have a ThreeJS set up where the camera can move through a 3D world and while moving it will change its lookAt multiple times. So let's assume that camera.getWorldDirection() will always be more or less random.

Now I need the camera to move exactly left / right / up / down relative to camera.getWorldDirection().
You can't just use something like camera.position.x += 1 because that only applies for a world direction of Vector3(0, 0, -1). If the world direction changes to e.g. (1, -1, 0), moving the camera to the right does require changes in the X and the Z axis.

I had a look at quaternions and 4D matrices but I can't get my monkeybrain around them. Would be really nice if someone could help me out.

Here is a demo: https://normanwink.com/demo/room/

Norman
  • 785
  • 7
  • 27
  • See if this helps: https://stackoverflow.com/a/38341191/1461008 – WestLangley Sep 15 '21 at 16:51
  • @WestLangley Thanks for the hint but unfortunately it won't work due to the rotation of the camera. E.g. if I turn my camera 90 degrees clockwise, moving "forward" along Z will result in a movement to the left, relative to the cameras perspective. – Norman Sep 16 '21 at 07:45
  • How does the solution in the link I posted above differ from the solution in the link you posted in your answer below? – WestLangley Sep 16 '21 at 10:29

1 Answers1

1

I found the answer here in a non-accepted answer.

You gotta use camera.translateX() and similar functions to transform its local position. That way you can always manipulate camera.position to move it around and have the translate functions to add offsets relative to the cameras viewing angle.

Norman
  • 785
  • 7
  • 27