Skip to content

Instantly share code, notes, and snippets.

@kiriappeee
Created July 2, 2012 07:05
Show Gist options
  • Save kiriappeee/3031597 to your computer and use it in GitHub Desktop.
Save kiriappeee/3031597 to your computer and use it in GitHub Desktop.
Making a kinect recognize the single player skeleton only
/*Having made a kinect game as promotions for our company, we discovered that there was a problem in taking this game out into
public and that was that if anyone stood behind the main player the game would just break in the sense
it wouldn't know how to track*/
//the code given by microsoft to activate the kinect tracking is as follows.
playerSkeleton = (from s in skeletonData
where s.TrackingState == SkeletonTrackingState.Tracked
select s).FirstOrDefault();
//The problem here is that the kinect has its own way of stacking skeletons and deciding which one is 'First'.
//The solution was to get it to attach to a certain ID at the start of the game and track only that from the start.
//Declare a variable called skeletonID and set it to -1 to indicate that no skeleton has been found
skeletonData = new Skeleton[skeletonFrame.SkeletonArrayLength];
skeletonFrame.CopySkeletonDataTo(this.skeletonData);
Skeleton playerSkeleton;
if (skeletonID == -1)
{
//this code is used if no skeleton has been detected
playerSkeleton = (from s in skeletonData
where s.TrackingState == SkeletonTrackingState.Tracked
select s).FirstOrDefault();
}
else
{
playerSkeleton = (from s in skeletonData
where s.TrackingState == SkeletonTrackingState.Tracked && s.TrackingId == skeletonID
select s).FirstOrDefault(); //once the skeleton has been detected we select only the one with the skeletonID stored (below)
}
if (playerSkeleton != null)
{
skeletonID = playerSkeleton.TrackingId; //if the skeleton gotten from the kinect isn't null then store that id. Yes this is being overwritten constantly. Maybe bad coding? I need to experiment and see what happens when I add a condition not to reset if it has already been set.
Joint rightHand = playerSkeleton.Joints[JointType.HandRight];
rightHandPosition = new Vector2((((0.5f * rightHand.Position.X) + 0.5f) * (1200)), (((-0.5f * rightHand.Position.Y) + 0.5f) * (900)));
Joint leftHand = playerSkeleton.Joints[JointType.HandLeft];
leftHandPosition = new Vector2((((0.5f * leftHand.Position.X) + 0.5f) * (1200)), (((-0.5f * leftHand.Position.Y) + 0.5f) * (900)));
}
else
{
skeletonID = -1; //if the skeleton is null (say when someone walks off the screen. Then the id used returns null. ) we reset the value in this line
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment