From 188d6239a3e7642916ac3f58cffc6091c3ea574c Mon Sep 17 00:00:00 2001 From: Richard Davey Date: Sun, 1 Sep 2013 03:15:27 +0100 Subject: [PATCH] Edge points in and working. --- examples/camera2.php | 28 ++++++++++------- examples/camera3.php | 55 ++++++++++++++++++++++++++++++++ src/gameobjects/Sprite.js | 66 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 137 insertions(+), 12 deletions(-) create mode 100644 examples/camera3.php diff --git a/examples/camera2.php b/examples/camera2.php index 26edb11a..87295cec 100644 --- a/examples/camera2.php +++ b/examples/camera2.php @@ -69,11 +69,7 @@ br = points[2]; bl = points[3]; - s.anchor.setTo(0, 0); - s.angle = 5; - - // get the distance between top-left and bottom-right - // distance = Phaser.Math.distance(0,0,s.width,s.height); + s.anchor.setTo(2, 0.5); // PIXI worldTransform order: @@ -94,10 +90,10 @@ s.angle += 0.5; - if (s.scale.x < 2) + if (s.scale.x > -2) { - s.scale.x += 0.01; - s.scale.y += 0.01; + s.scale.x -= 0.01; + s.scale.y -= 0.01; } } @@ -108,17 +104,23 @@ // var p1 = getLocalPosition(midpoint.x, midpoint.y, s); + var offsetX = s.anchor.x * s.width; + var offsetY = s.anchor.y * s.height; + + var sx = s.x - offsetX; + var sy = s.y - offsetY; + // top left - var p1 = getLocalPosition(s.x, s.y, s); + var p1 = getLocalPosition(sx, sy, s); // top right - var p2 = getLocalPosition(s.x + s.width, s.y, s); + var p2 = getLocalPosition(sx + s.width, sy, s); // bottom left - var p3 = getLocalPosition(s.x, s.y + s.height, s); + var p3 = getLocalPosition(sx, sy + s.height, s); // bottom right - var p4 = getLocalPosition(s.x + s.width, s.y + s.height, s); + var p4 = getLocalPosition(sx + s.width, sy + s.height, s); p1.add(s.x, s.y); p2.add(s.x, s.y); @@ -139,6 +141,8 @@ game.debug.renderText('ty: ' + tr.y, 32, 265); game.debug.renderText('px: ' + p2.x, 32, 280); game.debug.renderText('py: ' + p2.y, 32, 295); + game.debug.renderText('ox: ' + offsetX, 32, 350); + game.debug.renderText('oy: ' + offsetY, 32, 370); } diff --git a/examples/camera3.php b/examples/camera3.php new file mode 100644 index 00000000..5fddaf7e --- /dev/null +++ b/examples/camera3.php @@ -0,0 +1,55 @@ + + + + phaser.js - a new beginning + + + + + + + + \ No newline at end of file diff --git a/src/gameobjects/Sprite.js b/src/gameobjects/Sprite.js index 84b13fdb..079b0f34 100644 --- a/src/gameobjects/Sprite.js +++ b/src/gameobjects/Sprite.js @@ -106,6 +106,23 @@ Phaser.Sprite = function (game, x, y, key, frame) { this.worldView = new Phaser.Rectangle(x, y, this.width, this.height); + // Edge points + this.topLeft = new Phaser.Point(x, y); + this.topRight = new Phaser.Point(x + this.width, y); + this.bottomRight = new Phaser.Point(x + this.width, y + this.height); + this.bottomLeft = new Phaser.Point(x, y + this.height); + + this.offset = new Phaser.Point(); + + // help avoid gc spikes by using temp. vars + this._a00 = 0; + this._a01 = 0; + this._a02 = 0; + this._a10 = 0; + this._a11 = 0; + this._a12 = 0; + this._id = 0; + }; Phaser.Sprite.prototype = Object.create(PIXI.Sprite.prototype); @@ -123,10 +140,59 @@ Phaser.Sprite.prototype.update = function() { this.position.x = this._x - (this.game.world.camera.x * this.scrollFactor.x); this.position.y = this._y - (this.game.world.camera.y * this.scrollFactor.y); + // Update the edge points (sx, sy) + this.offset.setTo(this.x - (this.anchor.x * this.width), this.y - (this.anchor.y * this.height)); + + // var sx = s.x - offsetX; + // var sy = s.y - offsetY; + + // top left + this.getLocalPosition(this.topLeft, this.offset.x, this.offset.y); + // var p1 = getLocalPosition(sx, sy, s); + + // top right + this.getLocalPosition(this.topRight, this.offset.x + this.width, this.offset.y); + // var p2 = getLocalPosition(sx + s.width, sy, s); + + // bottom left + this.getLocalPosition(this.bottomLeft, this.offset.x, this.offset.y + this.height); + // var p3 = getLocalPosition(sx, sy + s.height, s); + + // bottom right + this.getLocalPosition(this.bottomRight, this.offset.x + this.width, this.offset.y + this.height); + // var p4 = getLocalPosition(sx + s.width, sy + s.height, s); + + // p1.add(s.x, s.y); + // p2.add(s.x, s.y); + // p3.add(s.x, s.y); + // p4.add(s.x, s.y); + // this.checkBounds(); } +Phaser.Sprite.prototype.getLocalPosition = function(p, x, y) { + + this._a00 = this.worldTransform[0]; // scaleX + this._a01 = this.worldTransform[1]; // skewY + this._a02 = this.worldTransform[2]; // translateX + this._a10 = this.worldTransform[3]; // skewX + this._a11 = this.worldTransform[4]; // scaleY + this._a12 = this.worldTransform[5]; // translateY + + this._a01 *= -1; + this._a10 *= -1; + + this._id = 1 / (this._a00 * this._a11 + this._a01 * -this._a10); + + p.x = (this._a11 * this._id * x + -this._a01 * this._id * y + (this._a12 * this._a01 - this._a02 * this._a11) * this._id) * this.scale.x; + p.y = (this._a00 * this._id * y + -this._a10 * this._id * x + (-this._a12 * this._a00 + this._a02 * this._a10) * this._id) * this.scale.y; + p.add(this.x, this.y); + + return p; + +} + Object.defineProperty(Phaser.Sprite.prototype, 'angle', { get: function() {