/// <summary> /// Updates the specified drawn skeleton with the new positions /// </summary> /// <param name="skeleton">The skeleton source.</param> /// <param name="drawing">The target drawing.</param> private void Update(Skeleton skeleton, SkeletonFigure figure) { foreach (Joint joint in skeleton.Joints) { var colorPoint = kinectSensor.CoordinateMapper.MapSkeletonPointToColorPoint(joint.Position, kinectSensor.ColorStream.Format); // Scale the ColorImagePoint position to the current window size var point = new System.Windows.Point((int)colorPoint.X / 640.0 * this.skeletonCanvas.ActualWidth, (int)colorPoint.Y / 480.0 * this.skeletonCanvas.ActualHeight); // update the position of that joint figure.Update(joint.JointType, point); instruction = figure.instruction; } }
public void SkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e) { using (SkeletonFrame skeletonFrame = e.OpenSkeletonFrame()) { if (skeletonFrame != null) { skeletonData = new Skeleton[6]; skeletonFrame.CopySkeletonDataTo(skeletonData); // Retrieves Skeleton objects with Tracked state var trackedSkeletons = skeletonData.Where(s => s.TrackingState == SkeletonTrackingState.Tracked); // By default, assume all the drawn skeletons are inactive foreach (SkeletonFigure skeleton in skeletons.Values) { skeleton.Status = ActivityState.Inactive; } foreach (Skeleton trackedSkeleton in trackedSkeletons) { SkeletonFigure skeletonFigure; // Checks if the tracked skeleton is already drawn. if (!skeletons.TryGetValue(trackedSkeleton.TrackingId, out skeletonFigure)) { // If not, create a new drawing on our canvas skeletonFigure = new SkeletonFigure(this.skeletonCanvas, this.color); try { skeletons.Add(trackedSkeleton.TrackingId, skeletonFigure); } catch (Exception err) { //shhhhh } activeSkel = trackedSkeleton; Canvas.SetTop(this.skeletonCanvas, 0); Canvas.SetLeft(this.skeletonCanvas, 0); } //update the depth of the tracked skeleton skelDepth = trackedSkeleton.Position.Z; skelL = trackedSkeleton.Joints[JointType.HandLeft].Position.X; skelR = trackedSkeleton.Joints[JointType.HandRight].Position.X; skelDepth = skelDepth * 1000; skelDepthPublic = skelDepth; skeletonFigure.setDepth(skelDepthPublic); skelL = (320 * (1 + skelL)) * 4; skelR = (320 * (1 + skelR)) * 4; // Update the drawing Update(trackedSkeleton, skeletonFigure); skeletonFigure.Status = ActivityState.Active; System.Windows.Media.Brush prevColor = this.color; //change color if (tooFarForward()) { this.color = blue; } else if (tooFarBack()) { this.color = red; } else { this.color = green; } //if color has been changed, change the skel colour if (!(this.color.Equals(prevColor))) { skeletonFigure.setColor(this.color); } } foreach (SkeletonFigure skeleton in skeletons.Values) { // Erase all the still inactive drawings. It means they are not tracked anymore. if (skeleton.Status == ActivityState.Inactive) { skeleton.Erase(); } } } else { return; } } }