如何旋转演员以匹配某个向量?

How to rotate an actor in order to match a certain vector?

我有一个 arrowsources 演员列表,我想将它们的方向设置为指向下一个演员位置。但是 运行 代码后的方向不正确。

def CalculateOrientation(vec):
    #Here I use only one value of a particular vector before deviding it with its length because
 #I'm trying to find the angle between the given vector and the world's axes (X - 1, 0, 0; Y - 0, 1, 0; Z - 0, 0, 1;).
 #I know the formula is dot product divided by the multiplication of the lengths but since the values
 #are 1 or 0 for the world axes, I don't need to include them in the code.
    length = CalculateLength(vec)
    angleX = np.rad2deg(np.arccos(vec[0] / length))
    angleY = np.rad2deg(np.arccos(vec[1] / length))
    angleZ = np.rad2deg(np.arccos(vec[2] / length))

    return [angleX, angleY, angleZ]

def CalculateLength(vec):
    print np.sqrt(vec[0]**2 + vec[1]**2 + vec[2]**2)
    return np.sqrt(vec[0]**2 + vec[1]**2 + vec[2]**2)


def GetVectorBetweenPoints(a, b):
    vec = [b[0] - a[0], b[1] - a[1], b[2] - a[2]]
    return vec
for i in range(1338):  
    arrowsActors[i].SetOrientation(CalculateOrientation(GetVectorBetweenPoints(arrowsActors[i].GetPosition(), arrowsActors[i+1].GetPosition())))

您可以改为使用 SetUserMatrix(),并为其提供一个 "look at" 矩阵,计算方式与 gluLookAt() 相同。您也可以避免使用昂贵的三角函数。

我认为你的数学不成立,因为一个轴上的旋转会影响其他两个轴的长度,例如查看 glRotatef() 生成的旋转矩阵。