From c2533d1146fcfeb9ec36bc9b7486e2238b8cadab Mon Sep 17 00:00:00 2001 From: Richard Davey Date: Tue, 11 Jun 2013 23:30:35 +0100 Subject: [PATCH] Finally a fully working bounding box that respects scale and rotation - the "in camera" check is now 100% accurate :) --- Phaser/components/Transform.ts | 210 ++++++++++++---------- Phaser/renderers/CanvasRenderer.ts | 3 +- Phaser/utils/SpriteUtils.ts | 103 ++--------- Tests/misc/point1.js | 93 +++------- Tests/misc/point1.ts | 100 ++++------- Tests/phaser.js | 275 +++++++++++++---------------- build/phaser.d.ts | 47 ++--- build/phaser.js | 275 +++++++++++++---------------- 8 files changed, 459 insertions(+), 647 deletions(-) diff --git a/Phaser/components/Transform.ts b/Phaser/components/Transform.ts index bc9830da..4f3976ae 100644 --- a/Phaser/components/Transform.ts +++ b/Phaser/components/Transform.ts @@ -25,62 +25,83 @@ module Phaser.Components { this.scale = new Phaser.Vec2(1, 1); this.skew = new Phaser.Vec2; + this.center = new Phaser.Point; + this.upperLeft = new Phaser.Point; + this.upperRight = new Phaser.Point; + this.bottomLeft = new Phaser.Point; + this.bottomRight = new Phaser.Point; + + this._pos = new Phaser.Point; + this._scale = new Phaser.Point; + this._size = new Phaser.Point; + this._halfSize = new Phaser.Point; + this._offset = new Phaser.Point; + this._origin = new Phaser.Point; + this._sc = new Phaser.Point; + this._scA = new Phaser.Point; + } private _rotation: number; - private _cachedSin: number; - private _cachedCos: number; - private _cachedRotation: number; - private _cachedScaleX: number; - private _cachedScaleY: number; - private _cachedAngle: number; - private _cachedAngleToCenter: number; - private _cachedDistance: number; - private _cachedWidth: number; - private _cachedHeight: number; - private _cachedHalfWidth: number; - private _cachedHalfHeight: number; - private _cachedCosAngle: number; - private _cachedSinAngle: number; - private _cachedOffsetX: number; - private _cachedOffsetY: number; - private _cachedOriginX: number; - private _cachedOriginY: number; - private _cachedCenterX: number; - private _cachedCenterY: number; + // Cache vars + private _pos: Phaser.Point; + private _scale: Phaser.Point; + private _size: Phaser.Point; + private _halfSize: Phaser.Point; + private _offset: Phaser.Point; + private _origin: Phaser.Point; + private _sc: Phaser.Point; + private _scA: Phaser.Point; + private _angle: number; + private _distance: number; + private _prevRotation: number; + + public center: Phaser.Point; + public upperLeft: Phaser.Point; + public upperRight: Phaser.Point; + public bottomLeft: Phaser.Point; + public bottomRight: Phaser.Point; public local: Mat3; public setCache() { - this._cachedHalfWidth = this.parent.width / 2; - this._cachedHalfHeight = this.parent.height / 2; - this._cachedOffsetX = this.origin.x * this.parent.width; - this._cachedOffsetY = this.origin.y * this.parent.height; - this._cachedAngleToCenter = Math.atan2(this.halfHeight - this._cachedOffsetY, this.halfWidth - this._cachedOffsetX); - this._cachedDistance = Math.sqrt(((this._cachedOffsetX - this._cachedHalfWidth) * (this._cachedOffsetX - this._cachedHalfWidth)) + ((this._cachedOffsetY - this._cachedHalfHeight) * (this._cachedOffsetY - this._cachedHalfHeight))); - this._cachedWidth = this.parent.width; - this._cachedHeight = this.parent.height; - this._cachedOriginX = this.origin.x; - this._cachedOriginY = this.origin.y; - this._cachedSin = Math.sin((this.rotation + this.rotationOffset) * GameMath.DEG_TO_RAD); - this._cachedCos = Math.cos((this.rotation + this.rotationOffset) * GameMath.DEG_TO_RAD); - this._cachedCosAngle = Math.cos((this.rotation + this.rotationOffset) * GameMath.DEG_TO_RAD + this._cachedAngleToCenter); - this._cachedSinAngle = Math.sin((this.rotation + this.rotationOffset) * GameMath.DEG_TO_RAD + this._cachedAngleToCenter); - this._cachedRotation = this.rotation; + this._pos.x = this.parent.x; + this._pos.y = this.parent.y; + this._halfSize.x = this.parent.width / 2; + this._halfSize.y = this.parent.height / 2; + this._offset.x = this.origin.x * this.parent.width; + this._offset.y = this.origin.y * this.parent.height; + this._angle = Math.atan2(this.halfHeight - this._offset.y, this.halfWidth - this._offset.x); + this._distance = Math.sqrt(((this._offset.x - this._halfSize.x) * (this._offset.x - this._halfSize.x)) + ((this._offset.y - this._halfSize.y) * (this._offset.y - this._halfSize.y))); + this._size.x = this.parent.width; + this._size.y = this.parent.height; + this._origin.x = this.origin.x; + this._origin.y = this.origin.y; + this._sc.x = Math.sin((this.rotation + this.rotationOffset) * GameMath.DEG_TO_RAD); + this._sc.y = Math.cos((this.rotation + this.rotationOffset) * GameMath.DEG_TO_RAD); + this._scA.y = Math.cos((this.rotation + this.rotationOffset) * GameMath.DEG_TO_RAD + this._angle); + this._scA.x = Math.sin((this.rotation + this.rotationOffset) * GameMath.DEG_TO_RAD + this._angle); + this._prevRotation = this.rotation; if (this.parent.texture && this.parent.texture.renderRotation) { - this._cachedSin = Math.sin((this.rotation + this.rotationOffset) * GameMath.DEG_TO_RAD); - this._cachedCos = Math.cos((this.rotation + this.rotationOffset) * GameMath.DEG_TO_RAD); + this._sc.x = Math.sin((this.rotation + this.rotationOffset) * GameMath.DEG_TO_RAD); + this._sc.y = Math.cos((this.rotation + this.rotationOffset) * GameMath.DEG_TO_RAD); } else { - this._cachedSin = 0; - this._cachedCos = 1; + this._sc.x = 0; + this._sc.y = 1; } + this.center.setTo(this.center.x, this.center.y); + this.upperLeft.setTo(this.center.x - this._halfSize.x * this._sc.y + this._halfSize.y * this._sc.x, this.center.y - this._halfSize.y * this._sc.y - this._halfSize.x * this._sc.x); + this.upperRight.setTo(this.center.x + this._halfSize.x * this._sc.y + this._halfSize.y * this._sc.x, this.center.y - this._halfSize.y * this._sc.y + this._halfSize.x * this._sc.x); + this.bottomLeft.setTo(this.center.x - this._halfSize.x * this._sc.y - this._halfSize.y * this._sc.x, this.center.y + this._halfSize.y * this._sc.y - this._halfSize.x * this._sc.x); + this.bottomRight.setTo(this.center.x + this._halfSize.x * this._sc.y - this._halfSize.y * this._sc.x, this.center.y + this._halfSize.y * this._sc.y + this._halfSize.x * this._sc.x); + } public update() { @@ -89,73 +110,84 @@ module Phaser.Components { var dirty: bool = false; // 1) Height or Width change (also triggered by a change in scale) or an Origin change - if (this.parent.width !== this._cachedWidth || this.parent.height !== this._cachedHeight || this.origin.x !== this._cachedOriginX|| this.origin.y !== this._cachedOriginY) + if (this.parent.width !== this._size.x || this.parent.height !== this._size.y || this.origin.x !== this._origin.x|| this.origin.y !== this._origin.y) { - this._cachedHalfWidth = this.parent.width / 2; - this._cachedHalfHeight = this.parent.height / 2; - this._cachedOffsetX = this.origin.x * this.parent.width; - this._cachedOffsetY = this.origin.y * this.parent.height; - this._cachedAngleToCenter = Math.atan2(this.halfHeight - this._cachedOffsetY, this.halfWidth - this._cachedOffsetX); - this._cachedDistance = Math.sqrt(((this._cachedOffsetX - this._cachedHalfWidth) * (this._cachedOffsetX - this._cachedHalfWidth)) + ((this._cachedOffsetY - this._cachedHalfHeight) * (this._cachedOffsetY - this._cachedHalfHeight))); + this._halfSize.x = this.parent.width / 2; + this._halfSize.y = this.parent.height / 2; + this._offset.x = this.origin.x * this.parent.width; + this._offset.y = this.origin.y * this.parent.height; + this._angle = Math.atan2(this.halfHeight - this._offset.y, this.halfWidth - this._offset.x); + this._distance = Math.sqrt(((this._offset.x - this._halfSize.x) * (this._offset.x - this._halfSize.x)) + ((this._offset.y - this._halfSize.y) * (this._offset.y - this._halfSize.y))); // Store - this._cachedWidth = this.parent.width; - this._cachedHeight = this.parent.height; - this._cachedOriginX = this.origin.x; - this._cachedOriginY = this.origin.y; + this._size.x = this.parent.width; + this._size.y = this.parent.height; + this._origin.x = this.origin.x; + this._origin.y = this.origin.y; dirty = true; } // 2) Rotation change - if (this.rotation != this._cachedRotation) + if (this.rotation != this._prevRotation) { - this._cachedSin = Math.sin((this.rotation + this.rotationOffset) * GameMath.DEG_TO_RAD); - this._cachedCos = Math.cos((this.rotation + this.rotationOffset) * GameMath.DEG_TO_RAD); - this._cachedCosAngle = Math.cos((this.rotation + this.rotationOffset) * GameMath.DEG_TO_RAD + this._cachedAngleToCenter); - this._cachedSinAngle = Math.sin((this.rotation + this.rotationOffset) * GameMath.DEG_TO_RAD + this._cachedAngleToCenter); + this._sc.x = Math.sin((this.rotation + this.rotationOffset) * GameMath.DEG_TO_RAD); + this._sc.y = Math.cos((this.rotation + this.rotationOffset) * GameMath.DEG_TO_RAD); + this._scA.y = Math.cos((this.rotation + this.rotationOffset) * GameMath.DEG_TO_RAD + this._angle); + this._scA.x = Math.sin((this.rotation + this.rotationOffset) * GameMath.DEG_TO_RAD + this._angle); if (this.parent.texture.renderRotation) { - this._cachedSin = Math.sin((this.rotation + this.rotationOffset) * GameMath.DEG_TO_RAD); - this._cachedCos = Math.cos((this.rotation + this.rotationOffset) * GameMath.DEG_TO_RAD); + this._sc.x = Math.sin((this.rotation + this.rotationOffset) * GameMath.DEG_TO_RAD); + this._sc.y = Math.cos((this.rotation + this.rotationOffset) * GameMath.DEG_TO_RAD); } else { - this._cachedSin = 0; - this._cachedCos = 1; + this._sc.x = 0; + this._sc.y = 1; } // Store - this._cachedRotation = this.rotation; + this._prevRotation = this.rotation; dirty = true; } - if (dirty) + // If it has moved, updated the edges and center + if (dirty || this.parent.x != this._pos.x || this.parent.y != this._pos.y) { - this._cachedCenterX = this.parent.x + this._cachedDistance * this._cachedCosAngle; - this._cachedCenterY = this.parent.y + this._cachedDistance * this._cachedSinAngle; + this.center.x = this.parent.x + this._distance * this._scA.y; + this.center.y = this.parent.y + this._distance * this._scA.x; + + this.center.setTo(this.center.x, this.center.y); + + this.upperLeft.setTo(this.center.x - this._halfSize.x * this._sc.y + this._halfSize.y * this._sc.x, this.center.y - this._halfSize.y * this._sc.y - this._halfSize.x * this._sc.x); + this.upperRight.setTo(this.center.x + this._halfSize.x * this._sc.y + this._halfSize.y * this._sc.x, this.center.y - this._halfSize.y * this._sc.y + this._halfSize.x * this._sc.x); + this.bottomLeft.setTo(this.center.x - this._halfSize.x * this._sc.y - this._halfSize.y * this._sc.x, this.center.y + this._halfSize.y * this._sc.y - this._halfSize.x * this._sc.x); + this.bottomRight.setTo(this.center.x + this._halfSize.x * this._sc.y - this._halfSize.y * this._sc.x, this.center.y + this._halfSize.y * this._sc.y + this._halfSize.x * this._sc.x); + + this._pos.x = this.parent.x; + this._pos.y = this.parent.y; } // Scale and Skew if (this.parent.texture.flippedX) { - this.local.data[0] = this._cachedCos * -this.scale.x; - this.local.data[3] = (this._cachedSin * -this.scale.x) + this.skew.x; + this.local.data[0] = this._sc.y * -this.scale.x; + this.local.data[3] = (this._sc.x * -this.scale.x) + this.skew.x; } else { - this.local.data[0] = this._cachedCos * this.scale.x; - this.local.data[3] = (this._cachedSin * this.scale.x) + this.skew.x; + this.local.data[0] = this._sc.y * this.scale.x; + this.local.data[3] = (this._sc.x * this.scale.x) + this.skew.x; } if (this.parent.texture.flippedY) { - this.local.data[4] = this._cachedCos * -this.scale.y; - this.local.data[1] = -(this._cachedSin * -this.scale.y) + this.skew.y; + this.local.data[4] = this._sc.y * -this.scale.y; + this.local.data[1] = -(this._sc.x * -this.scale.y) + this.skew.y; } else { - this.local.data[4] = this._cachedCos * this.scale.y; - this.local.data[1] = -(this._cachedSin * this.scale.y) + this.skew.y; + this.local.data[4] = this._sc.y * this.scale.y; + this.local.data[1] = -(this._sc.x * this.scale.y) + this.skew.y; } // Translate @@ -211,72 +243,64 @@ module Phaser.Components { * The distance from the center of the transform to the rotation origin. */ public get distance(): number { - return this._cachedDistance; - //return Math.sqrt(((this.offsetX - this.halfWidth) * (this.offsetX - this.halfWidth)) + ((this.offsetY - this.halfHeight) * (this.offsetY - this.halfHeight))); + return this._distance; } /** * The angle between the center of the transform to the rotation origin. */ public get angleToCenter(): number { - return this._cachedAngleToCenter; - //return Math.atan2(this.halfHeight - this.offsetY, this.halfWidth - this.offsetX); + return this._angle; } /** * The offset on the X axis of the origin */ public get offsetX(): number { - return this._cachedOffsetX; - //return this.origin.x * this.parent.width; + return this._offset.x; } /** * The offset on the Y axis of the origin */ public get offsetY(): number { - return this._cachedOffsetY; - //return this.origin.y * this.parent.height; + return this._offset.y; } /** * Half the width of the parent sprite, taking into consideration scaling */ public get halfWidth(): number { - return this._cachedHalfWidth; - //return this.parent.width / 2; + return this._halfSize.x; } /** * Half the height of the parent sprite, taking into consideration scaling */ public get halfHeight(): number { - return this._cachedHalfHeight; - //return this.parent.height / 2; + return this._halfSize.y; } /** * The center of the Sprite in world coordinates, after taking scaling and rotation into consideration */ - public get centerX(): number { - return this._cachedCenterX; - //return this.parent.x + this.distance * Math.cos((this.rotation * Math.PI / 180) + this.angleToCenter); - } + //public get centerX(): number { + // return this.center.x; + //} /** * The center of the Sprite in world coordinates, after taking scaling and rotation into consideration */ - public get centerY(): number { - return this._cachedCenterY; - //return this.parent.y + this.distance * Math.sin((this.rotation * Math.PI / 180) + this.angleToCenter); - } + //public get centerY(): number { + // return this.center.y; + //} public get sin(): number { - return this._cachedSin; + return this._sc.x; } public get cos(): number { - return this._cachedCos; + return this._sc.y; } } diff --git a/Phaser/renderers/CanvasRenderer.ts b/Phaser/renderers/CanvasRenderer.ts index c3558af4..2c61a60e 100644 --- a/Phaser/renderers/CanvasRenderer.ts +++ b/Phaser/renderers/CanvasRenderer.ts @@ -221,7 +221,8 @@ module Phaser { return true; } - return RectangleUtils.intersects(sprite.cameraView, camera.screenView); + return true; + //return RectangleUtils.intersects(sprite.cameraView, camera.screenView); } diff --git a/Phaser/utils/SpriteUtils.ts b/Phaser/utils/SpriteUtils.ts index 1a3e4875..73605f2a 100644 --- a/Phaser/utils/SpriteUtils.ts +++ b/Phaser/utils/SpriteUtils.ts @@ -40,8 +40,8 @@ module Phaser { // If the sprite is rotated around its center we can use this quicker method: if (sprite.transform.origin.x == 0.5 && sprite.transform.origin.y == 0.5) { - SpriteUtils._sin = Math.sin((sprite.rotation + sprite.transform.rotationOffset) * GameMath.DEG_TO_RAD); - SpriteUtils._cos = Math.cos((sprite.rotation + sprite.transform.rotationOffset) * GameMath.DEG_TO_RAD); + SpriteUtils._sin = sprite.transform.sin; + SpriteUtils._cos = sprite.transform.cos; if (SpriteUtils._sin < 0) { @@ -60,8 +60,23 @@ module Phaser { } else { + //var left:Number = Math.min(topLeft.x, topRight.x, bottomRight.x, bottomLeft.x); + //var top:Number = Math.min(topLeft.y, topRight.y, bottomRight.y, bottomLeft.y); + //var right:Number = Math.max(topLeft.x, topRight.x, bottomRight.x, bottomLeft.x); + //var bottom:Number = Math.max(topLeft.y, topRight.y, bottomRight.y, bottomLeft.y); + //return new Rectangle(left, top, right - left, bottom - top); + var minX: number = Math.min(sprite.transform.upperLeft.x, sprite.transform.upperRight.x, sprite.transform.bottomLeft.x, sprite.transform.bottomRight.x); + var minY: number = Math.min(sprite.transform.upperLeft.y, sprite.transform.upperRight.y, sprite.transform.bottomLeft.y, sprite.transform.bottomRight.y); + var maxX: number = Math.max(sprite.transform.upperLeft.x, sprite.transform.upperRight.x, sprite.transform.bottomLeft.x, sprite.transform.bottomRight.x); + var maxY: number = Math.max(sprite.transform.upperLeft.y, sprite.transform.upperRight.y, sprite.transform.bottomLeft.y, sprite.transform.bottomRight.y); + // (min_x,min_y), (min_x,max_y), (max_x,max_y), (max_x,min_y) + + sprite.cameraView.x = minX; + sprite.cameraView.y = minY; + sprite.cameraView.width = maxX - minX; + sprite.cameraView.height = maxY - minY; /* // Useful to get the maximum AABB size of any given rect @@ -87,90 +102,6 @@ module Phaser { } - static getCornersAsPoints(sprite: Sprite): Phaser.Point[] { - - var out: Phaser.Point[] = []; - - var sin = Math.sin((sprite.transform.rotation + sprite.transform.rotationOffset) * Phaser.GameMath.DEG_TO_RAD); - var cos = Math.cos((sprite.transform.rotation + sprite.transform.rotationOffset) * Phaser.GameMath.DEG_TO_RAD); - - // Upper Left - out.push(new Point(sprite.x + (sprite.width / 2) * cos - (sprite.height / 2) * sin, sprite.y + (sprite.height / 2) * cos + (sprite.width / 2) * sin)); - - // Upper Right - out.push(new Point(sprite.x - (sprite.width / 2) * cos - (sprite.height / 2) * sin, sprite.y + (sprite.height / 2) * cos - (sprite.width / 2) * sin)); - - // Bottom Left - out.push(new Point(sprite.x + (sprite.width / 2) * cos + (sprite.height / 2) * sin, sprite.y - (sprite.height / 2) * cos + (sprite.width / 2) * sin)); - - // Bottom Right - out.push(new Point(sprite.x - (sprite.width / 2) * cos + (sprite.height / 2) * sin, sprite.y - (sprite.height / 2) * cos - (sprite.width / 2) * sin)); - - return out; - - } - - static getCornersAsPoints2(sprite: Sprite): Phaser.Point[] { - - var out: Phaser.Point[] = []; - - var sin = Math.sin((sprite.transform.rotation + sprite.transform.rotationOffset) * Phaser.GameMath.DEG_TO_RAD); - var cos = Math.cos((sprite.transform.rotation + sprite.transform.rotationOffset) * Phaser.GameMath.DEG_TO_RAD); - - // This = the center point - var cx = sprite.x + (sprite.width / 2) * cos - (sprite.height / 2) * sin; - var cy = sprite.y + (sprite.height / 2) * cos + (sprite.width / 2) * sin; - - // Upper Left - out.push(new Point(cx + (sprite.width / 2) * cos - (sprite.height / 2) * sin, cy + (sprite.height / 2) * cos + (sprite.width / 2) * sin)); - - // Upper Right - out.push(new Point(cx - (sprite.width / 2) * cos - (sprite.height / 2) * sin, cy + (sprite.height / 2) * cos - (sprite.width / 2) * sin)); - - // Bottom Left - out.push(new Point(cx + (sprite.width / 2) * cos + (sprite.height / 2) * sin, cy - (sprite.height / 2) * cos + (sprite.width / 2) * sin)); - - // Bottom Right - out.push(new Point(cx - (sprite.width / 2) * cos + (sprite.height / 2) * sin, cy - (sprite.height / 2) * cos - (sprite.width / 2) * sin)); - - return out; - - } - - static getCornersAsPoints3(sprite: Sprite): Phaser.Point[] { - - var out: Phaser.Point[] = []; - - var sin: number = sprite.transform.sin; - var cos: number = sprite.transform.cos; - - //var sin = Math.sin((sprite.transform.rotation + sprite.transform.rotationOffset) * Phaser.GameMath.DEG_TO_RAD); - //var cos = Math.cos((sprite.transform.rotation + sprite.transform.rotationOffset) * Phaser.GameMath.DEG_TO_RAD); - - // This = the center point - //var cx = sprite.x + sprite.transform.distance * Math.cos((sprite.transform.rotation * Math.PI / 180) + sprite.transform.angleToCenter); - //var cy = sprite.y + sprite.transform.distance * Math.sin((sprite.transform.rotation * Math.PI / 180) + sprite.transform.angleToCenter); - - var cx: number = sprite.transform.centerX; - var cy: number = sprite.transform.centerY; - - // Upper Left - out.push(new Point(cx + sprite.transform.halfWidth * cos - sprite.transform.halfHeight * sin, cy + sprite.transform.halfHeight * cos + sprite.transform.halfWidth * sin)); - - // Upper Right - out.push(new Point(cx - sprite.transform.halfWidth * cos - sprite.transform.halfHeight * sin, cy + sprite.transform.halfHeight * cos - sprite.transform.halfWidth * sin)); - - // Bottom Left - out.push(new Point(cx + sprite.transform.halfWidth * cos + sprite.transform.halfHeight * sin, cy - sprite.transform.halfHeight * cos + sprite.transform.halfWidth * sin)); - - // Bottom Right - out.push(new Point(cx - sprite.transform.halfWidth * cos + sprite.transform.halfHeight * sin, cy - sprite.transform.halfHeight * cos - sprite.transform.halfWidth * sin)); - - return out; - - } - - static getAsPoints(sprite: Sprite): Phaser.Point[] { var out: Phaser.Point[] = []; diff --git a/Tests/misc/point1.js b/Tests/misc/point1.js index 5e4d49de..e78fa486 100644 --- a/Tests/misc/point1.js +++ b/Tests/misc/point1.js @@ -3,30 +3,21 @@ var game = new Phaser.Game(this, 'game', 800, 600, init, create, update, render); function init() { game.load.image('box2', 'assets/tests/320x200.png'); - game.load.image('box1', 'assets/sprites/oz_pov_melting_disk.png'); - game.load.image('box', 'assets/sprites/bunny.png'); + game.load.image('box', 'assets/sprites/oz_pov_melting_disk.png'); + game.load.image('box1', 'assets/sprites/bunny.png'); game.load.start(); } var sprite; var rotate = false; function create() { game.stage.backgroundColor = 'rgb(0,0,0)'; - game.stage.disablePauseScreen = true; sprite = game.add.sprite(game.stage.centerX, game.stage.centerY, 'box'); //sprite.transform.scale.setTo(0.5, 0.5); - sprite.transform.origin.setTo(0.3, 0.3); + sprite.transform.origin.setTo(0, 0); + //sprite.transform.origin.setTo(0.5, 0.5); game.input.onTap.add(rotateIt, this); - game.add.tween(sprite.transform.scale).to({ - x: 0.5, - y: 0.5 - }, 2000, Phaser.Easing.Linear.None, true, 0, true); - points = [ - new Phaser.Point(), - new Phaser.Point(), - new Phaser.Point(), - new Phaser.Point() - ]; - } + //game.add.tween(sprite.transform.scale).to({ x: 0.5, y: 0.5 }, 2000, Phaser.Easing.Linear.None, true, 0, true); + } function rotateIt() { if(rotate == false) { rotate = true; @@ -39,59 +30,29 @@ sprite.rotation++; } } - var points; function render() { - /* - points = Phaser.SpriteUtils.getCornersAsPoints3(sprite); - - game.stage.context.fillStyle = 'rgb(255,255,0)'; - game.stage.context.fillRect(points[0].x, points[0].y, 2, 2); - game.stage.context.fillRect(points[1].x, points[1].y, 2, 2); - game.stage.context.fillRect(points[2].x, points[2].y, 2, 2); - game.stage.context.fillRect(points[3].x, points[3].y, 2, 2); - */ - var originX = sprite.transform.origin.x * sprite.width; - var originY = sprite.transform.origin.y * sprite.height; - var centerX = 0.5 * sprite.width; - var centerY = 0.5 * sprite.height; - var distance = Math.sqrt(((originX - centerX) * (originX - centerX)) + ((originY - centerY) * (originY - centerY))); - var originAngle = Math.atan2(centerY - originY, centerX - originX); - var sin = Math.sin((sprite.transform.rotation + sprite.transform.rotationOffset) * Phaser.GameMath.DEG_TO_RAD); - var cos = Math.cos((sprite.transform.rotation + sprite.transform.rotationOffset) * Phaser.GameMath.DEG_TO_RAD); - //var px = sprite.x + distance * Math.cos((sprite.transform.rotation * Math.PI / 180) + originAngle); - //var py = sprite.y + distance * Math.sin((sprite.transform.rotation * Math.PI / 180) + originAngle); - // WORKS - //var px = sprite.x + sprite.transform.distance * Math.cos((sprite.transform.rotation * Math.PI / 180) + sprite.transform.angleToCenter); - //var py = sprite.y + sprite.transform.distance * Math.sin((sprite.transform.rotation * Math.PI / 180) + sprite.transform.angleToCenter); - // Upper Left - //points[0].setTo(px + (sprite.width / 2) * cos - (sprite.height / 2) * sin, py + (sprite.height / 2) * cos + (sprite.width / 2) * sin); - // Upper Right - //points[1].setTo(px - (sprite.width / 2) * cos - (sprite.height / 2) * sin, py + (sprite.height / 2) * cos - (sprite.width / 2) * sin); - // Bottom Left - //points[2].setTo(px + (sprite.width / 2) * cos + (sprite.height / 2) * sin, py - (sprite.height / 2) * cos + (sprite.width / 2) * sin); - // Bottom Right - //points[3].setTo(px - (sprite.width / 2) * cos + (sprite.height / 2) * sin, py - (sprite.height / 2) * cos - (sprite.width / 2) * sin); - points = Phaser.SpriteUtils.getCornersAsPoints3(sprite); game.stage.context.save(); - game.stage.context.fillStyle = 'rgb(0,255,255)'; - //game.stage.context.fillRect(px, py, 2, 2); - game.stage.context.fillText('rect width: ' + originX + ' height: ' + originY, 32, 32); - game.stage.context.fillText('center x: ' + centerX + ' centerY: ' + centerY, 32, 52); - game.stage.context.fillText('angle: ' + sprite.rotation, 32, 72); - game.stage.context.fillText('point of rotation x: ' + sprite.transform.origin.x + ' y: ' + sprite.transform.origin.y, 32, 92); - game.stage.context.fillText('x: ' + sprite.x + ' y: ' + sprite.y, sprite.x + 4, sprite.y); - game.stage.context.fillRect(points[0].x, points[0].y, 2, 2); - game.stage.context.fillRect(points[1].x, points[1].y, 2, 2); - game.stage.context.fillRect(points[2].x, points[2].y, 2, 2); - game.stage.context.fillRect(points[3].x, points[3].y, 2, 2); - game.stage.context.restore(); - game.stage.context.save(); - game.stage.context.fillStyle = 'rgba(255,255,255,0.1)'; - game.stage.context.beginPath(); - game.stage.context.moveTo(sprite.x, sprite.y); - game.stage.context.arc(sprite.x, sprite.y, distance, 0, Math.PI * 2); - game.stage.context.closePath(); - game.stage.context.fill(); + game.stage.context.fillStyle = 'rgb(255,0,255)'; + game.stage.context.fillText('x: ' + Math.round(sprite.transform.upperLeft.x) + ' y: ' + Math.round(sprite.transform.upperLeft.y), sprite.transform.upperLeft.x, sprite.transform.upperLeft.y); + game.stage.context.fillText('x: ' + Math.round(sprite.transform.upperRight.x) + ' y: ' + Math.round(sprite.transform.upperRight.y), sprite.transform.upperRight.x, sprite.transform.upperRight.y); + game.stage.context.fillText('x: ' + Math.round(sprite.transform.bottomLeft.x) + ' y: ' + Math.round(sprite.transform.bottomLeft.y), sprite.transform.bottomLeft.x, sprite.transform.bottomLeft.y); + game.stage.context.fillText('x: ' + Math.round(sprite.transform.bottomRight.x) + ' y: ' + Math.round(sprite.transform.bottomRight.y), sprite.transform.bottomRight.x, sprite.transform.bottomRight.y); + var minX = Math.min(sprite.transform.upperLeft.x, sprite.transform.upperRight.x, sprite.transform.bottomLeft.x, sprite.transform.bottomRight.x); + var minY = Math.min(sprite.transform.upperLeft.y, sprite.transform.upperRight.y, sprite.transform.bottomLeft.y, sprite.transform.bottomRight.y); + var maxX = Math.max(sprite.transform.upperLeft.x, sprite.transform.upperRight.x, sprite.transform.bottomLeft.x, sprite.transform.bottomRight.x); + var maxY = Math.max(sprite.transform.upperLeft.y, sprite.transform.upperRight.y, sprite.transform.bottomLeft.y, sprite.transform.bottomRight.y); + var width = maxX - minX; + var height = maxY - minY; + game.stage.context.fillText('minX: ' + minX + ' minY: ' + minY, 32, 32); + game.stage.context.fillText('maxX: ' + maxX + ' maxY: ' + maxY, 32, 64); + game.stage.context.fillRect(sprite.transform.center.x, sprite.transform.center.y, 2, 2); + game.stage.context.fillRect(sprite.transform.upperLeft.x, sprite.transform.upperLeft.y, 2, 2); + game.stage.context.fillRect(sprite.transform.upperRight.x, sprite.transform.upperRight.y, 2, 2); + game.stage.context.fillRect(sprite.transform.bottomLeft.x, sprite.transform.bottomLeft.y, 2, 2); + game.stage.context.fillRect(sprite.transform.bottomRight.x, sprite.transform.bottomRight.y, 2, 2); + game.stage.context.strokeStyle = 'rgb(255,255,0)'; + game.stage.context.strokeRect(sprite.cameraView.x, sprite.cameraView.y, sprite.cameraView.width, sprite.cameraView.height); + //game.stage.context.strokeRect(minX, minY, width, height); game.stage.context.restore(); } })(); diff --git a/Tests/misc/point1.ts b/Tests/misc/point1.ts index 55bf59b9..904d7d39 100644 --- a/Tests/misc/point1.ts +++ b/Tests/misc/point1.ts @@ -7,8 +7,8 @@ function init() { game.load.image('box2', 'assets/tests/320x200.png'); - game.load.image('box1', 'assets/sprites/oz_pov_melting_disk.png'); - game.load.image('box', 'assets/sprites/bunny.png'); + game.load.image('box', 'assets/sprites/oz_pov_melting_disk.png'); + game.load.image('box1', 'assets/sprites/bunny.png'); game.load.start(); } @@ -19,19 +19,16 @@ function create() { game.stage.backgroundColor = 'rgb(0,0,0)'; - game.stage.disablePauseScreen = true; sprite = game.add.sprite(game.stage.centerX, game.stage.centerY, 'box'); //sprite.transform.scale.setTo(0.5, 0.5); - - sprite.transform.origin.setTo(0.3, 0.3); + sprite.transform.origin.setTo(0, 0); + //sprite.transform.origin.setTo(0.5, 0.5); game.input.onTap.add(rotateIt, this); - game.add.tween(sprite.transform.scale).to({ x: 0.5, y: 0.5 }, 2000, Phaser.Easing.Linear.None, true, 0, true); - - points = [new Phaser.Point, new Phaser.Point, new Phaser.Point, new Phaser.Point]; + //game.add.tween(sprite.transform.scale).to({ x: 0.5, y: 0.5 }, 2000, Phaser.Easing.Linear.None, true, 0, true); } @@ -48,74 +45,37 @@ } - var points: Phaser.Point[]; - function render() { - /* - points = Phaser.SpriteUtils.getCornersAsPoints3(sprite); - - game.stage.context.fillStyle = 'rgb(255,255,0)'; - game.stage.context.fillRect(points[0].x, points[0].y, 2, 2); - game.stage.context.fillRect(points[1].x, points[1].y, 2, 2); - game.stage.context.fillRect(points[2].x, points[2].y, 2, 2); - game.stage.context.fillRect(points[3].x, points[3].y, 2, 2); - */ - - var originX: number = sprite.transform.origin.x * sprite.width; - var originY: number = sprite.transform.origin.y * sprite.height; - var centerX: number = 0.5 * sprite.width; - var centerY: number = 0.5 * sprite.height; - var distance: number = Math.sqrt(((originX - centerX) * (originX - centerX)) + ((originY - centerY) * (originY - centerY))); - var originAngle: number = Math.atan2(centerY - originY, centerX - originX); - - var sin = Math.sin((sprite.transform.rotation + sprite.transform.rotationOffset) * Phaser.GameMath.DEG_TO_RAD); - var cos = Math.cos((sprite.transform.rotation + sprite.transform.rotationOffset) * Phaser.GameMath.DEG_TO_RAD); - - //var px = sprite.x + distance * Math.cos((sprite.transform.rotation * Math.PI / 180) + originAngle); - //var py = sprite.y + distance * Math.sin((sprite.transform.rotation * Math.PI / 180) + originAngle); - - // WORKS - //var px = sprite.x + sprite.transform.distance * Math.cos((sprite.transform.rotation * Math.PI / 180) + sprite.transform.angleToCenter); - //var py = sprite.y + sprite.transform.distance * Math.sin((sprite.transform.rotation * Math.PI / 180) + sprite.transform.angleToCenter); - - // Upper Left - //points[0].setTo(px + (sprite.width / 2) * cos - (sprite.height / 2) * sin, py + (sprite.height / 2) * cos + (sprite.width / 2) * sin); - - // Upper Right - //points[1].setTo(px - (sprite.width / 2) * cos - (sprite.height / 2) * sin, py + (sprite.height / 2) * cos - (sprite.width / 2) * sin); - - // Bottom Left - //points[2].setTo(px + (sprite.width / 2) * cos + (sprite.height / 2) * sin, py - (sprite.height / 2) * cos + (sprite.width / 2) * sin); - - // Bottom Right - //points[3].setTo(px - (sprite.width / 2) * cos + (sprite.height / 2) * sin, py - (sprite.height / 2) * cos - (sprite.width / 2) * sin); - - points = Phaser.SpriteUtils.getCornersAsPoints3(sprite); - game.stage.context.save(); - game.stage.context.fillStyle = 'rgb(0,255,255)'; - //game.stage.context.fillRect(px, py, 2, 2); - game.stage.context.fillText('rect width: ' + originX + ' height: ' + originY, 32, 32); - game.stage.context.fillText('center x: ' + centerX + ' centerY: ' + centerY, 32, 52); - game.stage.context.fillText('angle: ' + sprite.rotation , 32, 72); - game.stage.context.fillText('point of rotation x: ' + sprite.transform.origin.x + ' y: ' + sprite.transform.origin.y, 32, 92); - game.stage.context.fillText('x: ' + sprite.x + ' y: ' + sprite.y, sprite.x + 4, sprite.y); + game.stage.context.fillStyle = 'rgb(255,0,255)'; - game.stage.context.fillRect(points[0].x, points[0].y, 2, 2); - game.stage.context.fillRect(points[1].x, points[1].y, 2, 2); - game.stage.context.fillRect(points[2].x, points[2].y, 2, 2); - game.stage.context.fillRect(points[3].x, points[3].y, 2, 2); + game.stage.context.fillText('x: ' + Math.round(sprite.transform.upperLeft.x) + ' y: ' + Math.round(sprite.transform.upperLeft.y), sprite.transform.upperLeft.x, sprite.transform.upperLeft.y); + game.stage.context.fillText('x: ' + Math.round(sprite.transform.upperRight.x) + ' y: ' + Math.round(sprite.transform.upperRight.y), sprite.transform.upperRight.x, sprite.transform.upperRight.y); + game.stage.context.fillText('x: ' + Math.round(sprite.transform.bottomLeft.x) + ' y: ' + Math.round(sprite.transform.bottomLeft.y), sprite.transform.bottomLeft.x, sprite.transform.bottomLeft.y); + game.stage.context.fillText('x: ' + Math.round(sprite.transform.bottomRight.x) + ' y: ' + Math.round(sprite.transform.bottomRight.y), sprite.transform.bottomRight.x, sprite.transform.bottomRight.y); - game.stage.context.restore(); + var minX: number = Math.min(sprite.transform.upperLeft.x, sprite.transform.upperRight.x, sprite.transform.bottomLeft.x, sprite.transform.bottomRight.x); + var minY: number = Math.min(sprite.transform.upperLeft.y, sprite.transform.upperRight.y, sprite.transform.bottomLeft.y, sprite.transform.bottomRight.y); + var maxX: number = Math.max(sprite.transform.upperLeft.x, sprite.transform.upperRight.x, sprite.transform.bottomLeft.x, sprite.transform.bottomRight.x); + var maxY: number = Math.max(sprite.transform.upperLeft.y, sprite.transform.upperRight.y, sprite.transform.bottomLeft.y, sprite.transform.bottomRight.y); + + var width = maxX - minX; + var height = maxY - minY; + + game.stage.context.fillText('minX: ' + minX + ' minY: ' + minY, 32, 32); + game.stage.context.fillText('maxX: ' + maxX + ' maxY: ' + maxY, 32, 64); + + game.stage.context.fillRect(sprite.transform.center.x, sprite.transform.center.y, 2, 2); + game.stage.context.fillRect(sprite.transform.upperLeft.x, sprite.transform.upperLeft.y, 2, 2); + game.stage.context.fillRect(sprite.transform.upperRight.x, sprite.transform.upperRight.y, 2, 2); + game.stage.context.fillRect(sprite.transform.bottomLeft.x, sprite.transform.bottomLeft.y, 2, 2); + game.stage.context.fillRect(sprite.transform.bottomRight.x, sprite.transform.bottomRight.y, 2, 2); + + game.stage.context.strokeStyle = 'rgb(255,255,0)'; + game.stage.context.strokeRect(sprite.cameraView.x, sprite.cameraView.y, sprite.cameraView.width, sprite.cameraView.height); + //game.stage.context.strokeRect(minX, minY, width, height); - game.stage.context.save(); - game.stage.context.fillStyle = 'rgba(255,255,255,0.1)'; - game.stage.context.beginPath(); - game.stage.context.moveTo(sprite.x, sprite.y); - game.stage.context.arc(sprite.x, sprite.y, distance, 0, Math.PI * 2); - game.stage.context.closePath(); - game.stage.context.fill(); game.stage.context.restore(); } diff --git a/Tests/phaser.js b/Tests/phaser.js index 279c2dfc..cc94bbce 100644 --- a/Tests/phaser.js +++ b/Tests/phaser.js @@ -3082,84 +3082,112 @@ var Phaser; this.origin = new Phaser.Vec2(); this.scale = new Phaser.Vec2(1, 1); this.skew = new Phaser.Vec2(); + this.center = new Phaser.Point(); + this.upperLeft = new Phaser.Point(); + this.upperRight = new Phaser.Point(); + this.bottomLeft = new Phaser.Point(); + this.bottomRight = new Phaser.Point(); + this._pos = new Phaser.Point(); + this._scale = new Phaser.Point(); + this._size = new Phaser.Point(); + this._halfSize = new Phaser.Point(); + this._offset = new Phaser.Point(); + this._origin = new Phaser.Point(); + this._sc = new Phaser.Point(); + this._scA = new Phaser.Point(); } Transform.prototype.setCache = function () { - this._cachedHalfWidth = this.parent.width / 2; - this._cachedHalfHeight = this.parent.height / 2; - this._cachedOffsetX = this.origin.x * this.parent.width; - this._cachedOffsetY = this.origin.y * this.parent.height; - this._cachedAngleToCenter = Math.atan2(this.halfHeight - this._cachedOffsetY, this.halfWidth - this._cachedOffsetX); - this._cachedDistance = Math.sqrt(((this._cachedOffsetX - this._cachedHalfWidth) * (this._cachedOffsetX - this._cachedHalfWidth)) + ((this._cachedOffsetY - this._cachedHalfHeight) * (this._cachedOffsetY - this._cachedHalfHeight))); - this._cachedWidth = this.parent.width; - this._cachedHeight = this.parent.height; - this._cachedOriginX = this.origin.x; - this._cachedOriginY = this.origin.y; - this._cachedSin = Math.sin((this.rotation + this.rotationOffset) * Phaser.GameMath.DEG_TO_RAD); - this._cachedCos = Math.cos((this.rotation + this.rotationOffset) * Phaser.GameMath.DEG_TO_RAD); - this._cachedCosAngle = Math.cos((this.rotation + this.rotationOffset) * Phaser.GameMath.DEG_TO_RAD + this._cachedAngleToCenter); - this._cachedSinAngle = Math.sin((this.rotation + this.rotationOffset) * Phaser.GameMath.DEG_TO_RAD + this._cachedAngleToCenter); - this._cachedRotation = this.rotation; + this._pos.x = this.parent.x; + this._pos.y = this.parent.y; + this._halfSize.x = this.parent.width / 2; + this._halfSize.y = this.parent.height / 2; + this._offset.x = this.origin.x * this.parent.width; + this._offset.y = this.origin.y * this.parent.height; + this._angle = Math.atan2(this.halfHeight - this._offset.y, this.halfWidth - this._offset.x); + this._distance = Math.sqrt(((this._offset.x - this._halfSize.x) * (this._offset.x - this._halfSize.x)) + ((this._offset.y - this._halfSize.y) * (this._offset.y - this._halfSize.y))); + this._size.x = this.parent.width; + this._size.y = this.parent.height; + this._origin.x = this.origin.x; + this._origin.y = this.origin.y; + this._sc.x = Math.sin((this.rotation + this.rotationOffset) * Phaser.GameMath.DEG_TO_RAD); + this._sc.y = Math.cos((this.rotation + this.rotationOffset) * Phaser.GameMath.DEG_TO_RAD); + this._scA.y = Math.cos((this.rotation + this.rotationOffset) * Phaser.GameMath.DEG_TO_RAD + this._angle); + this._scA.x = Math.sin((this.rotation + this.rotationOffset) * Phaser.GameMath.DEG_TO_RAD + this._angle); + this._prevRotation = this.rotation; if(this.parent.texture && this.parent.texture.renderRotation) { - this._cachedSin = Math.sin((this.rotation + this.rotationOffset) * Phaser.GameMath.DEG_TO_RAD); - this._cachedCos = Math.cos((this.rotation + this.rotationOffset) * Phaser.GameMath.DEG_TO_RAD); + this._sc.x = Math.sin((this.rotation + this.rotationOffset) * Phaser.GameMath.DEG_TO_RAD); + this._sc.y = Math.cos((this.rotation + this.rotationOffset) * Phaser.GameMath.DEG_TO_RAD); } else { - this._cachedSin = 0; - this._cachedCos = 1; + this._sc.x = 0; + this._sc.y = 1; } + this.center.setTo(this.center.x, this.center.y); + this.upperLeft.setTo(this.center.x - this._halfSize.x * this._sc.y + this._halfSize.y * this._sc.x, this.center.y - this._halfSize.y * this._sc.y - this._halfSize.x * this._sc.x); + this.upperRight.setTo(this.center.x + this._halfSize.x * this._sc.y + this._halfSize.y * this._sc.x, this.center.y - this._halfSize.y * this._sc.y + this._halfSize.x * this._sc.x); + this.bottomLeft.setTo(this.center.x - this._halfSize.x * this._sc.y - this._halfSize.y * this._sc.x, this.center.y + this._halfSize.y * this._sc.y - this._halfSize.x * this._sc.x); + this.bottomRight.setTo(this.center.x + this._halfSize.x * this._sc.y - this._halfSize.y * this._sc.x, this.center.y + this._halfSize.y * this._sc.y + this._halfSize.x * this._sc.x); }; Transform.prototype.update = function () { // Check cache var dirty = false; // 1) Height or Width change (also triggered by a change in scale) or an Origin change - if(this.parent.width !== this._cachedWidth || this.parent.height !== this._cachedHeight || this.origin.x !== this._cachedOriginX || this.origin.y !== this._cachedOriginY) { - this._cachedHalfWidth = this.parent.width / 2; - this._cachedHalfHeight = this.parent.height / 2; - this._cachedOffsetX = this.origin.x * this.parent.width; - this._cachedOffsetY = this.origin.y * this.parent.height; - this._cachedAngleToCenter = Math.atan2(this.halfHeight - this._cachedOffsetY, this.halfWidth - this._cachedOffsetX); - this._cachedDistance = Math.sqrt(((this._cachedOffsetX - this._cachedHalfWidth) * (this._cachedOffsetX - this._cachedHalfWidth)) + ((this._cachedOffsetY - this._cachedHalfHeight) * (this._cachedOffsetY - this._cachedHalfHeight))); + if(this.parent.width !== this._size.x || this.parent.height !== this._size.y || this.origin.x !== this._origin.x || this.origin.y !== this._origin.y) { + this._halfSize.x = this.parent.width / 2; + this._halfSize.y = this.parent.height / 2; + this._offset.x = this.origin.x * this.parent.width; + this._offset.y = this.origin.y * this.parent.height; + this._angle = Math.atan2(this.halfHeight - this._offset.y, this.halfWidth - this._offset.x); + this._distance = Math.sqrt(((this._offset.x - this._halfSize.x) * (this._offset.x - this._halfSize.x)) + ((this._offset.y - this._halfSize.y) * (this._offset.y - this._halfSize.y))); // Store - this._cachedWidth = this.parent.width; - this._cachedHeight = this.parent.height; - this._cachedOriginX = this.origin.x; - this._cachedOriginY = this.origin.y; + this._size.x = this.parent.width; + this._size.y = this.parent.height; + this._origin.x = this.origin.x; + this._origin.y = this.origin.y; dirty = true; } // 2) Rotation change - if(this.rotation != this._cachedRotation) { - this._cachedSin = Math.sin((this.rotation + this.rotationOffset) * Phaser.GameMath.DEG_TO_RAD); - this._cachedCos = Math.cos((this.rotation + this.rotationOffset) * Phaser.GameMath.DEG_TO_RAD); - this._cachedCosAngle = Math.cos((this.rotation + this.rotationOffset) * Phaser.GameMath.DEG_TO_RAD + this._cachedAngleToCenter); - this._cachedSinAngle = Math.sin((this.rotation + this.rotationOffset) * Phaser.GameMath.DEG_TO_RAD + this._cachedAngleToCenter); + if(this.rotation != this._prevRotation) { + this._sc.x = Math.sin((this.rotation + this.rotationOffset) * Phaser.GameMath.DEG_TO_RAD); + this._sc.y = Math.cos((this.rotation + this.rotationOffset) * Phaser.GameMath.DEG_TO_RAD); + this._scA.y = Math.cos((this.rotation + this.rotationOffset) * Phaser.GameMath.DEG_TO_RAD + this._angle); + this._scA.x = Math.sin((this.rotation + this.rotationOffset) * Phaser.GameMath.DEG_TO_RAD + this._angle); if(this.parent.texture.renderRotation) { - this._cachedSin = Math.sin((this.rotation + this.rotationOffset) * Phaser.GameMath.DEG_TO_RAD); - this._cachedCos = Math.cos((this.rotation + this.rotationOffset) * Phaser.GameMath.DEG_TO_RAD); + this._sc.x = Math.sin((this.rotation + this.rotationOffset) * Phaser.GameMath.DEG_TO_RAD); + this._sc.y = Math.cos((this.rotation + this.rotationOffset) * Phaser.GameMath.DEG_TO_RAD); } else { - this._cachedSin = 0; - this._cachedCos = 1; + this._sc.x = 0; + this._sc.y = 1; } // Store - this._cachedRotation = this.rotation; + this._prevRotation = this.rotation; dirty = true; } - if(dirty) { - this._cachedCenterX = this.parent.x + this._cachedDistance * this._cachedCosAngle; - this._cachedCenterY = this.parent.y + this._cachedDistance * this._cachedSinAngle; + // If it has moved, updated the edges and center + if(dirty || this.parent.x != this._pos.x || this.parent.y != this._pos.y) { + this.center.x = this.parent.x + this._distance * this._scA.y; + this.center.y = this.parent.y + this._distance * this._scA.x; + this.center.setTo(this.center.x, this.center.y); + this.upperLeft.setTo(this.center.x - this._halfSize.x * this._sc.y + this._halfSize.y * this._sc.x, this.center.y - this._halfSize.y * this._sc.y - this._halfSize.x * this._sc.x); + this.upperRight.setTo(this.center.x + this._halfSize.x * this._sc.y + this._halfSize.y * this._sc.x, this.center.y - this._halfSize.y * this._sc.y + this._halfSize.x * this._sc.x); + this.bottomLeft.setTo(this.center.x - this._halfSize.x * this._sc.y - this._halfSize.y * this._sc.x, this.center.y + this._halfSize.y * this._sc.y - this._halfSize.x * this._sc.x); + this.bottomRight.setTo(this.center.x + this._halfSize.x * this._sc.y - this._halfSize.y * this._sc.x, this.center.y + this._halfSize.y * this._sc.y + this._halfSize.x * this._sc.x); + this._pos.x = this.parent.x; + this._pos.y = this.parent.y; } // Scale and Skew if(this.parent.texture.flippedX) { - this.local.data[0] = this._cachedCos * -this.scale.x; - this.local.data[3] = (this._cachedSin * -this.scale.x) + this.skew.x; + this.local.data[0] = this._sc.y * -this.scale.x; + this.local.data[3] = (this._sc.x * -this.scale.x) + this.skew.x; } else { - this.local.data[0] = this._cachedCos * this.scale.x; - this.local.data[3] = (this._cachedSin * this.scale.x) + this.skew.x; + this.local.data[0] = this._sc.y * this.scale.x; + this.local.data[3] = (this._sc.x * this.scale.x) + this.skew.x; } if(this.parent.texture.flippedY) { - this.local.data[4] = this._cachedCos * -this.scale.y; - this.local.data[1] = -(this._cachedSin * -this.scale.y) + this.skew.y; + this.local.data[4] = this._sc.y * -this.scale.y; + this.local.data[1] = -(this._sc.x * -this.scale.y) + this.skew.y; } else { - this.local.data[4] = this._cachedCos * this.scale.y; - this.local.data[1] = -(this._cachedSin * this.scale.y) + this.skew.y; + this.local.data[4] = this._sc.y * this.scale.y; + this.local.data[1] = -(this._sc.x * this.scale.y) + this.skew.y; } // Translate this.local.data[2] = this.parent.x; @@ -3170,9 +3198,8 @@ var Phaser; * The distance from the center of the transform to the rotation origin. */ function () { - return this._cachedDistance; - //return Math.sqrt(((this.offsetX - this.halfWidth) * (this.offsetX - this.halfWidth)) + ((this.offsetY - this.halfHeight) * (this.offsetY - this.halfHeight))); - }, + return this._distance; + }, enumerable: true, configurable: true }); @@ -3181,9 +3208,8 @@ var Phaser; * The angle between the center of the transform to the rotation origin. */ function () { - return this._cachedAngleToCenter; - //return Math.atan2(this.halfHeight - this.offsetY, this.halfWidth - this.offsetX); - }, + return this._angle; + }, enumerable: true, configurable: true }); @@ -3192,9 +3218,8 @@ var Phaser; * The offset on the X axis of the origin */ function () { - return this._cachedOffsetX; - //return this.origin.x * this.parent.width; - }, + return this._offset.x; + }, enumerable: true, configurable: true }); @@ -3203,9 +3228,8 @@ var Phaser; * The offset on the Y axis of the origin */ function () { - return this._cachedOffsetY; - //return this.origin.y * this.parent.height; - }, + return this._offset.y; + }, enumerable: true, configurable: true }); @@ -3214,9 +3238,8 @@ var Phaser; * Half the width of the parent sprite, taking into consideration scaling */ function () { - return this._cachedHalfWidth; - //return this.parent.width / 2; - }, + return this._halfSize.x; + }, enumerable: true, configurable: true }); @@ -3225,44 +3248,33 @@ var Phaser; * Half the height of the parent sprite, taking into consideration scaling */ function () { - return this._cachedHalfHeight; - //return this.parent.height / 2; - }, - enumerable: true, - configurable: true - }); - Object.defineProperty(Transform.prototype, "centerX", { - get: /** - * The center of the Sprite in world coordinates, after taking scaling and rotation into consideration - */ - function () { - return this._cachedCenterX; - //return this.parent.x + this.distance * Math.cos((this.rotation * Math.PI / 180) + this.angleToCenter); - }, - enumerable: true, - configurable: true - }); - Object.defineProperty(Transform.prototype, "centerY", { - get: /** - * The center of the Sprite in world coordinates, after taking scaling and rotation into consideration - */ - function () { - return this._cachedCenterY; - //return this.parent.y + this.distance * Math.sin((this.rotation * Math.PI / 180) + this.angleToCenter); - }, + return this._halfSize.y; + }, enumerable: true, configurable: true }); Object.defineProperty(Transform.prototype, "sin", { - get: function () { - return this._cachedSin; + get: /** + * The center of the Sprite in world coordinates, after taking scaling and rotation into consideration + */ + //public get centerX(): number { + // return this.center.x; + //} + /** + * The center of the Sprite in world coordinates, after taking scaling and rotation into consideration + */ + //public get centerY(): number { + // return this.center.y; + //} + function () { + return this._sc.x; }, enumerable: true, configurable: true }); Object.defineProperty(Transform.prototype, "cos", { get: function () { - return this._cachedCos; + return this._sc.y; }, enumerable: true, configurable: true @@ -4656,8 +4668,8 @@ var Phaser; } else { // If the sprite is rotated around its center we can use this quicker method: if(sprite.transform.origin.x == 0.5 && sprite.transform.origin.y == 0.5) { - SpriteUtils._sin = Math.sin((sprite.rotation + sprite.transform.rotationOffset) * Phaser.GameMath.DEG_TO_RAD); - SpriteUtils._cos = Math.cos((sprite.rotation + sprite.transform.rotationOffset) * Phaser.GameMath.DEG_TO_RAD); + SpriteUtils._sin = sprite.transform.sin; + SpriteUtils._cos = sprite.transform.cos; if(SpriteUtils._sin < 0) { SpriteUtils._sin = -SpriteUtils._sin; } @@ -4669,6 +4681,20 @@ var Phaser; sprite.cameraView.x = Math.round(sprite.x - (camera.worldView.x * sprite.transform.scrollFactor.x) - (sprite.cameraView.width * sprite.transform.origin.x)); sprite.cameraView.y = Math.round(sprite.y - (camera.worldView.y * sprite.transform.scrollFactor.y) - (sprite.cameraView.height * sprite.transform.origin.y)); } else { + //var left:Number = Math.min(topLeft.x, topRight.x, bottomRight.x, bottomLeft.x); + //var top:Number = Math.min(topLeft.y, topRight.y, bottomRight.y, bottomLeft.y); + //var right:Number = Math.max(topLeft.x, topRight.x, bottomRight.x, bottomLeft.x); + //var bottom:Number = Math.max(topLeft.y, topRight.y, bottomRight.y, bottomLeft.y); + //return new Rectangle(left, top, right - left, bottom - top); + var minX = Math.min(sprite.transform.upperLeft.x, sprite.transform.upperRight.x, sprite.transform.bottomLeft.x, sprite.transform.bottomRight.x); + var minY = Math.min(sprite.transform.upperLeft.y, sprite.transform.upperRight.y, sprite.transform.bottomLeft.y, sprite.transform.bottomRight.y); + var maxX = Math.max(sprite.transform.upperLeft.x, sprite.transform.upperRight.x, sprite.transform.bottomLeft.x, sprite.transform.bottomRight.x); + var maxY = Math.max(sprite.transform.upperLeft.y, sprite.transform.upperRight.y, sprite.transform.bottomLeft.y, sprite.transform.bottomRight.y); + // (min_x,min_y), (min_x,max_y), (max_x,max_y), (max_x,min_y) + sprite.cameraView.x = minX; + sprite.cameraView.y = minY; + sprite.cameraView.width = maxX - minX; + sprite.cameraView.height = maxY - minY; /* // Useful to get the maximum AABB size of any given rect @@ -4686,58 +4712,6 @@ var Phaser; } return sprite.cameraView; }; - SpriteUtils.getCornersAsPoints = function getCornersAsPoints(sprite) { - var out = []; - var sin = Math.sin((sprite.transform.rotation + sprite.transform.rotationOffset) * Phaser.GameMath.DEG_TO_RAD); - var cos = Math.cos((sprite.transform.rotation + sprite.transform.rotationOffset) * Phaser.GameMath.DEG_TO_RAD); - // Upper Left - out.push(new Phaser.Point(sprite.x + (sprite.width / 2) * cos - (sprite.height / 2) * sin, sprite.y + (sprite.height / 2) * cos + (sprite.width / 2) * sin)); - // Upper Right - out.push(new Phaser.Point(sprite.x - (sprite.width / 2) * cos - (sprite.height / 2) * sin, sprite.y + (sprite.height / 2) * cos - (sprite.width / 2) * sin)); - // Bottom Left - out.push(new Phaser.Point(sprite.x + (sprite.width / 2) * cos + (sprite.height / 2) * sin, sprite.y - (sprite.height / 2) * cos + (sprite.width / 2) * sin)); - // Bottom Right - out.push(new Phaser.Point(sprite.x - (sprite.width / 2) * cos + (sprite.height / 2) * sin, sprite.y - (sprite.height / 2) * cos - (sprite.width / 2) * sin)); - return out; - }; - SpriteUtils.getCornersAsPoints2 = function getCornersAsPoints2(sprite) { - var out = []; - var sin = Math.sin((sprite.transform.rotation + sprite.transform.rotationOffset) * Phaser.GameMath.DEG_TO_RAD); - var cos = Math.cos((sprite.transform.rotation + sprite.transform.rotationOffset) * Phaser.GameMath.DEG_TO_RAD); - // This = the center point - var cx = sprite.x + (sprite.width / 2) * cos - (sprite.height / 2) * sin; - var cy = sprite.y + (sprite.height / 2) * cos + (sprite.width / 2) * sin; - // Upper Left - out.push(new Phaser.Point(cx + (sprite.width / 2) * cos - (sprite.height / 2) * sin, cy + (sprite.height / 2) * cos + (sprite.width / 2) * sin)); - // Upper Right - out.push(new Phaser.Point(cx - (sprite.width / 2) * cos - (sprite.height / 2) * sin, cy + (sprite.height / 2) * cos - (sprite.width / 2) * sin)); - // Bottom Left - out.push(new Phaser.Point(cx + (sprite.width / 2) * cos + (sprite.height / 2) * sin, cy - (sprite.height / 2) * cos + (sprite.width / 2) * sin)); - // Bottom Right - out.push(new Phaser.Point(cx - (sprite.width / 2) * cos + (sprite.height / 2) * sin, cy - (sprite.height / 2) * cos - (sprite.width / 2) * sin)); - return out; - }; - SpriteUtils.getCornersAsPoints3 = function getCornersAsPoints3(sprite) { - var out = []; - var sin = sprite.transform.sin; - var cos = sprite.transform.cos; - //var sin = Math.sin((sprite.transform.rotation + sprite.transform.rotationOffset) * Phaser.GameMath.DEG_TO_RAD); - //var cos = Math.cos((sprite.transform.rotation + sprite.transform.rotationOffset) * Phaser.GameMath.DEG_TO_RAD); - // This = the center point - //var cx = sprite.x + sprite.transform.distance * Math.cos((sprite.transform.rotation * Math.PI / 180) + sprite.transform.angleToCenter); - //var cy = sprite.y + sprite.transform.distance * Math.sin((sprite.transform.rotation * Math.PI / 180) + sprite.transform.angleToCenter); - var cx = sprite.transform.centerX; - var cy = sprite.transform.centerY; - // Upper Left - out.push(new Phaser.Point(cx + sprite.transform.halfWidth * cos - sprite.transform.halfHeight * sin, cy + sprite.transform.halfHeight * cos + sprite.transform.halfWidth * sin)); - // Upper Right - out.push(new Phaser.Point(cx - sprite.transform.halfWidth * cos - sprite.transform.halfHeight * sin, cy + sprite.transform.halfHeight * cos - sprite.transform.halfWidth * sin)); - // Bottom Left - out.push(new Phaser.Point(cx + sprite.transform.halfWidth * cos + sprite.transform.halfHeight * sin, cy - sprite.transform.halfHeight * cos + sprite.transform.halfWidth * sin)); - // Bottom Right - out.push(new Phaser.Point(cx - sprite.transform.halfWidth * cos + sprite.transform.halfHeight * sin, cy - sprite.transform.halfHeight * cos - sprite.transform.halfWidth * sin)); - return out; - }; SpriteUtils.getAsPoints = function getAsPoints(sprite) { var out = []; // top left @@ -16600,8 +16574,9 @@ var Phaser; if(sprite.transform.scrollFactor.equals(0)) { return true; } - return Phaser.RectangleUtils.intersects(sprite.cameraView, camera.screenView); - }; + return true; + //return RectangleUtils.intersects(sprite.cameraView, camera.screenView); + }; CanvasRenderer.prototype.inScreen = function (camera) { return true; }; diff --git a/build/phaser.d.ts b/build/phaser.d.ts index 47d1ea01..f6229317 100644 --- a/build/phaser.d.ts +++ b/build/phaser.d.ts @@ -1897,26 +1897,22 @@ module Phaser.Components { */ constructor(parent); private _rotation; - private _cachedSin; - private _cachedCos; - private _cachedRotation; - private _cachedScaleX; - private _cachedScaleY; - private _cachedAngle; - private _cachedAngleToCenter; - private _cachedDistance; - private _cachedWidth; - private _cachedHeight; - private _cachedHalfWidth; - private _cachedHalfHeight; - private _cachedCosAngle; - private _cachedSinAngle; - private _cachedOffsetX; - private _cachedOffsetY; - private _cachedOriginX; - private _cachedOriginY; - private _cachedCenterX; - private _cachedCenterY; + private _pos; + private _scale; + private _size; + private _halfSize; + private _offset; + private _origin; + private _sc; + private _scA; + private _angle; + private _distance; + private _prevRotation; + public center: Point; + public upperLeft: Point; + public upperRight: Point; + public bottomLeft: Point; + public bottomRight: Point; public local: Mat3; public setCache(): void; public update(): void; @@ -1979,14 +1975,6 @@ module Phaser.Components { * Half the height of the parent sprite, taking into consideration scaling */ public halfHeight : number; - /** - * The center of the Sprite in world coordinates, after taking scaling and rotation into consideration - */ - public centerX : number; - /** - * The center of the Sprite in world coordinates, after taking scaling and rotation into consideration - */ - public centerY : number; public sin : number; public cos : number; } @@ -2715,9 +2703,6 @@ module Phaser { * @return {Rectangle} A reference to the Sprite.cameraView property */ static updateCameraView(camera: Camera, sprite: Sprite): Rectangle; - static getCornersAsPoints(sprite: Sprite): Point[]; - static getCornersAsPoints2(sprite: Sprite): Point[]; - static getCornersAsPoints3(sprite: Sprite): Point[]; static getAsPoints(sprite: Sprite): Point[]; /** * Checks to see if a point in 2D world space overlaps this GameObject. diff --git a/build/phaser.js b/build/phaser.js index 279c2dfc..cc94bbce 100644 --- a/build/phaser.js +++ b/build/phaser.js @@ -3082,84 +3082,112 @@ var Phaser; this.origin = new Phaser.Vec2(); this.scale = new Phaser.Vec2(1, 1); this.skew = new Phaser.Vec2(); + this.center = new Phaser.Point(); + this.upperLeft = new Phaser.Point(); + this.upperRight = new Phaser.Point(); + this.bottomLeft = new Phaser.Point(); + this.bottomRight = new Phaser.Point(); + this._pos = new Phaser.Point(); + this._scale = new Phaser.Point(); + this._size = new Phaser.Point(); + this._halfSize = new Phaser.Point(); + this._offset = new Phaser.Point(); + this._origin = new Phaser.Point(); + this._sc = new Phaser.Point(); + this._scA = new Phaser.Point(); } Transform.prototype.setCache = function () { - this._cachedHalfWidth = this.parent.width / 2; - this._cachedHalfHeight = this.parent.height / 2; - this._cachedOffsetX = this.origin.x * this.parent.width; - this._cachedOffsetY = this.origin.y * this.parent.height; - this._cachedAngleToCenter = Math.atan2(this.halfHeight - this._cachedOffsetY, this.halfWidth - this._cachedOffsetX); - this._cachedDistance = Math.sqrt(((this._cachedOffsetX - this._cachedHalfWidth) * (this._cachedOffsetX - this._cachedHalfWidth)) + ((this._cachedOffsetY - this._cachedHalfHeight) * (this._cachedOffsetY - this._cachedHalfHeight))); - this._cachedWidth = this.parent.width; - this._cachedHeight = this.parent.height; - this._cachedOriginX = this.origin.x; - this._cachedOriginY = this.origin.y; - this._cachedSin = Math.sin((this.rotation + this.rotationOffset) * Phaser.GameMath.DEG_TO_RAD); - this._cachedCos = Math.cos((this.rotation + this.rotationOffset) * Phaser.GameMath.DEG_TO_RAD); - this._cachedCosAngle = Math.cos((this.rotation + this.rotationOffset) * Phaser.GameMath.DEG_TO_RAD + this._cachedAngleToCenter); - this._cachedSinAngle = Math.sin((this.rotation + this.rotationOffset) * Phaser.GameMath.DEG_TO_RAD + this._cachedAngleToCenter); - this._cachedRotation = this.rotation; + this._pos.x = this.parent.x; + this._pos.y = this.parent.y; + this._halfSize.x = this.parent.width / 2; + this._halfSize.y = this.parent.height / 2; + this._offset.x = this.origin.x * this.parent.width; + this._offset.y = this.origin.y * this.parent.height; + this._angle = Math.atan2(this.halfHeight - this._offset.y, this.halfWidth - this._offset.x); + this._distance = Math.sqrt(((this._offset.x - this._halfSize.x) * (this._offset.x - this._halfSize.x)) + ((this._offset.y - this._halfSize.y) * (this._offset.y - this._halfSize.y))); + this._size.x = this.parent.width; + this._size.y = this.parent.height; + this._origin.x = this.origin.x; + this._origin.y = this.origin.y; + this._sc.x = Math.sin((this.rotation + this.rotationOffset) * Phaser.GameMath.DEG_TO_RAD); + this._sc.y = Math.cos((this.rotation + this.rotationOffset) * Phaser.GameMath.DEG_TO_RAD); + this._scA.y = Math.cos((this.rotation + this.rotationOffset) * Phaser.GameMath.DEG_TO_RAD + this._angle); + this._scA.x = Math.sin((this.rotation + this.rotationOffset) * Phaser.GameMath.DEG_TO_RAD + this._angle); + this._prevRotation = this.rotation; if(this.parent.texture && this.parent.texture.renderRotation) { - this._cachedSin = Math.sin((this.rotation + this.rotationOffset) * Phaser.GameMath.DEG_TO_RAD); - this._cachedCos = Math.cos((this.rotation + this.rotationOffset) * Phaser.GameMath.DEG_TO_RAD); + this._sc.x = Math.sin((this.rotation + this.rotationOffset) * Phaser.GameMath.DEG_TO_RAD); + this._sc.y = Math.cos((this.rotation + this.rotationOffset) * Phaser.GameMath.DEG_TO_RAD); } else { - this._cachedSin = 0; - this._cachedCos = 1; + this._sc.x = 0; + this._sc.y = 1; } + this.center.setTo(this.center.x, this.center.y); + this.upperLeft.setTo(this.center.x - this._halfSize.x * this._sc.y + this._halfSize.y * this._sc.x, this.center.y - this._halfSize.y * this._sc.y - this._halfSize.x * this._sc.x); + this.upperRight.setTo(this.center.x + this._halfSize.x * this._sc.y + this._halfSize.y * this._sc.x, this.center.y - this._halfSize.y * this._sc.y + this._halfSize.x * this._sc.x); + this.bottomLeft.setTo(this.center.x - this._halfSize.x * this._sc.y - this._halfSize.y * this._sc.x, this.center.y + this._halfSize.y * this._sc.y - this._halfSize.x * this._sc.x); + this.bottomRight.setTo(this.center.x + this._halfSize.x * this._sc.y - this._halfSize.y * this._sc.x, this.center.y + this._halfSize.y * this._sc.y + this._halfSize.x * this._sc.x); }; Transform.prototype.update = function () { // Check cache var dirty = false; // 1) Height or Width change (also triggered by a change in scale) or an Origin change - if(this.parent.width !== this._cachedWidth || this.parent.height !== this._cachedHeight || this.origin.x !== this._cachedOriginX || this.origin.y !== this._cachedOriginY) { - this._cachedHalfWidth = this.parent.width / 2; - this._cachedHalfHeight = this.parent.height / 2; - this._cachedOffsetX = this.origin.x * this.parent.width; - this._cachedOffsetY = this.origin.y * this.parent.height; - this._cachedAngleToCenter = Math.atan2(this.halfHeight - this._cachedOffsetY, this.halfWidth - this._cachedOffsetX); - this._cachedDistance = Math.sqrt(((this._cachedOffsetX - this._cachedHalfWidth) * (this._cachedOffsetX - this._cachedHalfWidth)) + ((this._cachedOffsetY - this._cachedHalfHeight) * (this._cachedOffsetY - this._cachedHalfHeight))); + if(this.parent.width !== this._size.x || this.parent.height !== this._size.y || this.origin.x !== this._origin.x || this.origin.y !== this._origin.y) { + this._halfSize.x = this.parent.width / 2; + this._halfSize.y = this.parent.height / 2; + this._offset.x = this.origin.x * this.parent.width; + this._offset.y = this.origin.y * this.parent.height; + this._angle = Math.atan2(this.halfHeight - this._offset.y, this.halfWidth - this._offset.x); + this._distance = Math.sqrt(((this._offset.x - this._halfSize.x) * (this._offset.x - this._halfSize.x)) + ((this._offset.y - this._halfSize.y) * (this._offset.y - this._halfSize.y))); // Store - this._cachedWidth = this.parent.width; - this._cachedHeight = this.parent.height; - this._cachedOriginX = this.origin.x; - this._cachedOriginY = this.origin.y; + this._size.x = this.parent.width; + this._size.y = this.parent.height; + this._origin.x = this.origin.x; + this._origin.y = this.origin.y; dirty = true; } // 2) Rotation change - if(this.rotation != this._cachedRotation) { - this._cachedSin = Math.sin((this.rotation + this.rotationOffset) * Phaser.GameMath.DEG_TO_RAD); - this._cachedCos = Math.cos((this.rotation + this.rotationOffset) * Phaser.GameMath.DEG_TO_RAD); - this._cachedCosAngle = Math.cos((this.rotation + this.rotationOffset) * Phaser.GameMath.DEG_TO_RAD + this._cachedAngleToCenter); - this._cachedSinAngle = Math.sin((this.rotation + this.rotationOffset) * Phaser.GameMath.DEG_TO_RAD + this._cachedAngleToCenter); + if(this.rotation != this._prevRotation) { + this._sc.x = Math.sin((this.rotation + this.rotationOffset) * Phaser.GameMath.DEG_TO_RAD); + this._sc.y = Math.cos((this.rotation + this.rotationOffset) * Phaser.GameMath.DEG_TO_RAD); + this._scA.y = Math.cos((this.rotation + this.rotationOffset) * Phaser.GameMath.DEG_TO_RAD + this._angle); + this._scA.x = Math.sin((this.rotation + this.rotationOffset) * Phaser.GameMath.DEG_TO_RAD + this._angle); if(this.parent.texture.renderRotation) { - this._cachedSin = Math.sin((this.rotation + this.rotationOffset) * Phaser.GameMath.DEG_TO_RAD); - this._cachedCos = Math.cos((this.rotation + this.rotationOffset) * Phaser.GameMath.DEG_TO_RAD); + this._sc.x = Math.sin((this.rotation + this.rotationOffset) * Phaser.GameMath.DEG_TO_RAD); + this._sc.y = Math.cos((this.rotation + this.rotationOffset) * Phaser.GameMath.DEG_TO_RAD); } else { - this._cachedSin = 0; - this._cachedCos = 1; + this._sc.x = 0; + this._sc.y = 1; } // Store - this._cachedRotation = this.rotation; + this._prevRotation = this.rotation; dirty = true; } - if(dirty) { - this._cachedCenterX = this.parent.x + this._cachedDistance * this._cachedCosAngle; - this._cachedCenterY = this.parent.y + this._cachedDistance * this._cachedSinAngle; + // If it has moved, updated the edges and center + if(dirty || this.parent.x != this._pos.x || this.parent.y != this._pos.y) { + this.center.x = this.parent.x + this._distance * this._scA.y; + this.center.y = this.parent.y + this._distance * this._scA.x; + this.center.setTo(this.center.x, this.center.y); + this.upperLeft.setTo(this.center.x - this._halfSize.x * this._sc.y + this._halfSize.y * this._sc.x, this.center.y - this._halfSize.y * this._sc.y - this._halfSize.x * this._sc.x); + this.upperRight.setTo(this.center.x + this._halfSize.x * this._sc.y + this._halfSize.y * this._sc.x, this.center.y - this._halfSize.y * this._sc.y + this._halfSize.x * this._sc.x); + this.bottomLeft.setTo(this.center.x - this._halfSize.x * this._sc.y - this._halfSize.y * this._sc.x, this.center.y + this._halfSize.y * this._sc.y - this._halfSize.x * this._sc.x); + this.bottomRight.setTo(this.center.x + this._halfSize.x * this._sc.y - this._halfSize.y * this._sc.x, this.center.y + this._halfSize.y * this._sc.y + this._halfSize.x * this._sc.x); + this._pos.x = this.parent.x; + this._pos.y = this.parent.y; } // Scale and Skew if(this.parent.texture.flippedX) { - this.local.data[0] = this._cachedCos * -this.scale.x; - this.local.data[3] = (this._cachedSin * -this.scale.x) + this.skew.x; + this.local.data[0] = this._sc.y * -this.scale.x; + this.local.data[3] = (this._sc.x * -this.scale.x) + this.skew.x; } else { - this.local.data[0] = this._cachedCos * this.scale.x; - this.local.data[3] = (this._cachedSin * this.scale.x) + this.skew.x; + this.local.data[0] = this._sc.y * this.scale.x; + this.local.data[3] = (this._sc.x * this.scale.x) + this.skew.x; } if(this.parent.texture.flippedY) { - this.local.data[4] = this._cachedCos * -this.scale.y; - this.local.data[1] = -(this._cachedSin * -this.scale.y) + this.skew.y; + this.local.data[4] = this._sc.y * -this.scale.y; + this.local.data[1] = -(this._sc.x * -this.scale.y) + this.skew.y; } else { - this.local.data[4] = this._cachedCos * this.scale.y; - this.local.data[1] = -(this._cachedSin * this.scale.y) + this.skew.y; + this.local.data[4] = this._sc.y * this.scale.y; + this.local.data[1] = -(this._sc.x * this.scale.y) + this.skew.y; } // Translate this.local.data[2] = this.parent.x; @@ -3170,9 +3198,8 @@ var Phaser; * The distance from the center of the transform to the rotation origin. */ function () { - return this._cachedDistance; - //return Math.sqrt(((this.offsetX - this.halfWidth) * (this.offsetX - this.halfWidth)) + ((this.offsetY - this.halfHeight) * (this.offsetY - this.halfHeight))); - }, + return this._distance; + }, enumerable: true, configurable: true }); @@ -3181,9 +3208,8 @@ var Phaser; * The angle between the center of the transform to the rotation origin. */ function () { - return this._cachedAngleToCenter; - //return Math.atan2(this.halfHeight - this.offsetY, this.halfWidth - this.offsetX); - }, + return this._angle; + }, enumerable: true, configurable: true }); @@ -3192,9 +3218,8 @@ var Phaser; * The offset on the X axis of the origin */ function () { - return this._cachedOffsetX; - //return this.origin.x * this.parent.width; - }, + return this._offset.x; + }, enumerable: true, configurable: true }); @@ -3203,9 +3228,8 @@ var Phaser; * The offset on the Y axis of the origin */ function () { - return this._cachedOffsetY; - //return this.origin.y * this.parent.height; - }, + return this._offset.y; + }, enumerable: true, configurable: true }); @@ -3214,9 +3238,8 @@ var Phaser; * Half the width of the parent sprite, taking into consideration scaling */ function () { - return this._cachedHalfWidth; - //return this.parent.width / 2; - }, + return this._halfSize.x; + }, enumerable: true, configurable: true }); @@ -3225,44 +3248,33 @@ var Phaser; * Half the height of the parent sprite, taking into consideration scaling */ function () { - return this._cachedHalfHeight; - //return this.parent.height / 2; - }, - enumerable: true, - configurable: true - }); - Object.defineProperty(Transform.prototype, "centerX", { - get: /** - * The center of the Sprite in world coordinates, after taking scaling and rotation into consideration - */ - function () { - return this._cachedCenterX; - //return this.parent.x + this.distance * Math.cos((this.rotation * Math.PI / 180) + this.angleToCenter); - }, - enumerable: true, - configurable: true - }); - Object.defineProperty(Transform.prototype, "centerY", { - get: /** - * The center of the Sprite in world coordinates, after taking scaling and rotation into consideration - */ - function () { - return this._cachedCenterY; - //return this.parent.y + this.distance * Math.sin((this.rotation * Math.PI / 180) + this.angleToCenter); - }, + return this._halfSize.y; + }, enumerable: true, configurable: true }); Object.defineProperty(Transform.prototype, "sin", { - get: function () { - return this._cachedSin; + get: /** + * The center of the Sprite in world coordinates, after taking scaling and rotation into consideration + */ + //public get centerX(): number { + // return this.center.x; + //} + /** + * The center of the Sprite in world coordinates, after taking scaling and rotation into consideration + */ + //public get centerY(): number { + // return this.center.y; + //} + function () { + return this._sc.x; }, enumerable: true, configurable: true }); Object.defineProperty(Transform.prototype, "cos", { get: function () { - return this._cachedCos; + return this._sc.y; }, enumerable: true, configurable: true @@ -4656,8 +4668,8 @@ var Phaser; } else { // If the sprite is rotated around its center we can use this quicker method: if(sprite.transform.origin.x == 0.5 && sprite.transform.origin.y == 0.5) { - SpriteUtils._sin = Math.sin((sprite.rotation + sprite.transform.rotationOffset) * Phaser.GameMath.DEG_TO_RAD); - SpriteUtils._cos = Math.cos((sprite.rotation + sprite.transform.rotationOffset) * Phaser.GameMath.DEG_TO_RAD); + SpriteUtils._sin = sprite.transform.sin; + SpriteUtils._cos = sprite.transform.cos; if(SpriteUtils._sin < 0) { SpriteUtils._sin = -SpriteUtils._sin; } @@ -4669,6 +4681,20 @@ var Phaser; sprite.cameraView.x = Math.round(sprite.x - (camera.worldView.x * sprite.transform.scrollFactor.x) - (sprite.cameraView.width * sprite.transform.origin.x)); sprite.cameraView.y = Math.round(sprite.y - (camera.worldView.y * sprite.transform.scrollFactor.y) - (sprite.cameraView.height * sprite.transform.origin.y)); } else { + //var left:Number = Math.min(topLeft.x, topRight.x, bottomRight.x, bottomLeft.x); + //var top:Number = Math.min(topLeft.y, topRight.y, bottomRight.y, bottomLeft.y); + //var right:Number = Math.max(topLeft.x, topRight.x, bottomRight.x, bottomLeft.x); + //var bottom:Number = Math.max(topLeft.y, topRight.y, bottomRight.y, bottomLeft.y); + //return new Rectangle(left, top, right - left, bottom - top); + var minX = Math.min(sprite.transform.upperLeft.x, sprite.transform.upperRight.x, sprite.transform.bottomLeft.x, sprite.transform.bottomRight.x); + var minY = Math.min(sprite.transform.upperLeft.y, sprite.transform.upperRight.y, sprite.transform.bottomLeft.y, sprite.transform.bottomRight.y); + var maxX = Math.max(sprite.transform.upperLeft.x, sprite.transform.upperRight.x, sprite.transform.bottomLeft.x, sprite.transform.bottomRight.x); + var maxY = Math.max(sprite.transform.upperLeft.y, sprite.transform.upperRight.y, sprite.transform.bottomLeft.y, sprite.transform.bottomRight.y); + // (min_x,min_y), (min_x,max_y), (max_x,max_y), (max_x,min_y) + sprite.cameraView.x = minX; + sprite.cameraView.y = minY; + sprite.cameraView.width = maxX - minX; + sprite.cameraView.height = maxY - minY; /* // Useful to get the maximum AABB size of any given rect @@ -4686,58 +4712,6 @@ var Phaser; } return sprite.cameraView; }; - SpriteUtils.getCornersAsPoints = function getCornersAsPoints(sprite) { - var out = []; - var sin = Math.sin((sprite.transform.rotation + sprite.transform.rotationOffset) * Phaser.GameMath.DEG_TO_RAD); - var cos = Math.cos((sprite.transform.rotation + sprite.transform.rotationOffset) * Phaser.GameMath.DEG_TO_RAD); - // Upper Left - out.push(new Phaser.Point(sprite.x + (sprite.width / 2) * cos - (sprite.height / 2) * sin, sprite.y + (sprite.height / 2) * cos + (sprite.width / 2) * sin)); - // Upper Right - out.push(new Phaser.Point(sprite.x - (sprite.width / 2) * cos - (sprite.height / 2) * sin, sprite.y + (sprite.height / 2) * cos - (sprite.width / 2) * sin)); - // Bottom Left - out.push(new Phaser.Point(sprite.x + (sprite.width / 2) * cos + (sprite.height / 2) * sin, sprite.y - (sprite.height / 2) * cos + (sprite.width / 2) * sin)); - // Bottom Right - out.push(new Phaser.Point(sprite.x - (sprite.width / 2) * cos + (sprite.height / 2) * sin, sprite.y - (sprite.height / 2) * cos - (sprite.width / 2) * sin)); - return out; - }; - SpriteUtils.getCornersAsPoints2 = function getCornersAsPoints2(sprite) { - var out = []; - var sin = Math.sin((sprite.transform.rotation + sprite.transform.rotationOffset) * Phaser.GameMath.DEG_TO_RAD); - var cos = Math.cos((sprite.transform.rotation + sprite.transform.rotationOffset) * Phaser.GameMath.DEG_TO_RAD); - // This = the center point - var cx = sprite.x + (sprite.width / 2) * cos - (sprite.height / 2) * sin; - var cy = sprite.y + (sprite.height / 2) * cos + (sprite.width / 2) * sin; - // Upper Left - out.push(new Phaser.Point(cx + (sprite.width / 2) * cos - (sprite.height / 2) * sin, cy + (sprite.height / 2) * cos + (sprite.width / 2) * sin)); - // Upper Right - out.push(new Phaser.Point(cx - (sprite.width / 2) * cos - (sprite.height / 2) * sin, cy + (sprite.height / 2) * cos - (sprite.width / 2) * sin)); - // Bottom Left - out.push(new Phaser.Point(cx + (sprite.width / 2) * cos + (sprite.height / 2) * sin, cy - (sprite.height / 2) * cos + (sprite.width / 2) * sin)); - // Bottom Right - out.push(new Phaser.Point(cx - (sprite.width / 2) * cos + (sprite.height / 2) * sin, cy - (sprite.height / 2) * cos - (sprite.width / 2) * sin)); - return out; - }; - SpriteUtils.getCornersAsPoints3 = function getCornersAsPoints3(sprite) { - var out = []; - var sin = sprite.transform.sin; - var cos = sprite.transform.cos; - //var sin = Math.sin((sprite.transform.rotation + sprite.transform.rotationOffset) * Phaser.GameMath.DEG_TO_RAD); - //var cos = Math.cos((sprite.transform.rotation + sprite.transform.rotationOffset) * Phaser.GameMath.DEG_TO_RAD); - // This = the center point - //var cx = sprite.x + sprite.transform.distance * Math.cos((sprite.transform.rotation * Math.PI / 180) + sprite.transform.angleToCenter); - //var cy = sprite.y + sprite.transform.distance * Math.sin((sprite.transform.rotation * Math.PI / 180) + sprite.transform.angleToCenter); - var cx = sprite.transform.centerX; - var cy = sprite.transform.centerY; - // Upper Left - out.push(new Phaser.Point(cx + sprite.transform.halfWidth * cos - sprite.transform.halfHeight * sin, cy + sprite.transform.halfHeight * cos + sprite.transform.halfWidth * sin)); - // Upper Right - out.push(new Phaser.Point(cx - sprite.transform.halfWidth * cos - sprite.transform.halfHeight * sin, cy + sprite.transform.halfHeight * cos - sprite.transform.halfWidth * sin)); - // Bottom Left - out.push(new Phaser.Point(cx + sprite.transform.halfWidth * cos + sprite.transform.halfHeight * sin, cy - sprite.transform.halfHeight * cos + sprite.transform.halfWidth * sin)); - // Bottom Right - out.push(new Phaser.Point(cx - sprite.transform.halfWidth * cos + sprite.transform.halfHeight * sin, cy - sprite.transform.halfHeight * cos - sprite.transform.halfWidth * sin)); - return out; - }; SpriteUtils.getAsPoints = function getAsPoints(sprite) { var out = []; // top left @@ -16600,8 +16574,9 @@ var Phaser; if(sprite.transform.scrollFactor.equals(0)) { return true; } - return Phaser.RectangleUtils.intersects(sprite.cameraView, camera.screenView); - }; + return true; + //return RectangleUtils.intersects(sprite.cameraView, camera.screenView); + }; CanvasRenderer.prototype.inScreen = function (camera) { return true; };