Translate Object based on Object rotation ignoring one rotation axis

I have a camera and i want it to be able to look in any direction, and have the user move it in 2D.

Pseudo Code

const matrix = new Matrix4();
const dir = new Vector3(0, 0, -1)
// I need a way to rotate dir, so that i move forwards.
// i also need to control the y position separately because i have super basic physics.
matrix.setPosition(dir.x, 0, dir.z);


If you ( 0, 0, - 1 ) represents the default forward vector of your camera, compute the current look direction in world space like so:

dir.applyQuaternion( camera.quaternion );

Or you just use Camera.getWorldDirection().

You can then translate the camera along the look direction according to a specific offset.

@Mugen87 the issue that im having is that when i apply the cameras quaternion, i have no way (at least to my knowledge) to constrain the movement to two axis.

using this image as a visual representation i want to move in the red and green directions but not in the blue direction and i want the movement to be relative to the cameras y rotation.

Why don’t you add the displacement vector (the offset along the look direction) only to the x/z coordinates? It seems you are already doing this in your first code snippet?

1 Like

it works now, i thought i already tried that since its the obvious thing to do, i got stuck on something that i already solved yay thats always fun, anyways thanks for your help.