How to proper use camera.getWorldDirection
as direction to move the mesh (in world coordinates) to left/right relative to the camera (not rotate the camera)?
…or is there a better way?
const target = new Vector3();
const cameraDirection = camera.getWorldDirection(target);
const speed = 3;
// forward
const forward = cameraDirection.scale(speed);
// backward
const backward = cameraDirection.scale(-speed);
// leftward ?
// rightward ?
``
Try to be precise in your questions (and thoughts, of which your question is an expression).
Do you want to move the mesh (in world coordinates), or do you want to rotate the camera so in your view the mesh appears to have moved, while it in fact has retained its world coordinates?
1 Like
If the first one then you can do something like this (see Object3D.up for details):
const forward = cameraDirection.clone();
const backward = cameraDirection.clone().multiplyScalar(-1.0);
const worldUp = camera.localToWorld(camera.up.clone());
const leftward = forward.clone().applyAxisAngle(worldUp, -Math.PI / 2.0);
const rightward = forward.clone().applyAxisAngle(worldUp, Math.PI / 2.0);
Important note to your code - almost all, if not virtually all, vector methods mutate the original vector. Ie. So if you do:
const cameraDirection = camera.getWorldDirection(new Vector3());
const speed = 4.0;
cameraDirection.multiplyScalar(speed);
cameraDirection.multiplyScalar(-speed);
The final value of cameraDirection
will be vector * 4.0 * -4.0 = -16.0
, not -4.0
. Be sure to either clone
vectors whenever necessary or reuse ones that you no longer need.
1 Like
Exactly! Thanks for the explanation
Thanks. I’ve added more information for search optimization.