Search code examples
augmented-realityspark-ar-studio

Distance and width in Spark AR scripting


I'm having a hard time navigating Spark AR documentation and I couldn't find an answer to this: I have three planes in a Scene. Two of them are moving, one of them static.

  • How can I get the distance between those two planes?

  • How can I change the width of the third plane to that distance?

Thanks!


Solution

  • You're in luck because the Reactive Module just so happens to have a built-in distance function!

    //import the Reactive module
    const Reactive = require('Reactive');
    
    //find your planes
    let plane1 = Scene.root.find('plane1');
    let plane2 = Scene.root.find('plane2');
    let plane3 = Scene.root.find('plane3');
    
    //create point signals
    let point1 = Reactive.pack3(plane1.transform.x, plane1.transform.y, plane1.transform.z);
    let point2 = Reactive.pack3(plane2.transform.x, plane2.transform.y, plane2.transform.z);
    
    //get the distance between the two points
    let distance = Reactive.distance(point1, point2);
    
    //apply scale
    plane3.transform.scaleX = distance;
    

    Good luck!