/// <summary> /// Enables skeleton tracking. /// </summary> public void Enable() { var res = NuiSkeletonTrackingEnable(IntPtr.Zero, 1); if (res != HRESULT.S_OK) throw new KinectException("Failed to open skeletonstream"); for (int i = 0; i < skeletonData.Length; i++) skeletonData[i] = new Skeleton(); IsEnabled = true; }
internal SkeletonFrame OpenNextFrame() { var frame = new NuiSkeletonFrame(); // Get the next NuiSkeletonFrame. var res = NuiSkeletonGetNextFrame(0, ref frame); if (res == HRESULT.S_FALSE) return null; if (res != HRESULT.S_OK) throw new KinectException("Failed to get next frame"); // Apply TransformSmoothing. if (TransformSmooth) res = NuiTransformSmooth(ref frame, ref smoothParameters); if (res == HRESULT.S_FALSE) return null; if (res != HRESULT.S_OK) throw new KinectException("Failed to get next frame"); // Read every native SkeletonData object into a Skeleton object. for (int index = 0; index < frame.skeletonData.Length; index++) { var nuiData = frame.skeletonData[index]; if (nuiData.eTrackingState == SkeletonTrackingState.Tracked) { var joints = new List<Joint>(); for (int i = 0; i < nuiData.skeletonPositions.Length; i++) joints.Add(new Joint((JointType)i, nuiData.skeletonPositions[i], nuiData.eSkeletonPositionTrackingState[i])); var collection = new JointCollection(joints); skeletonData[index] = new Skeleton(nuiData.eTrackingState, (int)nuiData.dwTrackingID, (int)nuiData.dwEnrollmentIndex, nuiData.position, collection); } else skeletonData[index] = new Skeleton(); } return new SkeletonFrame(frame.liTimeStamp, (int)frame.dwFrameNumber, frame.vFloorClipPlane, skeletonData); }
private static void CheckMoves(Skeleton skeleton) { leftHand = skeleton.Joints[JointType.HandLeft]; rightHand = skeleton.Joints[JointType.HandRight]; shoulderCenter = skeleton.Joints[JointType.ShoulderCenter]; trackingId = skeleton.TrackingID; // Check if left hand is active. if (shoulderCenter.Position.Z - leftHand.Position.Z < 0.25) { leftHandActive = false; leftHandPositions.Clear(); } else if (!rightHandActive) { leftHandActive = true; leftHandPositions.Add(leftHand.Position); } // Check if right hand is active. if (shoulderCenter.Position.Z - rightHand.Position.Z < 0.25) { rightHandActive = false; rightHandPositions.Clear(); } else if (!leftHandActive) { rightHandActive = true; rightHandPositions.Add(rightHand.Position); } // When no hands are active, reset all starting points and reset swiping directions. if (!leftHandActive && !rightHandActive) { horizontalStartPoint = null; verticalStartPoint = null; horizontalDirection = SwipeDirection.None; verticalDirection = SwipeDirection.None; } else { if (rightHandActive) { // Check for horizontal swipes if the right hand is higher than the spine. if (rightHand.Position.Y > skeleton.Joints[JointType.Spine].Position.Y) CheckHorizontalSwipes(Hand.Right); // Check for vertical swipes. if (SwipeUp != null || SwipeDown != null) CheckVerticalSwipes(Hand.Right); } if (leftHandActive) { // Check for horizontal swipes if the left hand is higher than the spine. if (leftHand.Position.Y > skeleton.Joints[JointType.Spine].Position.Y) CheckHorizontalSwipes(Hand.Left); // Check for vertical swipes. if (SwipeUp != null || SwipeDown != null) CheckVerticalSwipes(Hand.Left); } } }
internal SkeletonFrame(long timeStamp, int frameNumber, SkeletonPoint floorClipPlane, Skeleton[] skeleton) { Timestamp = timeStamp; FrameNumber = frameNumber; FloorClipPlane = new Tuple<float, float, float, float>(floorClipPlane.X, floorClipPlane.Y, floorClipPlane.Z, floorClipPlane.W); Skeletons = skeleton; }
private static void CheckMoves(Skeleton skeleton) { var positions = skeleton.Joints.Select(j => j.Position).ToArray(); var newShoulderCenter = positions[(int)JointType.ShoulderCenter]; if (Math.Abs(newShoulderCenter.X - shoulderCenter.X) > 0.05) shoulderCenter = newShoulderCenter; hipCenter = positions[(int)JointType.HipCenter]; handRight = positions[(int)JointType.HandRight]; handLeft = positions[(int)JointType.HandLeft]; trackingID = skeleton.TrackingID; rightHandDifference = shoulderCenter.Z - handRight.Z; leftHandDifference = shoulderCenter.Z - handLeft.Z; // Convert the X and Y positions to a position on the screen. var leftX = Math.Round(ConvertX(handLeft.X + 0.45f), 0); var leftY = Math.Round(ConvertY(handLeft.Y), 0); var leftZ = handLeft.Z; if (leftX < 0) leftX = 0; if (leftY < 0) leftY = 0; leftArgs = new MoveEventArgs(trackingID, leftX, leftY, handLeft.Z); var rightX = Math.Round(ConvertX(handRight.X + 0.15f), 0); var rightY = Math.Round(ConvertY(handRight.Y), 0); var rightZ = handRight.Z; if (rightX < 0) rightX = 0; if (rightY < 0) rightY = 0; rightArgs = new MoveEventArgs(trackingID, rightX, rightY, handRight.Z); // Check all moves. CheckRightHandMove(); CheckLeftHandMove(); CheckScale(); CheckRotate(); CheckTranslate(); if (Transform != null) CheckTransform(); }