【问题标题】:how to rotate a vector around an arbitrary axis?如何围绕任意轴旋转矢量?
【发布时间】:2022-01-31 03:30:38
【问题描述】:

我有一个轴,由 2 个向量定义,例如一个向上指向 x = 10:

const axisStart = new Vector3(10, 0, 0)
const axisEnd = new Vector3(10, 0, 1)

我得到这样的归一化轴方向:

const axisDirection = new Vector3().subVectors(axisEnd, axisStart).normalize()

如何旋转矢量(例如Vector3(50, 0, 0)围绕我的原始轴

我尝试过使用Vector3.applyAxisAngle(axisDirection , radians),但由于轴已标准化,因此旋转发生在世界中心 (0, 0) 周围,而不是围绕轴的原始位置。

【问题讨论】:

  • 您需要在旋转之前将其移至原点,然后再将其移回原点。从正在旋转的向量中减去axisStart,照常进行旋转,然后将axisStart 添加到正在旋转的向量中。

标签: javascript three.js rotation


【解决方案1】:

我通过使用this answer 并将伪代码从它翻译成打字稿来找到该点旋转的轴上的确切点来解决这个问题:

getPivotPoint(pointToRotate: Vector3, axisStart: Vector3, axisEnd: Vector3) {
    const d = new Vector3().subVectors(axisEnd, axisStart).normalize()
    const v = new Vector3().subVectors(pointToRotate, axisStart)
    const t = v.dot(d)
    const pivotPoint = axisStart.add(d.multiplyScalar(t))
    return pivotPoint
  }

然后,正如@Ouroborus 指出的那样,我可以平移该点,应用旋转并将其平移回来:

rotatePointAroundAxis(pointToRotate: Vector3, axisStart: Vector3, axisEnd, radians: number) {
    const axisDirection = new Vector3().subVectors(axisEnd, axisStart).normalize()
    const pivotPoint = getPivotPoint(pointToRotate, axisStart, axisEnd)
    const translationToWorldCenter = new Vector3().subVectors(pointToRotate, pivotPoint)
    const translatedRotated = translationToWorldCenter.clone().applyAxisAngle(axisDirection, radians)
    const destination = pointToRotate.clone().add(translatedRotated).sub(translationToWorldCenter)
    return destination
  }

上面的代码运行良好,把它留在这里给我未来的自己和其他可能觉得这很有用的人。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多