diff --git a/api/animate.js b/api/animate.js index d0ba89ed..e0403200 100644 --- a/api/animate.js +++ b/api/animate.js @@ -168,7 +168,7 @@ export const flockAnimate = { "rotation", fps, BABYLON.Animation.ANIMATIONTYPE_VECTOR3, - loop || reverse + loop ? BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE : BABYLON.Animation.ANIMATIONLOOPMODE_CONSTANT, ); @@ -176,10 +176,8 @@ export const flockAnimate = { const rotateKeys = [ { frame: 0, value: startRotation }, { frame: frames, value: targetRotation }, + ...(reverse ? [{ frame: frames * 2, value: startRotation }] : []), ]; - if (reverse || loop) { - rotateKeys.push({ frame: frames * 2, value: startRotation }); - } rotateAnimation.setKeys(rotateKeys); if (easing !== "Linear") { @@ -205,13 +203,17 @@ export const flockAnimate = { mesh, [rotateAnimation], 0, - reverse || loop ? frames * 2 : frames, + reverse ? frames * 2 : frames, loop, ); animatable.onAnimationEndObservable.add(() => { flock.scene.onAfterAnimationsObservable.remove(syncObserver); - if (!reverse) mesh.rotation = targetRotation.clone(); + if (reverse) { + mesh.rotation = startRotation.clone(); + } else { + mesh.rotation = targetRotation.clone(); + } resolve(); }); }); diff --git a/tests/animate.test.js b/tests/animate.test.js index 8c191444..35813308 100644 --- a/tests/animate.test.js +++ b/tests/animate.test.js @@ -211,6 +211,32 @@ export function runAnimateTests(flock) { expect(actualDuration).to.be.at.least(180); // Allow some tolerance expect(actualDuration).to.be.at.most(300); }); + + it("should return to the start rotation when reverse is true", async function () { + const boxId = "rotateAnimReverse"; + await flock.createBox(boxId, { + width: 1, + height: 1, + depth: 1, + position: [0, 0, 0], + }); + boxIds.push(boxId); + + const mesh = flock.scene.getMeshByName(boxId); + expect(mesh).to.exist; + + const initialRotation = mesh.rotation.clone(); + + await flock.rotateAnim(boxId, { + y: 90, + duration: 0.1, + reverse: true, + }); + + expect(mesh.rotation.x).to.be.closeTo(initialRotation.x, 0.01); + expect(mesh.rotation.y).to.be.closeTo(initialRotation.y, 0.01); + expect(mesh.rotation.z).to.be.closeTo(initialRotation.z, 0.01); + }); }); describe("animateProperty function", function () {