Search code examples
javascriptspark-ar-studiocannon.js

Sending a vector3 to script from patches is not working


So I'm making a ball bounce in spark AR with cannon.js. Everything was working fine until I wanted to get the position of the forehead from the facetracker via the patch editor to a script.

Error:

    Error:Exception in HostFunction: valueOf() called on a Signal. This probably means that you are
    trying to perform an arithmetic operation on a signal like +, -, *, etc. Use functions .add, .sub(), 
    etc on the signal instead or .subscribeWithSnapshot() on an EventSource to get the signal's current value on a callback.
at ScalarSignal::valueOf (native)
{
"line": 4841,
"column": 19,
"sourceURL": "cannon.js"
}

My patches that send the vector3 of the forehead to the script.

this piece of code is giving the error:

var pos = Patches.getVectorValue('HeadPos');
groundBody.position.set(pos);

I sadly can't find anything online about sending a vector3 from the patches to a 'Working' value in the script, Does somebody know how to send a vector3 value ta a script and the use it as a value?


Solution

  • I've found a selution when working with cannon.js. cannon.js has a kind of update function so you can use .pinLastValue() because it does this every frame in order to update the physics.

    My code:

    // Create time interval loop for cannon 
    Time.setInterval(function (time) {
        if (lastTime !== undefined) {
            let dt = (time - lastTime) / 1000;
            world.step(fixedTimeStep, dt, maxSubSteps)
    
         // Get the head position values from the patches
         var headVal1 = Patches.getScalarValue("HeadPosX").pinLastValue();
         var headVal2 = Patches.getScalarValue("HeadPosY").pinLastValue();
         var headVal3 = Patches.getScalarValue("HeadPosZ").pinLastValue();
    
         // Set the position of the head hitbox to the headposition in the physics world
         HeadBody.position.x = headVal1;
         HeadBody.position.y = headVal2;
         HeadBody.position.z = headVal3;
       }
    
        lastTime = time
    }, timeInterval);
    

    This code gets the x, y and z values individually from the patches where I send these values individually as well. I could have done it as a Vector3 from both sides as well, but I thought this looked nicer and made it easier to edit the values individually through the patches instead of packing it as a Vector3 again.