xna得到正确的方向3d
问题描述:
如果我有起始位置vector3和旋转vector3,如何获得指示行进方向的矢量?我得到了一个标准化的矢量来表明它是如何旋转的,但是当然这只表示它如何旋转而不是移动的方向。即,如果我在y上旋转会影响x和z上的行进方向,而不是在y上旋转的向量的normailzing会做什么,这将表明它只是表示它已经在y上旋转了。xna得到正确的方向3d
答
在某些情况下,您可能会采用'旋转Vector3'并从中创建一个Matrix。该矩阵具有Vector3属性(Matrix.Forward
),该属性是对应于'旋转Vector3'的方向。如果你不想混淆你已有的矩阵,这种方法应该可以完成这项工作。
Vector3 DirectionToTravel(bool rotationVecIsInRadians, Vector3 rotationVec)//rotation vec must not be normalized at this point
{
Vector3 result;
if (!rotationVecIsInRadians)
{
rotationVec *= MathHelper.Pi/180f;
}
float angle = rotationVec.Length();
rotationVec /= angle; //normalizes rotation vec
result = Matrix.CreateFromAxisAngle(rotationVec, angle).Forward;
return result;
}
答
除了史蒂夫H的回答,您可能会发现需要使用Quarternions更有效地在3D空间中进行旋转。如果您决定使用Quarternions,我提供了一些链接可以帮助您。