Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
640 views
in Technique[技术] by (71.8m points)

javascript - Moving the camera in the direction it's facing, with ThreeJS

Say I have a camera placed at:

{
  x: 10,
  y: 30,
  z: 20
}

It's rotation is:

{
  x: 0.1,
  y: 0.2,
  z: 0.3
}

I want to move the camera 1 unit from it's current position in the direction it's facing.

How do I do calculate the cameras new position?

I'm using ThreeJS.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The THREE.Camera class extends the THREE.Object3D class. You can get the camera direction with the getWorldDirection method from the base class:

It returns a vector representing the direction in which the camera is looking, in world space.

When you have this direction vector you can use it to move the camera in the desired direction:

var direction = new THREE.Vector3();
camera.getWorldDirection( direction );

To move only 1 in that direction you can simply do:

camera.position.add( direction );

If you want to move more you could for example use the multiplyScalar method from the THREE.Vector3 class and multiply with the desired distance.

distance = 10;
camera.position.add( direction.multiplyScalar(distance) );

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...