Loading...
Logo
Processing Forum
Hi guys,
I was trying to use the Z position of a hand to emulate a button press. So far when I try it using one hand it works really well, however, when I try to get it to work with a second hand, only one of the hands work. (I know it can be more efficient by moving everything except the fill out of the if statements, but I kept those in there just in case I want to change the sizes or something.)

irz and ilz are the initial Z positions of the hands when they are first recognized by onCreateHands and rz and lz are the current Z positions. As of now, the code works fine with one hand, but the other hand will either stay pressed or unpressed. If i comment one of the sections out, it works fine as well.
Copy code
  1. if (rz - irz > 0) {
  2.     pushStyle();
  3.     fill(60);
  4.     ellipse(rx, ry, 10, 10);
  5.     popStyle();
  6.     rpressed = true;
  7.   }
  8.   else {
  9.     pushStyle();
  10.     noFill();
  11.     ellipse(rx, ry, 10, 10);
  12.     popStyle();
  13.     rpressed = false;
  14.   }
  15.   
  16.   if (lz - ilz > 0) {
  17.     pushStyle();
  18.     fill(60);
  19.     ellipse(lx, ly, 10, 10);
  20.     popStyle();
  21.     lpressed = true;
  22.   }
  23.   else {
  24.     pushStyle();
  25.     noFill();
  26.     ellipse(lx, ly, 10, 10);
  27.     popStyle();
  28.     lpressed = false;
  29.   }

I tried outputting the values of rz - irz and lz - ilz and the numbers range from small negative values to small positive values (around -8 to 8) for lz - ilz. But rz - irz outputs numbers from around 8-30 depending on each time I run it and is never consistent. Also, when I comment out the code for the lz-ilz, the values for rz-irz look just fine and it operates as intended. Is there a reason tracking both Z positions throws off one hand? And is there a way to get it to work?

Thanks!

Replies(2)

You are properly creating two hands elsewhere in the code?
Hi Jesse, thanks for the response,

The x and y positions of the two hands track correctly as long as they don't get lost. (Initially I tried doing with hashmaps and PVectors, but I couldn't seem to use any of the PVector methods (position.x) outside of the class even if it was declared above void setup().)

Long story short, it's probably not a very elegant solution, but it works exactly how I want as long as the hands don't get lost in terms of tracking the X and Y positions.

Anyways, here is the code for that part:


Copy code
  1. void onCreateHands(int handId, PVector position, float time)
  2. {
  3.   context.convertRealWorldToProjective(position, position);
  4.   //handPositions.add(position);
  5.   testMap.put(handId, position);
  6.   
  7.   lHand = (PVector)testMap.get(1);
  8.   rHand = (PVector)testMap.get(2);
  9.   if (handId ==1) {
  10.     rz = position.z;
  11.     irz = map(rz, 50, 2000, 50, 5);
  12.   }
  13.   else if (handId == 2) {
  14.     lz = position.z;
  15.     ilz = map(lz, 50, 2000, 50, 5);
  16.   }
  17.     
  18.   
  19. }

  20. void onUpdateHands(int handId, PVector position, float time)
  21. {
  22.   context.convertRealWorldToProjective(position, position);
  23.   orx = rx;
  24.   ory = ry;
  25.   olx = lx;
  26.   oly = ly;

  27.   //handPositions.add(position);
  28.   if (handId ==1) {
  29.     rx = position.x;
  30.     ry = position.y;
  31.     rz = position.z;
  32.   }
  33.   
  34.   else if (handId ==2) {
  35.     lx = position.x;
  36.     ly = position.y;
  37.     lz = position.z;
  38.   }

  39.   
  40.   else if (rx == orx && ry == ory)  {
  41.     rx = position.x;
  42.     ry = position.y;
  43.     rz = position.z;
  44.     //print("rx");
  45.   }
  46.   
  47.   else if (lx == olx && ly == oly)  {
  48.     lx = position.x;
  49.     ly = position.y;
  50.     lz = position.z;
  51.     //println("lx");
  52.   }
  53.   
  54.   
  55.   else {
  56.    println("error"); 
  57.   }
  58.   //println("rz: " + rz);
  59.   //println("lz: " + lz);
  60.   rz = map(rz, 50, 2000, 50, 5);
  61.   lz = map(lz, 50, 2000, 50, 5);
  62.   


  63.   //testMap.put(handId, indHands);
  64.   
  65.   //lHand = (PVector)testMap.get(1);
  66.   //rHand = (PVector)testMap.get(2);
  67. }