Search code examples
javascriptspark-ar-studio

How to get the x position of the Facebook AR hand tracker?


For hand trackers, their patch has info X, Y, and Z and when a hand is tracked there are values that appear. How do i get those value via script? is it possible? There aren't any hand tracking script examples.

I have the following so far to determine if a hand is detected, still not sure about getting the position:

//im assuming this is the correct module
const HandTracking = require('HandTracking');
const Scene = require('Scene');

//this is to check if a hand is detected
HandTracking.count.monitor().subscribe(function(e){
   if(e.newValue){
       Debug.log("hand found");
   }
})

UPDATE

So im assuming it has something like this :

var thehand = HandTracking.hand(0);
var posx = thehand.cameraTransform.x.lastValue;
Debug.log(posx);

But this way is deprecated? need to use subscribeWithSnapshot but I'm not sure how to implement this.

Also it needs to be dynamic(?) because the value changes with every movement.


Solution

  • You are on the right track, what you need is simply:

    var posx = thehand.cameraTransform.x;
    

    This will give you a signal for the hand x position. To debug this value you can use:

    Diagnostics.watch("hand x",posx);
    

    A signal is a value that changes over time, and it can be bound to another object property, for example:

    mySceneObject.transform.x =  thehand.cameraTransform.x;
    

    This will bind the hand x position signal to the object x position, so that the object will move with the hand.

    Read more about signals here: https://developers.facebook.com/docs/ar-studio/scripting/reactive

    They are a very powerful tool and essential knowledge for scripting in AR Studio.

    Hope this helps!