Search code examples
javascriptaugmented-realityspark-ar-studio

Get the position of 3d objects in Facebook AR and changing them via script


I have a 3d object which i want to "move" from A to B via script. I am not too sure how to go about it; I don't understand the Facebook documents. Just a short example as a start would be great.

I assume something along the lines:

var object = Scene.root.find("object");
var lastPosX = object.transform.positionX.lastValue;
object.transform.positionX = //NOT SURE HOW TO PUT THE NEW POSITION

Solution

  • What you need to do is use the AnimationModule - here is a simple example of how to do that:

    const Animation = require('Animation');
    var obj = Scene.root.find("object");
    
    //set up the length of the animations, 1000 = 1 second
    var driver = Animation.timeDriver({durationMilliseconds: 1000});
    
    //define the starting and ending values (start at 0, go to 100)
    var sampler = Animation.samplers.linear(0, 100);
    
    //create an animation signal to control the object x position
    obj.transform.x = Animation.animate(driver, sampler);
    
    //start the animation
    driver.start();
    

    Animation in ARS, like many other things, is based around the concept of "Reactive Programming" and working with "Signals" which are values that change over time. It is essential to get a good grasp of what a signal is and how it works to write useful code in ARS. Read through this for an introductory overview: https://developers.facebook.com/docs/ar-studio/scripting/basics

    The above is a very basic example, but there is much more interesting, advanced, and complex effects that you can achieve using the AnimationModule, take a look at the documentation here for more information: https://developers.facebook.com/docs/ar-studio/reference/classes/animationmodule/

    Hope this helps!