Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions api/animate.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,18 +168,16 @@ export const flockAnimate = {
"rotation",
fps,
BABYLON.Animation.ANIMATIONTYPE_VECTOR3,
loop || reverse
loop
? BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE
: BABYLON.Animation.ANIMATIONLOOPMODE_CONSTANT,
);

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") {
Expand All @@ -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();
});
});
Expand Down
26 changes: 26 additions & 0 deletions tests/animate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down