Sprite bounds finally correct, regardless of rotation, parenting, scale or anchor.

This commit is contained in:
photonstorm
2013-10-25 03:49:14 +01:00
parent 1469663ea5
commit 427819c655
3 changed files with 50 additions and 131 deletions
+4 -6
View File
@@ -55,14 +55,12 @@ function create() {
button5.scale.setTo(0.5, 0.5);
// Scaled and Rotated button
button6 = game.add.button(600, 200, 'button', changeSky, this, 2, 1, 0);
button6 = game.add.button(570, 200, 'button', changeSky, this, 2, 1, 0); // anchor 0.5
// button6 = game.add.button(470, 100, 'button', changeSky, this, 2, 1, 0); // anchor 0
button6.name = 'sky6';
button6.angle = 32;
button6.scale.setTo(2, 2);
button6.anchor.setTo(0.5, 0.5);
// scale + anchor works fine
// angle + anchor works fine
// scale + angle + anchor falls over
// game.world.setBounds(0, 0, 2000, 2000);
@@ -87,8 +85,8 @@ function render () {
game.debug.renderSpriteCorners(button5, false, true);
game.debug.renderSpriteCorners(button6, false, true);
// game.debug.renderLocalTransformInfo(button3, 32, 132);
// game.debug.renderText('ox: ' + button1.offset.x + ' oy: ' + button1.offset.y, 32, 200);
// game.debug.renderWorldTransformInfo(button1, 32, 132);
// game.debug.renderText('sx: ' + button3.scale.x + ' sy: ' + button3.scale.y + ' w: ' + button3.width + ' cw: ' + button3._cache.width, 32, 20);
game.debug.renderPoint(button6.input._tempPoint);
}
+42 -121
View File
@@ -422,7 +422,6 @@ Phaser.Sprite.prototype.updateAnimation = function() {
if (this.currentFrame && this.currentFrame.uuid != this._cache.frameID)
{
console.log('ua frame 1 change', this.name);
this._cache.frameWidth = this.texture.frame.width;
this._cache.frameHeight = this.texture.frame.height;
this._cache.frameID = this.currentFrame.uuid;
@@ -454,22 +453,22 @@ Phaser.Sprite.prototype.updateAnimation = function() {
*/
Phaser.Sprite.prototype.updateBounds = function() {
// Update the edge points
var sx = 1;
var sy = 1;
this.offset.setTo(this._cache.a02 - (this.anchor.x * this._cache.width), this._cache.a12 - (this.anchor.y * this._cache.height));
if (this.worldTransform[3] !== 0 || this.worldTransform[1] !== 0)
{
sx = this.scale.x;
sy = this.scale.y;
}
// this.offset.setTo(this._cache.a02 - (this.anchor.x * this.currentFrame.width), this._cache.a12 - (this.anchor.y * this.currentFrame.height));
// this.offset.setTo(this._cache.a02 - (this.anchor.x * this.width), this._cache.a12 - (this.anchor.y * this.height));
this.offset.setTo(this._cache.a02 - (this.anchor.x * this.width), this._cache.a12 - (this.anchor.y * this.height));
// this.offset.setTo(this._cache.a02 - (this.anchor.x * this._cache.width), this._cache.a12 - (this.anchor.y * this._cache.height));
console.log('updateBounds', this.name, this.offset.x, this.offset.y);
this.getLocalPosition(this.center, this.offset.x + this._cache.halfWidth, this.offset.y + this._cache.halfHeight);
this.getLocalPosition(this.topLeft, this.offset.x, this.offset.y);
this.getLocalPosition(this.topRight, this.offset.x + this._cache.width, this.offset.y);
this.getLocalPosition(this.bottomLeft, this.offset.x, this.offset.y + this._cache.height);
this.getLocalPosition(this.bottomRight, this.offset.x + this._cache.width, this.offset.y + this._cache.height);
this.getLocalPosition(this.center, this.offset.x + (this.width / 2), this.offset.y + (this.height / 2), sx, sy);
this.getLocalPosition(this.topLeft, this.offset.x, this.offset.y, sx, sy);
this.getLocalPosition(this.topRight, this.offset.x + this.width, this.offset.y, sx, sy);
this.getLocalPosition(this.bottomLeft, this.offset.x, this.offset.y + this.height, sx, sy);
this.getLocalPosition(this.bottomRight, this.offset.x + this.width, this.offset.y + this.height, sx, sy);
this._cache.left = Phaser.Math.min(this.topLeft.x, this.topRight.x, this.bottomLeft.x, this.bottomRight.x);
this._cache.right = Phaser.Math.max(this.topLeft.x, this.topRight.x, this.bottomLeft.x, this.bottomRight.x);
@@ -481,9 +480,6 @@ Phaser.Sprite.prototype.updateBounds = function() {
// This is the coordinate the Sprite was at when the last bounds was created
this._cache.boundsX = this._cache.x;
this._cache.boundsY = this._cache.y;
// this._cache.calcWidth = this.width;
// this._cache.calcHeight = this.height;
this.updateFrame = true;
@@ -519,59 +515,44 @@ Phaser.Sprite.prototype.updateBounds = function() {
/**
* Description.
*
* @method Phaser.Sprite.prototype.updateBounds
*
* @method Phaser.Sprite.prototype.getLocalPosition
* @param {Description} p - Description.
* @param {number} x - Description.
* @param {number} y - Description.
* @return {Description} Description.
*/
Phaser.Sprite.prototype.DEADupdateBounds = function() {
Phaser.Sprite.prototype.getLocalPosition = function(p, x, y, sx, sy) {
// Update the edge points
// p.x = ((this._cache.a11 * this._cache.id * x + -this._cache.a01 * this._cache.id * y + (this._cache.a12 * this._cache.a01 - this._cache.a02 * this._cache.a11) * this._cache.id) * this.scale.x) + this._cache.a02;
// p.y = ((this._cache.a00 * this._cache.id * y + -this._cache.a10 * this._cache.id * x + (-this._cache.a12 * this._cache.a00 + this._cache.a02 * this._cache.a10) * this._cache.id) * this.scale.y) + this._cache.a12;
this.offset.setTo(this._cache.a02 - (this.anchor.x * this.width), this._cache.a12 - (this.anchor.y * this.height));
p.x = ((this._cache.a11 * this._cache.id * x + -this._cache.a01 * this._cache.id * y + (this._cache.a12 * this._cache.a01 - this._cache.a02 * this._cache.a11) * this._cache.id) * sx) + this._cache.a02;
p.y = ((this._cache.a00 * this._cache.id * y + -this._cache.a10 * this._cache.id * x + (-this._cache.a12 * this._cache.a00 + this._cache.a02 * this._cache.a10) * this._cache.id) * sy) + this._cache.a12;
this.getLocalPosition(this.center, this.offset.x + this._cache.halfWidth, this.offset.y + this._cache.halfHeight);
this.getLocalPosition(this.topLeft, this.offset.x, this.offset.y);
this.getLocalPosition(this.topRight, this.offset.x + this._cache.width, this.offset.y);
this.getLocalPosition(this.bottomLeft, this.offset.x, this.offset.y + this._cache.height);
this.getLocalPosition(this.bottomRight, this.offset.x + this._cache.width, this.offset.y + this._cache.height);
return p;
this._cache.left = Phaser.Math.min(this.topLeft.x, this.topRight.x, this.bottomLeft.x, this.bottomRight.x);
this._cache.right = Phaser.Math.max(this.topLeft.x, this.topRight.x, this.bottomLeft.x, this.bottomRight.x);
this._cache.top = Phaser.Math.min(this.topLeft.y, this.topRight.y, this.bottomLeft.y, this.bottomRight.y);
this._cache.bottom = Phaser.Math.max(this.topLeft.y, this.topRight.y, this.bottomLeft.y, this.bottomRight.y);
}
this.bounds.setTo(this._cache.left, this._cache.top, this._cache.right - this._cache.left, this._cache.bottom - this._cache.top);
/**
* Description.
*
* @method Phaser.Sprite.prototype.getLocalUnmodifiedPosition
* @param {Description} p - Description.
* @param {number} x - Description.
* @param {number} y - Description.
* @return {Description} Description.
*/
Phaser.Sprite.prototype.getLocalUnmodifiedPosition = function(p, x, y) {
// This is the coordinate the Sprite was at when the last bounds was created
this._cache.boundsX = this._cache.x;
this._cache.boundsY = this._cache.y;
p.x = this._cache.a11 * this._cache.idi * x + -this._cache.i01 * this._cache.idi * y + (this._cache.a12 * this._cache.i01 - this._cache.a02 * this._cache.a11) * this._cache.idi;
p.y = this._cache.a00 * this._cache.idi * y + -this._cache.i10 * this._cache.idi * x + (-this._cache.a12 * this._cache.a00 + this._cache.a02 * this._cache.i10) * this._cache.idi;
if (this.inWorld == false)
{
// Sprite WAS out of the screen, is it still?
this.inWorld = Phaser.Rectangle.intersects(this.bounds, this.game.world.bounds, this.inWorldThreshold);
// apply anchor
p.x += (this.anchor.x * this.width);
p.y += (this.anchor.y * this.height);
if (this.inWorld)
{
// It's back again, reset the OOB check
this._outOfBoundsFired = false;
}
}
else
{
// Sprite WAS in the screen, has it now left?
this.inWorld = Phaser.Rectangle.intersects(this.bounds, this.game.world.bounds, this.inWorldThreshold);
if (this.inWorld == false)
{
this.events.onOutOfBounds.dispatch(this);
this._outOfBoundsFired = true;
if (this.outOfBoundsKill)
{
this.kill();
}
}
}
return p;
}
@@ -830,66 +811,6 @@ Phaser.Sprite.prototype.reset = function(x, y, health) {
}
/**
* Description.
*
* @method Phaser.Sprite.prototype.getLocalPosition
* @param {Description} p - Description.
* @param {number} x - Description.
* @param {number} y - Description.
* @return {Description} Description.
*/
Phaser.Sprite.prototype.getLocalPosition = function(p, x, y) {
p.x = ((this._cache.a11 * this._cache.id * x + -this._cache.a01 * this._cache.id * y + (this._cache.a12 * this._cache.a01 - this._cache.a02 * this._cache.a11) * this._cache.id) * this.scale.x) + this._cache.a02;
p.y = ((this._cache.a00 * this._cache.id * y + -this._cache.a10 * this._cache.id * x + (-this._cache.a12 * this._cache.a00 + this._cache.a02 * this._cache.a10) * this._cache.id) * this.scale.y) + this._cache.a12;
/*
if (this.worldTransform[0] == 1)
{
p.x = ((this._cache.a11 * this._cache.id * x + -this._cache.a01 * this._cache.id * y + (this._cache.a12 * this._cache.a01 - this._cache.a02 * this._cache.a11) * this._cache.id) * 1) + this._cache.a02;
}
else
{
p.x = ((this._cache.a11 * this._cache.id * x + -this._cache.a01 * this._cache.id * y + (this._cache.a12 * this._cache.a01 - this._cache.a02 * this._cache.a11) * this._cache.id) * this.scale.x) + this._cache.a02;
}
if (this.worldTransform[4] == 1)
{
p.y = ((this._cache.a00 * this._cache.id * y + -this._cache.a10 * this._cache.id * x + (-this._cache.a12 * this._cache.a00 + this._cache.a02 * this._cache.a10) * this._cache.id) * 1) + this._cache.a12;
}
else
{
p.y = ((this._cache.a00 * this._cache.id * y + -this._cache.a10 * this._cache.id * x + (-this._cache.a12 * this._cache.a00 + this._cache.a02 * this._cache.a10) * this._cache.id) * this.scale.y) + this._cache.a12;
}
*/
return p;
}
/**
* Description.
*
* @method Phaser.Sprite.prototype.getLocalUnmodifiedPosition
* @param {Description} p - Description.
* @param {number} x - Description.
* @param {number} y - Description.
* @return {Description} Description.
*/
Phaser.Sprite.prototype.getLocalUnmodifiedPosition = function(p, x, y) {
p.x = this._cache.a11 * this._cache.idi * x + -this._cache.i01 * this._cache.idi * y + (this._cache.a12 * this._cache.i01 - this._cache.a02 * this._cache.a11) * this._cache.idi;
p.y = this._cache.a00 * this._cache.idi * y + -this._cache.i10 * this._cache.idi * x + (-this._cache.a12 * this._cache.a00 + this._cache.a02 * this._cache.i10) * this._cache.idi;
// apply anchor
p.x += (this.anchor.x * this.width);
p.y += (this.anchor.y * this.height);
return p;
}
/**
* Description.
*
+4 -4
View File
@@ -221,10 +221,10 @@ Phaser.Utils.Debug.prototype = {
// this.context.stroke();
this.renderPoint(sprite.center);
this.renderPoint(sprite.topLeft);
this.renderPoint(sprite.topRight);
this.renderPoint(sprite.bottomLeft);
this.renderPoint(sprite.bottomRight);
this.renderPoint(sprite.topLeft, 'rgb(255,255,0)');
this.renderPoint(sprite.topRight, 'rgb(255,0,0)');
this.renderPoint(sprite.bottomLeft, 'rgb(0,0,255)');
this.renderPoint(sprite.bottomRight, 'rgb(255,255,255)');
if (showText)
{