用给定的3d点计算角度

时间:2012-07-19 15:08:57

标签: vector xna microsoft-metro windows-runtime kinect

我在地铁应用的winrt上试验kinect。 我试图在肘部获得角度。

通常我会做以下

Vector3D handLeftVector = new Vector3D(HandLeftX, HandLeftY, HandLeftZ);
handLeftVector.Normalize();

Vector3D ElbowLeftEVector = new Vector3D(ElbowLeftX, ElbowLeftY, ElbowLeftZ);
ElbowLeftEVector.Normalize();

Vector3D ShoulderLeftVector = new Vector3D(ShoulderLeftX, ShoulderLeftY, ShoulderLeftZ);
ShoulderLeftVector.Normalize();

Vector3D leftElbowV1 = ShoulderLeftVector - ElbowLeftEVector;
Vector3D leftElbowV2 = handLeftVector - ElbowLeftEVector;
double leftElbowAngle = Vector3D.AngleBetween(leftElbowV1, leftElbowV2);

然而,在winrt中没有Vector3D对象。

我决定复制Vector3D方法,如下所示。但结果似乎并不像预期的那样。我在任何地方都犯了错误吗?

double leftElbowV1X = ShoulderLeftX - ElbowLeftX;
double leftElbowV1Y = ShoulderLeftY - ElbowLeftY;
double leftElbowV1Z = ShoulderLeftZ - ElbowLeftZ;

double leftElbowV2X = handLeftX - ElbowLeftX;
double leftElbowV2Y = handLeftY - ElbowLeftY;
double leftElbowV2Z = handLeftZ - ElbowLeftZ;

double product = leftElbowV1X * leftElbowV2X + leftElbowV1Y * leftElbowV2Y + leftElbowV1Z * leftElbowV2Z;

double magnitudeA = Math.Sqrt(Math.Pow(leftElbowV1X, 2) + Math.Pow(leftElbowV1Y, 2) + Math.Pow(leftElbowV1Z, 2));
double magnitudeB = Math.Sqrt(Math.Pow(leftElbowV2X, 2) + Math.Pow(leftElbowV2Y, 2) + Math.Pow(leftElbowV2Z, 2));
magnitudeA = Math.Abs(magnitudeA);
magnitudeB = Math.Abs(magnitudeB);

double cosDelta = product / (magnitudeA * magnitudeB);
double angle = Math.Acos(cosDelta) *180.0 / Math.P;

是否需要将其标准化?

我设法解决了它,但我在想是否有更有效的方法。

1 个答案:

答案 0 :(得分:0)

不确定这是否有帮助,但这是我使用的一些旧角度代码,以度数返回:

float AngleBetween(Vector3 from, Vector3 dest)  {
    float len = from.magnitude *  dest.magnitude;
    if(len < Mathf.Epsilon) len = Mathf.Epsilon;

    float f = Vector3.Dot(from,dest) / len;
    if(f>1.0f)f=1.0f;
    else if ( f < -1.0f) f = -1.0f;

    return Mathf.Acos(f) * 180.0f / (float)Math.PI;
}

显然使用API​​特定语法,但我认为该方法很明确。