【问题标题】:Understanding the rotation math in this code snippet了解此代码段中的旋转数学
【发布时间】:2020-02-11 12:20:23
【问题描述】:

这是我的特定于旋转事物的代码,由伪代码给出。

var rad = (targettransform.rotationEuler.y) * Math.PI / 180;


var difVec = targettransform.pos - othertransform.pos;

//the difVec.z which means radius
this._targetPos.z -= difVec.z * (1 - Math.cos(rad));
this._targetPos.x += difVec.z * Math.sin(rad);

this.transform.position = this._targetPos;
this.transform.lookAt(targettransform.position, Utils.UnitY);

上面的伪代码无非就是旋转,但是我不明白1 - Math.cos(rad)是做什么的,可能是相对于三角函数的一些推导。有人能详细解释一下这个数学吗?

编辑

对不起,我的错误,代码sn-p的主要目的是在x-z平面上将“_targetpos”偏移vec3(difVec.z * Math.sin(rad),0,this._targetPos.z)。我设法重写伪代码以使其有意义。看来sn-ps的代码只实现了保持相对距离和看目标的功能。

【问题讨论】:

    标签: algorithm unity3d math 3d game-development


    【解决方案1】:

    1 - cos(Θ)= 2 sin²(Θ/2) 和 sin(Θ) = 2 sin(Θ/2) cos(Θ/2),因此您将标量 δ 乘以 2 sin(Θ /2),并通过一个方向为-Θ/2的向量,与目标位置相加。

    【讨论】:

    • 我知道三角恒等式,它对旋转没有任何意义
    • @haoc:我不同意你的观点。
    【解决方案2】:

    我们可以在Rodrigues绕任意轴的旋转公式中看到(1-Cos(Fi))(Sin(Fi))这样的术语

    但问题没有提供足够的细节来确定该代码的真正作用。

    【讨论】:

    • 我怀疑 - 将 z 分量移动 this._targetPos.z 可能表示它变为零。也许更清楚地描述实际问题可能会有所帮助。
    猜你喜欢
    • 2021-04-20
    • 1970-01-01
    • 1970-01-01
    • 2021-03-14
    • 2018-07-02
    • 2022-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多