Fixing collision issues

This commit is contained in:
Richard Davey
2013-09-23 01:06:09 +01:00
parent b69b3db486
commit bc02a1a05e
10 changed files with 118 additions and 80 deletions
+2
View File
@@ -391,6 +391,8 @@ Phaser.Game.prototype = {
this.state.update();
this.plugins.update();
this.world.postUpdate();
this.renderer.render(this.stage._stage);
this.plugins.render();
this.state.render();
+24
View File
@@ -112,6 +112,30 @@ Phaser.World.prototype = {
},
/**
* This is called automatically every frame, and is where main logic happens.
* @method update
*/
postUpdate: function () {
if (this.game.stage._stage.first._iNext)
{
var currentNode = this.game.stage._stage.first._iNext;
do
{
if (currentNode['postUpdate'])
{
currentNode.postUpdate();
}
currentNode = currentNode._iNext;
}
while (currentNode != this.game.stage._stage.last._iNext)
}
},
/**
* Updates the size of this world.
* @method setSize
+9 -27
View File
@@ -210,17 +210,8 @@ Phaser.Sprite.prototype.preUpdate = function() {
this._cache.dirty = true;
}
this._cache.x = this.x - (this.game.world.camera.x * this.scrollFactor.x);
this._cache.y = this.y - (this.game.world.camera.y * this.scrollFactor.y);
// If this sprite or the camera have moved then let's update everything
// Note: The actual position shouldn't be changed if this item is inside a Group?
if (this.position.x != this._cache.x || this.position.y != this._cache.y)
{
this.position.x = this._cache.x;
this.position.y = this._cache.y;
this._cache.dirty = true;
}
// this._cache.x = this.x - (this.game.world.camera.x * this.scrollFactor.x);
// this._cache.y = this.y - (this.game.world.camera.y * this.scrollFactor.y);
if (this.visible)
{
@@ -281,21 +272,6 @@ Phaser.Sprite.prototype.preUpdate = function() {
this.updateBounds();
}
// }
// else
// {
// We still need to work out the bounds in case the camera has moved
// but we can't use the local or worldTransform to do it, as Pixi resets that if a Sprite is invisible.
// So we'll compare against the cached state + new position.
// if (this._cache.dirty && this.visible == false)
// {
// this.bounds.x -= this._cache.boundsX - this._cache.x;
// this._cache.boundsX = this._cache.x;
// this.bounds.y -= this._cache.boundsY - this._cache.y;
// this._cache.boundsY = this._cache.y;
// }
// }
// Re-run the camera visibility check
if (this._cache.dirty)
@@ -312,7 +288,7 @@ Phaser.Sprite.prototype.preUpdate = function() {
this.body.updateBounds(this.center.x, this.center.y, this._cache.scaleX, this._cache.scaleY);
}
this.body.update();
this.body.preUpdate();
}
@@ -320,7 +296,13 @@ Phaser.Sprite.prototype.postUpdate = function() {
if (this.exists)
{
// The sprite is positioned in this call, after taking into consideration motion updates and collision
this.body.postUpdate();
this.position.x -= (this.game.world.camera.x * this.scrollFactor.x);
this.position.y -= (this.game.world.camera.y * this.scrollFactor.y);
this.x -= (this.game.world.camera.x * this.scrollFactor.x);
this.y -= (this.game.world.camera.y * this.scrollFactor.y);
}
}
+5 -5
View File
@@ -380,11 +380,11 @@ Phaser.Physics.Arcade.prototype = {
this._result = (this.separateX(body1, body2) || this.separateY(body1, body2));
if (this._result)
{
body1.postUpdate();
body2.postUpdate();
}
// if (this._result)
// {
// body1.postUpdate();
// body2.postUpdate();
// }
},
+54 -30
View File
@@ -7,6 +7,8 @@ Phaser.Physics.Arcade.Body = function (sprite) {
this.x = sprite.x;
this.y = sprite.y;
this.preX = sprite.x;
this.preY = sprite.y;
this.lastX = sprite.x;
this.lastY = sprite.y;
@@ -82,7 +84,7 @@ Phaser.Physics.Arcade.Body.prototype = {
},
update: function () {
preUpdate: function () {
// Store and reset collision flags
this.wasTouching.none = this.touching.none;
@@ -97,13 +99,16 @@ Phaser.Physics.Arcade.Body.prototype = {
this.touching.left = false;
this.touching.right = false;
this.lastX = this.x;
this.lastY = this.y;
this.preX = (this.sprite.worldTransform[2] - (this.sprite.anchor.x * this.width)) + this.offset.x;
this.preY = (this.sprite.worldTransform[5] - (this.sprite.anchor.y * this.height)) + this.offset.y;
this.rotation = this.sprite.angle;
this.x = this.preX;
this.y = this.preY;
// There is a bug here in that the worldTransform values are what should be used, otherwise the quadTree gets the wrong rect given to it
this.x = (this.sprite.x - (this.sprite.anchor.x * this.width)) + this.offset.x;
this.y = (this.sprite.y - (this.sprite.anchor.y * this.height)) + this.offset.y;
// this.x = (this.sprite.x - (this.sprite.anchor.x * this.width)) + this.offset.x;
// this.y = (this.sprite.y - (this.sprite.anchor.y * this.height)) + this.offset.y;
// this.x = (this.sprite.worldTransform[2] - (this.sprite.anchor.x * this.width)) + this.offset.x;
// this.y = (this.sprite.worldTransform[5] - (this.sprite.anchor.y * this.height)) + this.offset.y;
@@ -124,20 +129,6 @@ Phaser.Physics.Arcade.Body.prototype = {
this.game.physics.quadTree.insert(this);
}
if (this.deltaX() != 0)
{
this.sprite.x -= this.deltaX();
}
if (this.deltaY() != 0)
{
this.sprite.y -= this.deltaY();
}
// Adjust the sprite based on all of the above, so the x/y coords will be correct going into the State update
this.sprite.x = this.x - this.offset.x + (this.sprite.anchor.x * this.width);
this.sprite.y = this.y - this.offset.y + (this.sprite.anchor.y * this.height);
if (this.allowRotation)
{
this.sprite.angle = this.rotation;
@@ -147,14 +138,43 @@ Phaser.Physics.Arcade.Body.prototype = {
postUpdate: function () {
this.sprite.x = this.x - this.offset.x + (this.sprite.anchor.x * this.width);
this.sprite.y = this.y - this.offset.y + (this.sprite.anchor.y * this.height);
if (this.allowRotation)
if (this.deltaX() != 0)
{
this.sprite.angle = this.rotation;
this.sprite.position.x += this.deltaX();
this.sprite.x += this.deltaX();
}
if (this.deltaY() != 0)
{
this.sprite.position.y += this.deltaY();
this.sprite.y += this.deltaY();
}
// this._cache.x = this.x - (this.game.world.camera.x * this.scrollFactor.x);
// this._cache.y = this.y - (this.game.world.camera.y * this.scrollFactor.y);
// if (this.position.x != this._cache.x || this.position.y != this._cache.y)
// {
// this.position.x = this._cache.x;
// this.position.y = this._cache.y;
// this._cache.dirty = true;
// }
// Adjust the sprite based on all of the above, so the x/y coords will be correct going into the State update
// this.sprite.x = this.x - this.offset.x + (this.sprite.anchor.x * this.width);
// this.sprite.y = this.y - this.offset.y + (this.sprite.anchor.y * this.height);
// this.sprite.x = this.x - this.offset.x + (this.sprite.anchor.x * this.width);
// this.sprite.y = this.y - this.offset.y + (this.sprite.anchor.y * this.height);
// if (this.allowRotation)
// {
// this.sprite.angle = this.rotation;
// }
},
checkWorldBounds: function () {
@@ -206,27 +226,31 @@ Phaser.Physics.Arcade.Body.prototype = {
this.angularVelocity = 0;
this.angularAcceleration = 0;
this.x = (this.sprite.x - (this.sprite.anchor.x * this.width)) + this.offset.x;
this.y = (this.sprite.y - (this.sprite.anchor.y * this.height)) + this.offset.y;
this.lastX = this.x;
this.lastY = this.y;
// this.x = (this.sprite.x - (this.sprite.anchor.x * this.width)) + this.offset.x;
// this.y = (this.sprite.y - (this.sprite.anchor.y * this.height)) + this.offset.y;
// this.lastX = this.x;
// this.lastY = this.y;
},
deltaAbsX: function () {
// return (this.deltaX() > 0 ? this.deltaX() : -this.deltaX());
return (this.deltaX() > 0 ? this.deltaX() : -this.deltaX());
},
deltaAbsY: function () {
// return (this.deltaY() > 0 ? this.deltaY() : -this.deltaY());
return (this.deltaY() > 0 ? this.deltaY() : -this.deltaY());
},
deltaX: function () {
return this.x - this.lastX;
// return this.x - this.lastX;
return this.x - this.preX;
},
deltaY: function () {
return this.y - this.lastY;
// return this.y - this.lastY;
return this.y - this.preY;
}
};
+2
View File
@@ -170,6 +170,8 @@ Phaser.Sound.prototype = {
},
// start and stop are in SECONDS.MS (2.5 = 2500ms, 0.5 = 500ms, etc)
// volume is between 0 and 1
addMarker: function (name, start, stop, volume, loop) {
volume = volume || 1;