1

我正在为 Metro 应用程序在 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);

但是 Vector3D 对象在 winrt 中不可用。

我决定复制 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;

是否需要对其进行规范化?

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

4

1 回答 1

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 特定的语法,但我认为方法很清楚。

于 2012-07-22T15:43:44.820 回答