[SOLVED] Get position of entity from animation in future

Is there a way to get the position/rotation out of an animated entity 1 second into the future?

In my use case I have a vehicle with LEDs, which should blink some time before the vehicle is actually turning.

I don’t want to use animation events or something similiar, as this would involve manual setup.

After going through the playcanvas source code. I’ve got this working pretty well. For anyone wanting to do something similiar here is how to get the rotation of an entity 2 seconds into the future:

var AgvLed = pc.createScript('agvLed');

AgvLed.prototype.initialize = function () {
    this.setupAnimationVars('IDLE');

    this.app.on('chapter-started', function (stateName) {
        this.setupAnimationVars(stateName);
    }, this);

    this.quat = pc.Quat.IDENTITY.clone();
};

AgvLed.prototype.update = function (dt) {
    this.animTrack.eval(this.entity.anim.baseLayer.activeStateCurrentTime + 2, this.animSnapShot);
    this.quat.set(this.animSnapShot._results[this.resultIndex][0], this.animSnapShot._results[this.resultIndex][1], this.animSnapShot._results[this.resultIndex][2], this.animSnapShot._results[this.resultIndex][3]);
    console.log(this.quat.getEulerAngles());
};

AgvLed.prototype.setupAnimationVars = function (stateName) {
    const assetObj = this.entity.anim.baseLayer.getAnimationAsset(stateName);
    const asset = this.app.assets.get(assetObj.asset);
    this.animTrack = asset.resource;
    this.animSnapShot = new pc.AnimSnapshot(this.animTrack);
    this.resultIndex = this.getResultIndex('RootNode/AGV_Rig/AGV_Root', 'localRotation');
};

AgvLed.prototype.getResultIndex = function (entityPath, propertyPath) {
    return this.animTrack.curves.findIndex((animCurve) => {
        return animCurve.paths[0].entityPath.join('/') === entityPath && animCurve.paths[0].propertyPath.join('/') === propertyPath;
    });
};

There is a bit of custom code in there so you have to adjust for your use case. Especially the entitypath 'RootNode/AGV_Rig/AGV_Root' and stateName.

1 Like

Well done and thanks a lot for sharing! :pray: