了解此代码段中的旋转数学

问题描述 投票:-1回答:2

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

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)的作用,它可能是相对于三角学的一些推导。有人能详细解释一下数学吗?

编辑

[抱歉,我的错误,代码段的主要目的是在x-z平面上将“ _targetpos”偏移vec3(difVec.z * Math.sin(rad),0,this._targetPos.z)。我设法重写伪代码以使其有意义。似乎代码片段仅实现了保持相对距离并查看目标的功能。

algorithm unity3d math 3d game-development
2个回答
0
投票

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

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


0
投票

1- cos(Θ)= 2sin²(Θ/ 2)和sin(Θ)= 2 sin(Θ/ 2)cos(Θ/ 2),因此您要将标量δ乘以2 sin(Θ/ 2),并沿-Θ/ 2方向的向量将其添加到目标位置。

© www.soinside.com 2019 - 2024. All rights reserved.