How to get the forward direction from the Matrix4 of the camera

Hello good day, everybody. I need help of three.js mater.

I would like to move camera forward, backward, left and right focusing the target.
But I don’t use the three.js library, I’m using handmade Camera function.
And it’s only have own Matrix4 value.

So please teach me how can I get the forward direction and how can I move camera.
Thanks for reading very much.

Hello, Mr.copy

I’m still a beginner, but I recently did something similar, so I hope this helps you in your work.

const e = Any// Your Matrix4
const position = new Vector3().setFromMatrixPosition(e);
const frontValue = position.z;

// MoveCamera
camera.position.z += 0.1;
// or
camera.position.z -= 0.1;

Hello @ShoOsaka, really thank you for your reply.
This is only move the camera up, down, left, right.

Actually I want to make the camera rotate the point.

It looks like i gave the wrong answer.

i don’t know if this is what you want but
I wrote code that also changes LookAt .

What about below?

const st = .5;
var cameraDirection = new Vector3();
camera.getWorldDirection(cameraDirection);
var cameraPosition = camera.position.clone();
if (move.forward) {
  cameraPosition.add(cameraDirection.multiplyScalar(st));
  camera.position.copy(cameraPosition);
}
if (move.backward) {
  cameraPosition.sub(cameraDirection.multiplyScalar(st));
  camera.position.copy(cameraPosition);
}
if (move.right) {
  var cameraRight = new Vector3();
  cameraRight.crossVectors(cameraDirection, camera.up).normalize();
  cameraPosition.add(cameraRight.multiplyScalar(st));
  camera.position.copy(cameraPosition);
  var cameraTarget = cameraPosition.clone().add(cameraDirection);
  camera.lookAt(cameraTarget); // LookAt Update
}
if (move.left) {
  var cameraLeft = new Vector3();
  cameraLeft.crossVectors(cameraDirection, camera.up).normalize();
  cameraPosition.sub(cameraLeft.multiplyScalar(st));
  camera.position.copy(cameraPosition);
  camera.lookAt(cameraPosition.clone().add(cameraDirection));
}

PS: If you want to look at a specific target when moving right or left, I think the “camera.lookAt” part in the last line can be realized with ``camera.lookAt(target.position.clone())‘’.

Thanks for your kind info.
As I said, I don’t use three.js module, so I can’t use getWorldDirection(), and other functions.
I would like to get everything from worldmatrix (Matrix4) of the camera.

Mr.copy

understood.

I’m sorry I can’t be of any help.

to my knowledge,
It may be a little difficult to answer.

Can someone please answer Mr. Copy?

var fwdValue = 0
var tempVector = new THREE.Vector3()
var upVector = new THREE.Vector3(0, 1, 0)
this.tempVector.
set(0, 0, -fwdValue).
applyAxisAngle(this.upVector, angle);
this.camera.position.addScaledVector(
this.tempVector,
1);
This is moving forward
Angle : is the current angle of the camera
You can see example here :https://codepen.io/ogames/pen/rNmYpdo

1 Like

your example is crazy
this is what I want
I want my camera to move forward, backward, leftward, and rightward, with the w,s, a, and d keys, and rotate on the y-axis using the q and e keys. I am finding solutions for 3 days, please help
can you make it for PC