使用Kinect绘制DrawBone角度变量

时间:2014-12-13 06:56:48

标签: c# kinect xna-4.0 kinect-sdk xna-math-library

当我们想要将骨骼绘制到XNA中时(使用KnectSDK和C#),您必须计算两个关节之间的差异并在它们之间绘制骨骼。这是绘制骨骼的功能:

private void DrawBone(JointCollection joints, JointType startJoint, JointType endJoint)
    {
        Vector2 start = this.mapMethod(joints[startJoint].Position);
        Vector2 end = this.mapMethod(joints[endJoint].Position);
        Vector2 diff = end - start;
        Vector2 scale = new Vector2(1.0f, diff.Length() / this.boneTexture.Height);

        float angle = (float)Math.Atan2(diff.Y, diff.X) - MathHelper.PiOver2;

        Color color = Color.LightGreen;
        if (joints[startJoint].TrackingState != JointTrackingState.Tracked ||
            joints[endJoint].TrackingState != JointTrackingState.Tracked)
        {
            color = Color.Gray;
        }

        this.SharedSpriteBatch.Draw(this.boneTexture, start, null, color, angle, this.boneOrigin, scale, SpriteEffects.None, 1.0f);
    }

我只是想了解计算角度的方式以及角度公式的工作原理

由于

1 个答案:

答案 0 :(得分:2)

这是计算两个向量之间角度的代码(下面的代码说明):

    public class Angles
    {
    public double AngleBetweenTwoVectors(Vector3D vectorA, Vector3D vectorB)
        {
            double dotProduct = 0.0;
            dotProduct = Vector3D.DotProduct(vectorA, vectorB);

            return (double)Math.Acos(dotProduct)/Math.PI*180;
        }

        public double[] GetVector(Skeleton skeleton)
        {
            Vector3D ShoulderCenter = new Vector3D(skeleton.Joints[JointType.ShoulderCenter].Position.X, skeleton.Joints[JointType.ShoulderCenter].Position.Y, skeleton.Joints[JointType.ShoulderCenter].Position.Z);
            Vector3D RightShoulder = new Vector3D(skeleton.Joints[JointType.ShoulderRight].Position.X, skeleton.Joints[JointType.ShoulderRight].Position.Y, skeleton.Joints[JointType.ShoulderRight].Position.Z);
            Vector3D LeftShoulder = new Vector3D(skeleton.Joints[JointType.ShoulderLeft].Position.X, skeleton.Joints[JointType.ShoulderLeft].Position.Y, skeleton.Joints[JointType.ShoulderLeft].Position.Z);
            Vector3D RightElbow = new Vector3D(skeleton.Joints[JointType.ElbowRight].Position.X, skeleton.Joints[JointType.ElbowRight].Position.Y, skeleton.Joints[JointType.ElbowRight].Position.Z);
            Vector3D LeftElbow = new Vector3D(skeleton.Joints[JointType.ElbowLeft].Position.X, skeleton.Joints[JointType.ElbowLeft].Position.Y, skeleton.Joints[JointType.ElbowLeft].Position.Z);
            Vector3D RightWrist = new Vector3D(skeleton.Joints[JointType.WristRight].Position.X, skeleton.Joints[JointType.WristRight].Position.Y, skeleton.Joints[JointType.WristRight].Position.Z);
            Vector3D LeftWrist = new Vector3D(skeleton.Joints[JointType.WristLeft].Position.X, skeleton.Joints[JointType.WristLeft].Position.Y, skeleton.Joints[JointType.WristLeft].Position.Z);

           /* ShoulderCenter.Normalize();
            RightShoulder.Normalize();
            LeftShoulder.Normalize();
            RightElbow.Normalize();
            LeftElbow.Normalize();
            RightWrist.Normalize();
            LeftWrist.Normalize();

            if (skeleton.Joints[JointType.ShoulderCenter].TrackingState == JointTrackingState.Tracked) { 

            }
            */

            double AngleRightElbow = AngleBetweenTwoVectors(RightElbow - RightShoulder, RightElbow - RightWrist);
            double AngleRightShoulder = AngleBetweenTwoVectors(RightShoulder - ShoulderCenter, RightShoulder - RightElbow);
            double AngleLeftElbow = AngleBetweenTwoVectors(LeftElbow - LeftShoulder, LeftElbow - LeftWrist);
            double AngleLeftShoulder = AngleBetweenTwoVectors(LeftShoulder - ShoulderCenter, LeftShoulder - LeftElbow);

            double[] Angles = {AngleRightElbow, AngleRightShoulder, AngleLeftElbow, AngleLeftShoulder};
            return Angles;
        }
}

首先,我们来看一下GetVector(Skeleton skeleton)方法。在这个方法中,我们首先定义我们的Vector3D(对于您选择的关节)。然后我们调用方法AngleBetweenTwoVectors并给它两个参数。

但请注意。如果我们只给它关节的矢量,我们就不会得到正确的角度。我们首先必须从Vector中减去周围的矢量(我们想从矢量得到矢量)。然后,我们将两个向量传递给方法AngleBetweenTwoVectors

在这种方法中,我们首先要计算点积。有关点积的更多信息,请单击here。 使用点积我们可以计算角度。为此,我们只需要使用arcos()方法。

我努力的另一件事:集会。 您需要导入:

using System.Windows.Media;
using Microsoft.Kinect;
using Microsoft.Kinect.Toolkit.Fusion;
using System.Windows.Media.Media3D;

要获得[...]。Toolkit.Fusion程序集转到"添加引用"并点击"浏览"。然后,您将从MicrosoftSDK&Knesct / Developer Toolkit v1.8.0 / Assemblies指导汇编目录。导入程序集并将其添加到项目中。