Skip to content

Commit 47e1b1b

Browse files
committed
Sprite optimisations.
1 parent 455b18b commit 47e1b1b

5 files changed

Lines changed: 30 additions & 35 deletions

File tree

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,14 @@ Version 1.0.7 (in progress in the dev branch)
8383
* Change: We've removed the scrollFactor property from all Game Objects. Sorry, but the new Camera system doesn't work with it and it caused all kinds of issues anyway. We will sort out a replacement for it at a later date.
8484
* Change: World now extends Phaser.Group. As a result we've updated GameObjectFactory and other classes that linked to it. If you have anywhere in your code that used to reference world.group you can just remove 'group' from that. So before, world.group.add() is now just world.add().
8585
* Change: The Camera has been completely revamped. Rather than adjusting the position of all display objects (bad) it now just shifts the position of the single world container (good!), this is much quicker and also stops the game objects positions from self-adjusting all the time, allowing for them to be properly nested with other containers.
86-
* New: Direction constants have been added to Sprites and adjust based on body motion. Access them via
86+
* New: Direction constants have been added to Sprites and adjust based on body motion.
8787
* World.randomX/Y now returns values anywhere in the world.bounds range (if set, otherwise 0), including negative values.
88+
* Fixed a bug in the Sprite transform cache check that caused the skew/scale cache to get constantly invalidated - now only updates as needed, significant performance increase!
89+
* Brand new Sprite.update loop handler. Combined with the transform cache fix and further optimisations this is now much quicker to execute.
90+
* Made Sprite.body optional and added in checks, so you can safely null the Sprite body object if using your own physics system and not impact rendering.
8891

8992

9093
* TODO: addMarker hh:mm:ss:ms
91-
* TODO: Direction constants
9294

9395
Version 1.0.6 (September 24th 2013)
9496

