phaserjs / phaser

Phaser is a fun, free and fast 2D game framework for making HTML5 games for desktop and mobile web browsers, supporting Canvas and WebGL rendering.
https://phaser.io
MIT License
36.96k stars 7.09k forks source link

Tweening Matter.js object scale results in memory leak #3248

Closed kirillbunin closed 6 years ago

kirillbunin commented 6 years ago

I'm trying to tween scale of an matter.js image object, code goes like this:

    const ball = this.matter.add.image(400, gameHeight - 105, 'ball', null);
    ball.setCircle();
    ball.setScale(1);
    ball.setFriction(0.005);
    ball.setBounce(0.6);

From working with Matter.js, I was used to tween props of an object (using TweenLite) like this:

    let ballScale = {scale: 1};
    this.tweens.add({
        targets: ballScale,
        scale: 0.5,
        ease: 'Linear',
        duration: 800,
        onUpdate: () => { ball.setScale(ballScale.scale) }
    })

Using plain object as a middle man, because setScale preforms more than just replaces scaleXand scaleY props of an object. With Phaser 3.1.0, it resolves in memory leak.

If I try to tween just the props:

    this.tweens.add({
        targets: ball,
        scaleX: 0.5,
        scaleY: 0.5,
        ease: 'Linear',
        duration: 800,
    })

Object disappears and game crashes.

kirillbunin commented 6 years ago

I played with it some more, and found out that ball.setScale(0.5) will be always relative to the current size of the ball. Therefore, reapplying it, will always cut the size to the half of the current size. ball.setScale(1) does nothing.

Is this on purpouse? I would think, it would be more intuitive if ball.setScale(1) return the size of the object to it's original size, and ball.setScale(0.5) would be always half of the original size.

I noticed there is ScaleMode but I'm yet to find out how it works.

photonstorm commented 6 years ago

I'm afraid this is how Matter itself scales bodies. When you scale via setScale it simply passes those values onto Matter to use. I'm not sure there's an easy way to change this behavior, as looking at the Matter code the scale is applied across the board to all vertices with no previous sizes retained in order to have a 'base' scale. Open to suggestions for this one, but might just have to be a case of "that's how it works".

photonstorm commented 6 years ago

Just to say that we finally addressed this and you can find the fix in the master branch, it will be part of the 3.12 release.