认识到两个骷髅Kinect

时间:2014-06-19 09:49:06

标签: c# kinect kinect-interaction

我使用kinect制作游戏(pong),我可以识别一个骨架,我可以做出手势来控制左手棒,但是当我尝试玩两个玩家时,不要认出其他玩家的骨架。这是我到目前为止所做的:

private void SensorSkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
        {
            Skeleton[] skeletons = new Skeleton[0];

            using (SkeletonFrame skeletonFrame = e.OpenSkeletonFrame())
            {
                if (skeletonFrame != null)
                {
                    skeletons = new Skeleton[skeletonFrame.SkeletonArrayLength];
                    skeletonFrame.CopySkeletonDataTo(skeletons);
                }
                if (skeletons.Length != 0)
                {
                    foreach (Skeleton skel in skeletons)
                    {
                        if (skel.TrackingState == SkeletonTrackingState.Tracked)
                        {
                            this.tracked(skel);
                            this.trackedLeft(skel);
                        }
                    }
                }
            }
        }

        public void tracked(Skeleton skeleton)
        {
            Joint jHandRight = skeleton.Joints[JointType.HandRight];
            Joint jHipCenter = skeleton.Joints[JointType.HipCenter];
            if ((jHipCenter.Position.Z - jHandRight.Position.Z) > 0.2)
            {
                //Consider hand raised in front of them
                //System.Diagnostics.Debug.WriteLine("Hand: Raised");
                //MessageBox.Show("POR FAVORRRRRRRR");
                //movement[0] = false;
                movement[0] = true;
                movement[1] = false;
            }
            else
            {
                //Hand is lowered by the users side
                //System.Diagnostics.Debug.WriteLine("Hand: Lowered");
                //MessageBox.Show("A SERRRRIIIIIOOOOOOOOOOOOOO");
                //movement[1] = false;
                movement[1] = true;
                movement[0] = false;
            }
        }

有人可以帮助我。

1 个答案:

答案 0 :(得分:1)

你需要一种方法来区分骨架一和二。有关如何执行此操作,请参阅Kinect user Detection。然后,您可以将第一个和第二个玩家的骨架传递给两个不同的方法。我使用玩家ID,因为如果一帧或两帧的骨架丢失,它们的ID保持不变

int id1 = 0, id2 = 0;

... 

if (skeletons.Length != 0)
{
    foreach (Skeleton skel in skeletons)
    {
         if (skel.TrackingState == SkeletonTrackingState.Tracked)
         {
                if (skel.TrackingID == id1)
                    this.tracked(skel);
                else if (skel.TrackingID == id2)
                    this.trackedLeft(skel);
                else
                {
                     if (id1 != 0 && id2 == 0)
                         id2 = skel.TrackingID;
                     else if (id2 != 0 && id1 == 0)
                         id1 = skel.TrackingID;
                }
         }
     }
  }