Jump to content

Is RTree improving performance?


kurhlaa
 Share

Recommended Posts

Hello!

From docs and code comments I see that Phaser uses RTree for faster calculations. It should improve the performance very much. Recommendation is to use it with <5000 bodies, and it is enabled by default.

But I did a simple test - calculate how long it takes to run 1 physics step:

/*
 * @method Phaser.Physics.Arcade.World#step
 */
step: function (delta)
{
    var t0 = performance.now();

    ...

    var t1 = performance.now();
    console.log("Call took " + (t1 - t0) + " ms");
}

Then I create a player and a group of 1000 bodies:

 

function create ()
{
    var player = this.physics.add.sprite(50, 50, 'dude');

    player.setVelocity(500, 0);
    player.setBounce(1, 0);
    player.setCollideWorldBounds(true);


    var group = this.physics.add.group({
                key: 'boom',
                active: false,
                frame: [ 0, 1 ],
                frameQuantity: 500
    });

    Phaser.Actions.GridAlign(group.getChildren(), {
                width: 30,
                height: 20,
                cellWidth: 16,
                cellHeight: 16,
                x: 0,
                y: 0
    });

    this.physics.add.collider(player, group);
}

 

And the numbers I see most of time are ~0.8 - 1.0 ms

Then I set useTree: false and try again. The numbers are ~0.4 - 0.5 ms. That means RTree at this moment makes Phaser to run slower.

I also tried to comment out recreating RTree on every frame:

// this.tree.clear();
// this.tree.load(bodies);

.. this makes numbers pretty close to disabled RTree. 

 

I'm not sure whether such testing is correct, but here it shows that RTree currently is not an improvement. Or there should be a special environment to make RTree useful?

Link to comment
Share on other sites

Probably no, because World.js has:

/**
 * The spatial index of Dynamic Bodies.
 *
 * @name Phaser.Physics.Arcade.World#tree
 * @type {Phaser.Structs.RTree}
 * @since 3.0.0
 */
this.tree = new RTree(this.maxEntries);

/**
 * The spatial index of Static Bodies.
 *
 * @name Phaser.Physics.Arcade.World#staticTree
 * @type {Phaser.Structs.RTree}
 * @since 3.0.0
 */
this.staticTree = new RTree(this.maxEntries);

...
...
...

this.tree.load(bodies); // on every frame

.. and bodies are dynamic bodies. But it doesn't really matter - in this test I take a default Phaser and create normal bodies like in any example, and enabled RTree makes the update loop run slower. There are no separate settings for static and dynamic bodies

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...