给定一个位置和旋转,我怎样才能找到一个从它延伸 X 距离的点?

Given a position and rotation, how can I find a point that extends X distance from it?

我在 3D 中有一个点 space,那个点有一个方向。我想按照方向获取距原点 X 距离的直线的终点。我该怎么做?

答案不必特定于任何库,但我正在使用 Three.JS。

关于 Three.JS,根据您的起点或您的舒适程度,有多种解决方案。

Object3D

它为您提供额外的实用方法,让 Three.js 为您计算数学:

// Create an Object3D
const element = new THREE.Object3D();

// Set position and orientation
element.position.copy(startPosition);
element.rotation.copy(eulerRotations);

// Move "forward" by the desired distance
element.translateZ(distance);

// Now we have our final position!
console.log(element.position);

我认为你的问题的关键是 Object3D.translateZ() 方法,you can read more about it in the docs

矢量 3

在内部,Object3D 刚刚做的是 Vector3 数学运算。如果您只处理点和方向,直接使用 Vector3 可能更有意义:

const finalPosition = new Vector3(0, 0, 1)
  .applyQuaternion(quaternionRotations)
  .multiplyScalar(distance)
  .add(startPosition);

数学

如果您只想要数学,这就是 Three.JS 在幕后所做的事情:

let x = 0;
let y = 0;
let z = 1;

const ix =   quaternionW * x + quaternionY * z - quaternionZ * y;
const iy =   quaternionW * y + quaternionZ * x - quaternionX * z;
const iz =   quaternionW * z + quaternionX * y - quaternionY * x;
const iw = - quaternionX * x - quaternionY * y - quaternionZ * z;

x = ix * quaternionW + iw * - quaternionX + iy * - quaternionZ - iz * - quaternionY;
y = iy * quaternionW + iw * - quaternionY + iz * - quaternionX - ix * - quaternionZ;
z = iz * quaternionW + iw * - quaternionZ + ix * - quaternionY - iy * - quaternionX;

x = x * distance + originalPositionX;
y = y * distance + originalPositionY;
z = z * distance + originalPositionZ;

可以简化为:

function ray(position, distance, direction) {
  const dy2 = 2 * direction.y;
  const dx2 = 2 * direction.x;
  const x = position.x + distance * (dy2 * direction.w + dx2 * direction.z);
  const y = position.y + distance * (dy2 * direction.z - dx2 * direction.w);
  const z =
    position.z +
    distance *
      (-1 * Math.pow(direction.y, 2) +
        Math.pow(direction.z, 2) +
        Math.pow(direction.w, 2) -
        Math.pow(direction.x, 2));

  return {x, y, z};
}