none
Manipulate application whit two skeletons RRS feed

  • Question

  • Hi everybody, i need to track two skeletons and both can manipulate the apliaction, this is my code 

                                                  

        using (SkeletonFrame skeletonFrameData = e.OpenSkeletonFrame())
                    {
                        if (skeletonFrameData != null)
                        {
                            Skeleton[] allSkeletons = new Skeleton[skeletonFrameData.SkeletonArrayLength];


                            skeletonFrameData.CopySkeletonDataTo(allSkeletons);


                            foreach (Skeleton sd in allSkeletons)
                                if (
                                    sd.TrackingState == Nui.SkeletonTrackingState.Tracked)
                                {
                                    skeleton = sd;
                                    break;
                                }

                            if (skeleton == null)
                            {
                                return;
                            }

                            var nuiv = skeleton.Joints[JointType.HandRight].ScaleTo((int)ActualWidth, (int)ActualHeight, 0.50f, 0.30f).Position;
                            CursorPosition = new Point(nuiv.X, nuiv.Y);

    But just track a single skeleton, thanks for all and sorry for my english


    • Edited by Raulmc Thursday, November 1, 2012 6:45 PM
    Thursday, November 1, 2012 6:44 PM

Answers

  • Sorry there are some places in the code where I was in a hurry and did not pay attention to the variable names.  I think this is the corrected code:

     

    private int PlayerId1; private int PlayerId2; private boolean PlayerOneExists; private boolean PlayerTwoExists;

    if(!PlayerOneExists || !PlayerTwoExists)

    {

    foreach (Skeleton sd in allSkeletons) { if (sd == null || sd.TrackingState != Nui.SkeletonTrackingState.Tracked) { break; }

    if(!PlayerOneExists)

    { PlayerOneExists = true; PlayerId1 = sd.TrackingId; }

    else

    if(!PlayerTwoExists)

    {

    PlayerTwoExists = true; PlayerId2 = sd.TrackingId;

    } }

    }

    if(PlayerOneExists)

    {

    var sd = (from s in allSkeletons where s.TrackingState == SkeletonTrackingState.Tracked && s.TrackingId == PlayerId1 select s).FirstOrDefault(); if(sd != null)

    {

    var nuiv = sd.Joints[JointType.HandRight].ScaleTo((int)ActualWidth, (int)ActualHeight, 0.50f, 0.30f).Position;

    CursorPosition1 = new Point(nuiv.X, nuiv.Y);

    }

    else

    {

    PlayerOneExists = false;

    PlayerID1 = null;

    }

    if(PlayerTwoExists)

    {

    var sd = (from s in allSkeletons where s.TrackingState == SkeletonTrackingState.Tracked && s.TrackingId == PlayerId2 select s).FirstOrDefault(); if(sd != null)

    {

    var nuiv = sd.Joints[JointType.HandRight].ScaleTo((int)ActualWidth, (int)ActualHeight, 0.50f, 0.30f).Position;

    CursorPosition2 = new Point(nuiv.X, nuiv.Y);

    }

    }

    else

    {

    PlayerTwoExists = false;

    PlayerID2 = null;

    }

    Please mark "Propose As Answer" if this has fixed your problem. Thank you.


    Walt Smith Senior Architect

    • Marked as answer by Raulmc Sunday, November 11, 2012 6:40 PM
    Friday, November 9, 2012 1:32 PM
  • Yes you are correct.  They are integer values and you should set them to zero instead.  The point is that you should not keep a valid player ID in the variable if the player doesn't exist.

    Walt Smith Senior Architect

    • Marked as answer by Raulmc Tuesday, November 13, 2012 10:50 PM
    Tuesday, November 13, 2012 1:45 PM

All replies

  • I would like to help you, but could you re-state your request please?  At the top you say you want to track two skeletons, but at the bottom you say you want to track one skeleton.  What is it that you want to do exactly?

    Walt Smith Senior Architect

    Tuesday, November 6, 2012 9:20 PM
  • I would like to help you, but could you re-state your request please?  At the top you say you want to track two skeletons, but at the bottom you say you want to track one skeleton.  What is it that you want to do exactly?

    Walt Smith Senior Architect

    Sorry for my bad english, i try to say, at the end i just track 1 skeleton, and i want to track two skeletons.
    Wednesday, November 7, 2012 2:17 AM
  • I would like to help you, but could you re-state your request please?  At the top you say you want to track two skeletons, but at the bottom you say you want to track one skeleton.  What is it that you want to do exactly?


    Walt Smith Senior Architect

    Sorry for my bad english, i try to say, at the end i just track 1 skeleton, and i want to track two skeletons.

    My application must be manipulated by one or two persons, but until now, just one persons  can manipulate the application.

    in this part I call my skeleton and scale to a handrigth

    var nuiv = skeleton.Joints[JointType.HandRight].ScaleTo((int)ActualWidth, (int)ActualHeight, 0.50f, 0.30f).Position;
                            CursorPosition = new Point(nuiv.X, nuiv.Y);

    so,how I can made that the second skeleton can be tracked and scale to his rigth  hand to.

    Wednesday, November 7, 2012 2:29 AM
  • I think the main problem you are having is with your code structure.  If you move your skeleton tracking code into your for loop, I think you would be able to work out the rest of your issues.  It might look something like this:

    foreach (Skeleton sd in allSkeletons)

    {    if (sd == null || sd.TrackingState != Nui.SkeletonTrackingState.Tracked)
        {
              break;
         }

          var nuiv = sd.Joints[JointType.HandRight].ScaleTo((int)ActualWidth, (int)ActualHeight, 0.50f, 0.30f).Position;
          CursorPosition = new Point(nuiv.X, nuiv.Y);

    }


    Walt Smith Senior Architect

    Wednesday, November 7, 2012 2:48 PM
  • It´s works, but how I can track a second person, I mean, works whit the CursorPosition, but CursorPosition2 must be manipulated by the second person, but when i run the application the same person manipulated CursosPosition =(.

     var nuiv = sd.Joints[JointType.HandRight].ScaleTo((int)ActualWidth, (int)ActualHeight, 0.50f, 0.30f).Position;  //This is for the person one, works fine!
          CursorPosition = new Point(nuiv.X, nuiv.Y);

     var nuiv = sd.Joints[JointType.HandRight].ScaleTo((int)ActualWidth, (int)ActualHeight, 0.50f, 0.30f).Position; //This is for the person two, but when i run the application the                                                                                                                                                                            //person one manipulated to
          CursorPosition2 = new Point(nuiv.X, nuiv.Y);                                        

    What I try to say, is I have two cursorpisition,  CursorPosition  for the person one, and   CursorPosition2 for the person two.

    Thanks again!                                              

    Wednesday, November 7, 2012 8:08 PM
  • I think the main problem you are having is with your code structure.  If you move your skeleton tracking code into your for loop, I think you would be able to work out the rest of your issues.  It might look something like this:

    foreach (Skeleton sd in allSkeletons)

    {    if (sd == null || sd.TrackingState != Nui.SkeletonTrackingState.Tracked)
        {
              break;
         }

          var nuiv = sd.Joints[JointType.HandRight].ScaleTo((int)ActualWidth, (int)ActualHeight, 0.50f, 0.30f).Position;
          CursorPosition = new Point(nuiv.X, nuiv.Y);

    }


    Walt Smith Senior Architect

    It´s works, but how I can track a second person, I mean, works whit the CursorPosition, but CursorPosition2 must be manipulated by the second person, but when i run the application the same person manipulated CursosPosition =(.

     var nuiv = sd.Joints[JointType.HandRight].ScaleTo((int)ActualWidth, (int)ActualHeight, 0.50f, 0.30f).Position;  //This is for the person one, works fine!
          CursorPosition = new Point(nuiv.X, nuiv.Y);

     var nuiv = sd.Joints[JointType.HandRight].ScaleTo((int)ActualWidth, (int)ActualHeight, 0.50f, 0.30f).Position; //This is for the person two, but when i run the application the                                                                                                                                                                            //person one manipulated to
          CursorPosition2 = new Point(nuiv.X, nuiv.Y);                                        

    What I try to say, is I have two cursorpisition,  CursorPosition  for the person one, and   CursorPosition2 for the person two.

    Thanks again!    


    Wednesday, November 7, 2012 8:08 PM
  • In general, I think you will need to do the following steps:

    1) In the top of your code, create two variables to hold the tracking ID's of the two people you are interested in (PlayerIdOne and PlayerIdTwo) and two variables to hold boolean values that represent whether you are tracking that person (PlayerOneExists and PlayerTwoExists)

    private int PlayerId1;
    private int PlayerId2;
    private boolean PlayerOneExists;
    private boolean PlayerTwoExists; 

    2) The first time you see a person you are interested in tracking, capture the skeleton TrackingID for that person into the integer variable and set the boolean variable to true so that you can track the person

    if(!PlayerOneExists || !PlayerTwoExists)

    {

    foreach (Skeleton sd in allSkeletons) { if (sd == null || sd.TrackingState != Nui.SkeletonTrackingState.Tracked) { break; }

    if(!PlayerOneExists)

    { PlayerOneExists = true; PlayerIdOne = sd.TrackingId; }

    else

    if(!PlayerTwoExists)

    {

    PlayerTwoExists = true; PlayerIdTwo = sd.TrackingId;

    } }

    }


    3) From then on, if either PlayerOneExists or PlayerTwoExists,  instead of using foreach to go through all skeletons, use a linq query to select the skeleton that has the TrackingID that you are interested in, like this:

    if(PlayerOneExists)

    {

    var sd = (from s in allSkeletons where s.TrackingState == SkeletonTrackingState.Tracked && s.TrackingId == PlayerIdOne select s).FirstOrDefault(); if(sd != null)

    {

    var nuiv = sd.Joints[JointType.HandRight].ScaleTo((int)ActualWidth, (int)ActualHeight, 0.50f, 0.30f).Position;

    CursorPosition1 = new Point(nuiv.X, nuiv.Y);

    }

    else

    {

    PlayerOneExists = false;

    PlayerID1 = null;

    }

    if(PlayerTwoExists)

    {

    var sd = (from s in allSkeletons where s.TrackingState == SkeletonTrackingState.Tracked && s.TrackingId == PlayerIdTwo select s).FirstOrDefault(); if(sd != null)

    {

    var nuiv = sd.Joints[JointType.HandRight].ScaleTo((int)ActualWidth, (int)ActualHeight, 0.50f, 0.30f).Position;

    CursorPosition2 = new Point(nuiv.X, nuiv.Y);

    }

    }

    else

    {

    PlayerOneExists = false;

    PlayerID1 = null;

    }

    Of course, you will need to beef this up with some more code of your own to handle errors and such, but I hope you get the idea.  Hope this helps you.


    Walt Smith Senior Architect


    • Edited by Walt Smith Wednesday, November 7, 2012 8:57 PM
    Wednesday, November 7, 2012 8:55 PM
  • Thaks a lot, now works! but only when 2 persons are tracked, but I think I can handle by myself, thaks again, greetings from México.
    Friday, November 9, 2012 3:35 AM
  • In general, I think you will need to do the following steps:

    1) In the top of your code, create two variables to hold the tracking ID's of the two people you are interested in (PlayerIdOne and PlayerIdTwo) and two variables to hold boolean values that represent whether you are tracking that person (PlayerOneExists and PlayerTwoExists)

    private int PlayerId1;
    private int PlayerId2;
    private boolean PlayerOneExists;
    private boolean PlayerTwoExists; 

    2) The first time you see a person you are interested in tracking, capture the skeleton TrackingID for that person into the integer variable and set the boolean variable to true so that you can track the person

    if(!PlayerOneExists || !PlayerTwoExists)

    {

    foreach (Skeleton sd in allSkeletons) { if (sd == null || sd.TrackingState != Nui.SkeletonTrackingState.Tracked) { break; }

    if(!PlayerOneExists)

    { PlayerOneExists = true; PlayerIdOne = sd.TrackingId; }

    else

    if(!PlayerTwoExists)

    {

    PlayerTwoExists = true; PlayerIdTwo = sd.TrackingId;

    } }

    }


    3) From then on, if either PlayerOneExists or PlayerTwoExists,  instead of using foreach to go through all skeletons, use a linq query to select the skeleton that has the TrackingID that you are interested in, like this:

    if(PlayerOneExists)

    {

    var sd = (from s in allSkeletons where s.TrackingState == SkeletonTrackingState.Tracked && s.TrackingId == PlayerIdOne select s).FirstOrDefault(); if(sd != null)

    {

    var nuiv = sd.Joints[JointType.HandRight].ScaleTo((int)ActualWidth, (int)ActualHeight, 0.50f, 0.30f).Position;

    CursorPosition1 = new Point(nuiv.X, nuiv.Y);

    }

    else

    {

    PlayerOneExists = false;

    PlayerID1 = null;

    }

    if(PlayerTwoExists)

    {

    var sd = (from s in allSkeletons where s.TrackingState == SkeletonTrackingState.Tracked && s.TrackingId == PlayerIdTwo select s).FirstOrDefault(); if(sd != null)

    {

    var nuiv = sd.Joints[JointType.HandRight].ScaleTo((int)ActualWidth, (int)ActualHeight, 0.50f, 0.30f).Position;

    CursorPosition2 = new Point(nuiv.X, nuiv.Y);

    }

    }

    else

    {

    PlayerOneExists = false;

    PlayerID1 = null;

    }

    Of course, you will need to beef this up with some more code of your own to handle errors and such, but I hope you get the idea.  Hope this helps you.


    Walt Smith Senior Architect


    Yiu never use this variables Player1ID and Player2ID and 
    private int PlayerId1;
    private int PlayerId2;
    Friday, November 9, 2012 3:42 AM
  • Sorry there are some places in the code where I was in a hurry and did not pay attention to the variable names.  I think this is the corrected code:

     

    private int PlayerId1; private int PlayerId2; private boolean PlayerOneExists; private boolean PlayerTwoExists;

    if(!PlayerOneExists || !PlayerTwoExists)

    {

    foreach (Skeleton sd in allSkeletons) { if (sd == null || sd.TrackingState != Nui.SkeletonTrackingState.Tracked) { break; }

    if(!PlayerOneExists)

    { PlayerOneExists = true; PlayerId1 = sd.TrackingId; }

    else

    if(!PlayerTwoExists)

    {

    PlayerTwoExists = true; PlayerId2 = sd.TrackingId;

    } }

    }

    if(PlayerOneExists)

    {

    var sd = (from s in allSkeletons where s.TrackingState == SkeletonTrackingState.Tracked && s.TrackingId == PlayerId1 select s).FirstOrDefault(); if(sd != null)

    {

    var nuiv = sd.Joints[JointType.HandRight].ScaleTo((int)ActualWidth, (int)ActualHeight, 0.50f, 0.30f).Position;

    CursorPosition1 = new Point(nuiv.X, nuiv.Y);

    }

    else

    {

    PlayerOneExists = false;

    PlayerID1 = null;

    }

    if(PlayerTwoExists)

    {

    var sd = (from s in allSkeletons where s.TrackingState == SkeletonTrackingState.Tracked && s.TrackingId == PlayerId2 select s).FirstOrDefault(); if(sd != null)

    {

    var nuiv = sd.Joints[JointType.HandRight].ScaleTo((int)ActualWidth, (int)ActualHeight, 0.50f, 0.30f).Position;

    CursorPosition2 = new Point(nuiv.X, nuiv.Y);

    }

    }

    else

    {

    PlayerTwoExists = false;

    PlayerID2 = null;

    }

    Please mark "Propose As Answer" if this has fixed your problem. Thank you.


    Walt Smith Senior Architect

    • Marked as answer by Raulmc Sunday, November 11, 2012 6:40 PM
    Friday, November 9, 2012 1:32 PM
  • This Its my last question, PlayerID1 = null and  PlayerID2 = null; I assume is PlayerId1 and PlayerId2 , can't receive null values
    Tuesday, November 13, 2012 4:27 AM
  • Yes you are correct.  They are integer values and you should set them to zero instead.  The point is that you should not keep a valid player ID in the variable if the player doesn't exist.

    Walt Smith Senior Architect

    • Marked as answer by Raulmc Tuesday, November 13, 2012 10:50 PM
    Tuesday, November 13, 2012 1:45 PM
  • Yes you are correct.  They are integer values and you should set them to zero instead.  The point is that you should not keep a valid player ID in the variable if the player doesn't exist.

    Walt Smith Senior Architect

    Ok, thank you so much for the help! 
    Tuesday, November 13, 2012 10:51 PM