admin管理员组文章数量:1406924
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
.
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
.
3 Answers
Reset to default 6three.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:
- Calculate the vector from
B
toA
(this will just beA - B
). - Normalize that vector (make it's length 1).
- Multiply that by the distance you want.
- Add that vector to the point
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.
本文标签: javascriptCalculate point in 3d spacebetween two points at specific distanceStack Overflow
版权声明:本文标题:javascript - Calculate point in 3d space, between two points at specific distance - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744975154a2635471.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论