Skip to content

Commit fef28c6

Browse files
committed
Optimised update loop to reduce calculations if the transform doesn't change.
1 parent 6ef8982 commit fef28c6

2 files changed

Lines changed: 48 additions & 22 deletions

File tree

examples/camera3.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ function create() {
2929

3030
s.addChild(s2);
3131

32-
// s.angle = 45;
3332
s.scale.x = 2;
3433
s.scale.y = 2;
3534
s2.scale.x = 0.5;

src/gameobjects/Sprite.js

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -114,17 +114,24 @@ Phaser.Sprite = function (game, x, y, key, frame) {
114114

115115
this.offset = new Phaser.Point();
116116

117-
// help avoid gc spikes by using temp. vars
117+
this._dirty = true;
118+
119+
// transform cache
118120
this._a00 = 0;
119121
this._a01 = 0;
120122
this._a02 = 0;
121123
this._a10 = 0;
122124
this._a11 = 0;
123125
this._a12 = 0;
124126
this._id = 0;
127+
128+
// The actual scale X value based on the worldTransform
125129
this._sx = 0;
130+
// The actual scale Y value based on the worldTransform
126131
this._sy = 0;
132+
// The width of the image, based on the un-modified frame size multiplied by the final calculated scale size
127133
this._sw = 0;
134+
// The height of the image, based on the un-modified frame size multiplied by the final calculated scale size
128135
this._sh = 0;
129136

130137
};
@@ -133,10 +140,12 @@ Phaser.Sprite.prototype = Object.create(PIXI.Sprite.prototype);
133140
Phaser.Sprite.prototype.constructor = Phaser.Sprite;
134141

135142
/**
136-
* Automatically called after update() by the game loop for all 'alive' objects.
143+
* Automatically called by the game loop.
137144
*/
138145
Phaser.Sprite.prototype.update = function() {
139146

147+
this._dirty = false;
148+
140149
this.animations.update();
141150

142151
this.worldView.setTo(this._x, this._y, this.width, this.height);
@@ -149,30 +158,48 @@ Phaser.Sprite.prototype.update = function() {
149158
// |0 0 1|
150159

151160
// Cache our transform values
152-
this._a00 = this.worldTransform[0]; // scaleX a
153-
this._a01 = this.worldTransform[1]; // skewY c
154-
this._a02 = this.worldTransform[2]; // translateX tx
155-
this._a10 = this.worldTransform[3]; // skewX b
156-
this._a11 = this.worldTransform[4]; // scaleY d
157-
this._a12 = this.worldTransform[5]; // translateY ty
158-
159-
this._sx = Math.sqrt((this._a00 * this._a00) + (this._a01 * this._a01));
160-
this._sy = Math.sqrt((this._a10 * this._a10) + (this._a11 * this._a11));
161+
if (this.worldTransform[0] != this._a00 || this.worldTransform[1] != this._a01)
162+
{
163+
this._a00 = this.worldTransform[0]; // scaleX a
164+
this._a01 = this.worldTransform[1]; // skewY c
165+
this._sx = Math.sqrt((this._a00 * this._a00) + (this._a01 * this._a01));
166+
this._a01 *= -1;
167+
this._dirty = true;
168+
}
169+
170+
if (this.worldTransform[3] != this._a10 || this.worldTransform[4] != this._a11)
171+
{
172+
this._a10 = this.worldTransform[3]; // skewX b
173+
this._a11 = this.worldTransform[4]; // scaleY d
174+
this._sy = Math.sqrt((this._a10 * this._a10) + (this._a11 * this._a11));
175+
this._a10 *= -1;
176+
this._dirty = true;
177+
}
178+
179+
if (this.worldTransform[2] != this._a02 || this.worldTransform[5] != this._a12)
180+
{
181+
this._a02 = this.worldTransform[2]; // translateX tx
182+
this._a12 = this.worldTransform[5]; // translateY ty
183+
this._dirty = true;
184+
}
185+
186+
// If the frame has changed we ought to set _dirty
161187
this._sw = this.texture.frame.width * this._sx;
162188
this._sh = this.texture.frame.height * this._sy;
163189

164-
this._a01 *= -1;
165-
this._a10 *= -1;
166-
167-
this._id = 1 / (this._a00 * this._a11 + this._a01 * -this._a10);
190+
if (this._dirty)
191+
{
192+
this._id = 1 / (this._a00 * this._a11 + this._a01 * -this._a10);
168193

169-
// Update the edge points
170-
this.offset.setTo(this._a02 - (this.anchor.x * this._sw), this._a12 - (this.anchor.y * this._sh));
194+
// Update the edge points
195+
this.offset.setTo(this._a02 - (this.anchor.x * this._sw), this._a12 - (this.anchor.y * this._sh));
171196

172-
this.getLocalPosition(this.topLeft, this.offset.x, this.offset.y);
173-
this.getLocalPosition(this.topRight, this.offset.x + this._sw, this.offset.y);
174-
this.getLocalPosition(this.bottomLeft, this.offset.x, this.offset.y + this._sh);
175-
this.getLocalPosition(this.bottomRight, this.offset.x + this._sw, this.offset.y + this._sh);
197+
// Do we need all 4 edge points? It might be better to just calculate the center and apply the circle for a bounds check
198+
this.getLocalPosition(this.topLeft, this.offset.x, this.offset.y);
199+
this.getLocalPosition(this.topRight, this.offset.x + this._sw, this.offset.y);
200+
this.getLocalPosition(this.bottomLeft, this.offset.x, this.offset.y + this._sh);
201+
this.getLocalPosition(this.bottomRight, this.offset.x + this._sw, this.offset.y + this._sh);
202+
}
176203

177204
// this.checkBounds();
178205

0 commit comments

Comments
 (0)