示例#1
0
        /// <summary>
        /// Gets called if the RGB, depth and skeleton frame is ready
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mainKinectSensor_AllFramesReady(object sender, AllFramesReadyEventArgs e)
        {
            if (!tutorial)
            {
                return;
            }


            Skeleton mainSkeleton = getFirstSkeleton(e);    //get the first skeleton

            if (mainSkeleton == null)
            {
                return;                                                       //return if the Kinect does not recognize any skeletons
            }
            CoordinateMapper mapper = new CoordinateMapper(mainKinectSensor); //mapper between skeleton and depth image

            if (!corrected)
            {
                DepthImagePoint pointHead = mapper.MapSkeletonPointToDepthPoint(mainSkeleton.Joints[JointType.Head].Position, DepthImageFormat.Resolution640x480Fps30);

                if (pointHead.Y < 100)
                {
                    mainKinectSensor.ElevationAngle += Convert.ToInt32((120 - pointHead.Y) / TILT_FACTOR);
                    corrected = true;
                    return;
                }
                if (pointHead.Y > 140)
                {
                    mainKinectSensor.ElevationAngle -= Convert.ToInt32((pointHead.Y - 120) / TILT_FACTOR);
                    corrected = true;
                    return;
                }
            }
            corrected = true;

            DepthImagePoint pointRight = mapper.MapSkeletonPointToDepthPoint(mainSkeleton.Joints[JointType.HandRight].Position, DepthImageFormat.Resolution640x480Fps30); //get the right hand
            DepthImagePoint pointLeft  = mapper.MapSkeletonPointToDepthPoint(mainSkeleton.Joints[JointType.HandLeft].Position, DepthImageFormat.Resolution640x480Fps30);  //get the left hand

            if (rotation & Math.Abs(pointLeft.X - pointRight.X) < HANDS_DISTANCE)
            {
                mainWindow.setHandMarkers(pointRight.X, pointRight.Y, pointLeft.X, pointLeft.Y, true);
                if (!playing)
                {
                    return;
                }
                if (oldAngle == -1)
                {
                    oldAngle = pointLeft.Y - pointRight.Y;
                }
                room.rotateCurrentElement(((pointLeft.Y - pointRight.Y) - oldAngle) / 2);
                oldAngle = pointLeft.Y - pointRight.Y;
            }
            else
            {
                oldAngle = -1;
                mainWindow.setHandMarkers(pointRight.X, pointRight.Y, pointLeft.X, pointLeft.Y, false);
            }

            if (!playing)
            {
                return;
            }

            if (absolute)
            {
                if (zMax == -1 && zMin == -1)
                {
                    zMax = pointRight.Depth + 400;
                    zMin = pointRight.Depth - 400;
                }

                if (pointRight.Depth > zMax)     //adapt the Z-Range if the player goes out of it
                {
                    zMax = pointRight.Depth;
                    zMin = pointRight.Depth - 800;
                }
                if (pointRight.Depth < zMin)
                {
                    zMax = pointRight.Depth + 800;
                    zMin = pointRight.Depth;
                }

                Console.WriteLine(pointRight.Depth - zMin);
                room.translateCurrentElementAbsolute(pointRight.X, pointRight.Y, pointRight.Depth - zMin);  //absolute movement
            }
            else
            {
                if (oldx == -1)             //relative movement
                {
                    oldx = pointRight.X;
                }
                if (oldy == -1)
                {
                    oldy = pointRight.Y;
                }
                if (oldz == -1)
                {
                    oldz = pointRight.Depth;
                }

                room.translateCurrentElementRelative((pointRight.X - oldx) * Room.FACTOR_X, 0, 0);
                room.translateCurrentElementRelative(0, (oldy - pointRight.Y) * Room.FACTOR_Y, 0);
                room.translateCurrentElementRelative(0, 0, (pointRight.Depth - oldz) * Room.FACTOR_Z);

                oldx = pointRight.X;
                oldy = pointRight.Y;
                oldz = pointRight.Depth;
            }
        }