1
\$\begingroup\$

For example, an object is located at (x,y,z) in world space. Now how to calculate the offset translation, I mean no rotation, that will be applied to the camera so as to place the object at the center of the screen?

BTW, I'm using three.js r91. But pseudo code/C++/C# is also OK.

\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

If you want to move the camera a certain distance from an object, and looking at the object, while preserving the camera's rotation, just move the camera at the object's location, then translate on the +Z axis (or -Z, depending where your camera is looking at).

camera.position = object.position;
camera.Translate(0, 0, -r); // where `r` is the desired distance
\$\endgroup\$
2
\$\begingroup\$

You need to position the camera relative to the object using the negative of its front vector (zAxis) multiplied by the distance you want.

var ignore1 = new Vector3();
var ignore2 = new Vector3();
var front_vector = new Vector3();

// get the direction the camera is pointing at
camera.matrix.extractBasis ( ignore1, ignore2, front_vector );

// put the camera at a negative distance from the object
camera.position.copy(object.position);
camera.position.addScaledVector(front_vector, -distance_from_object);
\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.