examples/world/move around world.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function preload() {
1515

1616
game.stage.backgroundColor = '#007236';
1717

18-
game.load.image('mushroom', 'assets/sprites/mushroom.png');
18+
game.load.image('mushroom', 'assets/sprites/mushroom2.png');
1919
game.load.image('phaser', 'assets/sprites/sonic_havok_sanity.png');
2020

2121
}
@@ -30,7 +30,7 @@ function create() {
3030

3131
for (var i = 0; i < 100; i++)
3232
{
33-
// var s = game.add.sprite(game.world.randomX, game.world.randomY, 'mushroom');
33+
var s = game.add.sprite(game.world.randomX, game.world.randomY, 'mushroom');
3434
// console.log(s.x, s.y);
3535
}
3636

@@ -43,7 +43,7 @@ function create() {
4343

4444
function update() {
4545

46-
// d.angle++;
46+
d.angle += 1;
4747

4848
if (cursors.up.isDown)
4949
{
@@ -96,6 +96,7 @@ function update() {
9696
function render() {
9797

9898
game.debug.renderCameraInfo(game.camera, 32, 32);
99+
game.debug.renderSpriteInfo(d, 32, 200);
99100
// game.debug.renderWorldTransformInfo(d, 32, 200);
100101
// game.debug.renderLocalTransformInfo(d, 32, 400);
101102
game.debug.renderSpriteCorners(d, false, true);

src/gameobjects/Sprite.js

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,6 @@ Phaser.Sprite = function (game, x, y, key, frame) {
198198
// Input specific transform cache
199199
i01: -1, i10: -1, idi: -1,
200200

201-
// World transform cache
202-
w01: -1, w10: -1,
203-
204201
// Bounds check
205202
left: null, right: null, top: null, bottom: null,
206203

@@ -338,15 +335,11 @@ Phaser.Sprite.prototype.preUpdate = function() {
338335
this.prevX = this.x;
339336
this.prevY = this.y;
340337

341-
if (this.worldTransform[2] != this._cache.a02 || this.worldTransform[5] != this._cache.a12 || this.worldTransform[0] != this._cache.a00 || this.worldTransform[1] != this._cache.a01 || this.worldTransform[3] != this._cache.a10 || this.worldTransform[4] != this._cache.a11)
342-
{
343-
this.updateCache();
344-
}
338+
this.updateCache();
345339

346340
// Re-run the camera visibility check
347-
if (this._cache.dirty || this._cache.first)
341+
if (this._cache.dirty)
348342
{
349-
this._cache.first = false;
350343
this._cache.cameraVisible = Phaser.Rectangle.intersects(this.game.world.camera.screenView, this.bounds, 0);
351344

352345
if (this.autoCull == true)
@@ -356,7 +349,10 @@ Phaser.Sprite.prototype.preUpdate = function() {
356349
}
357350

358351
// Update our physics bounds
359-
this.body.updateBounds(this.center.x, this.center.y, this._cache.scaleX, this._cache.scaleY);
352+
if (this.body)
353+
{
354+
this.body.updateBounds(this.center.x, this.center.y, this._cache.scaleX, this._cache.scaleY);
355+
}
360356
}
361357

362358
if (this.body)
@@ -372,38 +368,30 @@ Phaser.Sprite.prototype.updateCache = function() {
372368
// |b d ty|
373369
// |0 0 1|
374370

375-
// Only update the values we need
376-
// if (this.worldTransform[0] != this._cache.a00 || this.worldTransform[1] != this._cache.w01 || this.worldTransform[3] != this._cache.w10 || this.worldTransform[4] != this._cache.a11)
377-
if (this.worldTransform[1] != this._cache.w01 || this.worldTransform[3] != this._cache.w10)
371+
if (this.worldTransform[1] != this._cache.i01 || this.worldTransform[3] != this._cache.i10)
378372
{
379-
// Non-modified
380-
this._cache.w01 = this.worldTransform[1]; // skewY c
381-
this._cache.w10 = this.worldTransform[3]; // skewX b
382-
383373
this._cache.a00 = this.worldTransform[0]; // scaleX a
384374
this._cache.a01 = this.worldTransform[1]; // skewY c
385-
this._cache.i01 = this.worldTransform[1]; // skewY c
386375
this._cache.a10 = this.worldTransform[3]; // skewX b
387-
this._cache.i10 = this.worldTransform[3]; // skewX b
388376
this._cache.a11 = this.worldTransform[4]; // scaleY d
389377

378+
this._cache.i01 = this.worldTransform[1]; // skewY c (remains non-modified for input checks)
379+
this._cache.i10 = this.worldTransform[3]; // skewX b (remains non-modified for input checks)
380+
390381
this._cache.scaleX = Math.sqrt((this._cache.a00 * this._cache.a00) + (this._cache.a01 * this._cache.a01)); // round this off a bit?
391382
this._cache.scaleY = Math.sqrt((this._cache.a10 * this._cache.a10) + (this._cache.a11 * this._cache.a11)); // round this off a bit?
392383

393384
this._cache.a01 *= -1;
394385
this._cache.a10 *= -1;
395386

396387
this._cache.dirty = true;
397-
console.log('cache1');
398-
399388
}
400389

401390
if (this.worldTransform[2] != this._cache.a02 || this.worldTransform[5] != this._cache.a12)
402391
{
403392
this._cache.a02 = this.worldTransform[2]; // translateX tx
404393
this._cache.a12 = this.worldTransform[5]; // translateY ty
405394
this._cache.dirty = true;
406-
console.log('cache2');
407395
}
408396

409397
// Frame updated?

src/math/Math.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,16 +522,21 @@ Phaser.Math = {
522522
wrap: function (value, min, max) {
523523

524524
var range = max - min;
525+
525526
if (range <= 0)
526527
{
527528
return 0;
528529
}
530+
529531
var result = (value - min) % range;
532+
530533
if (result < 0)
531534
{
532535
result += range;
533536
}
537+
534538
return result + min;
539+
535540
},
536541

537542
/**

src/utils/Debug.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -454,9 +454,9 @@ Phaser.Utils.Debug.prototype = {
454454
this.start(x, y, color);
455455

456456
this.line('Sprite: ' + ' (' + sprite.width + ' x ' + sprite.height + ') anchor: ' + sprite.anchor.x + ' x ' + sprite.anchor.y);
457-
this.line('x: ' + sprite.x.toFixed(1) + ' y: ' + sprite.y.toFixed(1) + ' rotation: ' + sprite.rotation.toFixed(1));
458-
this.line('visible: ' + sprite.visible);
459-
this.line('in camera: ' + sprite.inCamera);
457+
this.line('x: ' + sprite.x.toFixed(1) + ' y: ' + sprite.y.toFixed(1));
458+
this.line('angle: ' + sprite.angle + ' rotation: ' + sprite.rotation.toFixed(1));
459+
this.line('visible: ' + sprite.visible + ' in camera: ' + sprite.inCamera);
460460
this.line('body x: ' + sprite.body.x.toFixed(1) + ' y: ' + sprite.body.y.toFixed(1));
461461

462462
// 0 = scaleX
@@ -466,18 +466,17 @@ Phaser.Utils.Debug.prototype = {
466466
// 4 = scaleY
467467
// 5 = translateY
468468

469-
470469
// this.line('id: ' + sprite._id);
471470
// this.line('scale x: ' + sprite.worldTransform[0]);
472471
// this.line('scale y: ' + sprite.worldTransform[4]);
473472
// this.line('tx: ' + sprite.worldTransform[2]);
474473
// this.line('ty: ' + sprite.worldTransform[5]);
475474
// this.line('skew x: ' + sprite.worldTransform[3]);
476475
// this.line('skew y: ' + sprite.worldTransform[1]);
477-
this.line('dx: ' + sprite.body.deltaX());
478-
this.line('dy: ' + sprite.body.deltaY());
479-
this.line('sdx: ' + sprite.deltaX());
480-
this.line('sdy: ' + sprite.deltaY());
476+
// this.line('dx: ' + sprite.body.deltaX());
477+
// this.line('dy: ' + sprite.body.deltaY());
478+
// this.line('sdx: ' + sprite.deltaX());
479+
// this.line('sdy: ' + sprite.deltaY());
481480

482481
// this.line('inCamera: ' + this.game.renderer.spriteRenderer.inCamera(this.game.camera, sprite));
483482

0 commit comments

Comments
 (0)