For a camera movement in three.js I need to calculate point C
so to move the camera from point A
to a certain distance dist
to point B
.
three.js has methods to do that very easily.
Assuming a
, b
, and c
are instances of THREE.Vector3()
,
a.set( 2, 1, 4 );
b.set( 9, 4, 2 );
c.subVectors( a, b ).setLength( dist ).add( b );
three.js r.91
So you need to calculate the coordinates of point C
, given that it lies on the line between B
and A
at the given distance from B
? This is pretty straightforward using the following steps:
B
to A
(this will just be A - B
). B
. So a short javascript example:
const A = [2, 1, 4]; const B = [9, 4, 2]; const dist = 3; function addVectors(v1, v2) { return v1.map((x, i) => x + v2[i]); } function scalarMultiply(v, c) { return v.map(x => x*c); } function getLength(v) { return Math.hypot(...v); } const vecB2A = addVectors(A, scalarMultiply(B, -1)); // Step 1 const normB2A = scalarMultiply(vecB2A, 1/getLength(vecB2A)); // Step 2 const distB2A = scalarMultiply(normB2A, dist); // Step 3 const C = addVectors(B, distB2A); // Final step console.log(C);
Point C is equal to point B minus 'dist' times a unit vector whose direction is AB. So it is quite easy:
vector v from A to B is equal (xB-xA, yB-yA, zB-zA) / distance(AB)
Then C = B - d*v where d is the distance from B you want C to be.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.