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

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.3 KiB

View file

@ -7,7 +7,7 @@
(function () {
var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create, update: update });
var game = new Phaser.Game(800, 600, Phaser.CANVAS, '', { preload: preload, create: create, update: update, render: render });
function preload() {
@ -88,6 +88,13 @@
}
function render () {
game.debug.renderQuadTree(game.physics.quadTree);
}
})();
</script>

View file

@ -23,8 +23,6 @@
var bullets;
var bulletTime = 0;
var pickle;
function create() {
player = game.add.sprite(400, 500, 'ship');
@ -36,13 +34,12 @@
{
for (var x = 0; x < 10; x++)
{
aliens.create(170 + x * 48, 100 + y * 50, 'alien');
aliens.create(x * 48, y * 50, 'alien');
}
}
// Shows off a known bug:
// aliens.x = 100;
// aliens.y = 50;
aliens.x = 100;
aliens.y = 50;
bullets = game.add.group(null, 'bullets');
@ -52,18 +49,15 @@
b.name = 'bullet' + i;
b.exists = false;
b.visible = false;
b.anchor.setTo(0.5, 1);
b.events.onOutOfBounds.add(resetBullet, this);
}
// Shows off a known bug:
// var tween = game.add.tween(aliens).to({x: 200}, 3000, Phaser.Easing.Linear.None, true, 0, 1000, true);
// tween.onComplete.add(descend, this);
var tween = game.add.tween(aliens).to({x: 200}, 3000, Phaser.Easing.Linear.None, true, 0, 1000, true);
tween.onComplete.add(descend, this);
}
function overAlien () {
console.log('over pickle');
}
function descend() {
aliens.y += 10;
@ -100,7 +94,7 @@
if (bullet)
{
bullet.reset(player.x + 6, player.y - 8);
bullet.reset(player.x, player.y - 8);
bullet.velocity.y = -300;
bulletTime = game.time.now + 250;
}
@ -122,7 +116,7 @@
function render () {
aliens.forEach(renderBounds, this);
// aliens.forEach(renderBounds, this);
game.debug.renderQuadTree(game.physics.quadTree);

View file

@ -9,9 +9,12 @@
if (isset($mobile))
{
?>
<meta name="viewport" content="initial-scale=1 maximum-scale=1 user-scalable=0" />
<?php
echo ' <meta name="viewport" content="initial-scale=1 maximum-scale=1 user-scalable=0" />';
}
if (isset($css))
{
echo " <style>$css</style>";
}
?>
</head>

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();

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

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);
}
}

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();
// }
},

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;
}
};

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;