Lots more physics tests and updates.

PLEASE DO NOT upgrade to this release if you need your game working and it uses any of the physics functions, as they're nearly all broken here.
Just pushing up so I can share it with someone.
This commit is contained in:
photonstorm
2014-01-20 20:14:34 +00:00
parent 2532df8793
commit 128c7143d5
17 changed files with 1623 additions and 99 deletions
+86 -4
View File
@@ -310,14 +310,15 @@ Phaser.Physics.Arcade.prototype = {
/**
* Checks for collision between two game objects. You can perform Sprite vs. Sprite, Sprite vs. Group, Group vs. Group, Sprite vs. Tilemap Layer or Group vs. Tilemap Layer collisions.
* If you'd like to collide an array of objects see Phaser.Physics.Arcade#collideArray.
* The objects are also automatically separated. If you don't require separation then use ArcadePhysics.overlap instead.
* An optional processCallback can be provided. If given this function will be called when two sprites are found to be colliding. It is called before any separation takes place,
* giving you the chance to perform additional checks. If the function returns true then the collision and separation is carried out. If it returns false it is skipped.
* The collideCallback is an optional function that is only called if two sprites collide. If a processCallback has been set then it needs to return true for collideCallback to be called.
*
* @method Phaser.Physics.Arcade#collide
* @param {Phaser.Sprite|Phaser.Group|Phaser.Particles.Emitter|Phaser.Tilemap} object1 - The first object to check. Can be an instance of Phaser.Sprite, Phaser.Group, Phaser.Particles.Emitter, or Phaser.Tilemap
* @param {Phaser.Sprite|Phaser.Group|Phaser.Particles.Emitter|Phaser.Tilemap} object2 - The second object to check. Can be an instance of Phaser.Sprite, Phaser.Group, Phaser.Particles.Emitter or Phaser.Tilemap
* @param {Phaser.Sprite|Phaser.Group|Phaser.Particles.Emitter|Phaser.Tilemap} object1 - The first object to check. Can be an instance of Phaser.Sprite, Phaser.Group, Phaser.Particles.Emitter, or Phaser.Tilemap.
* @param {Phaser.Sprite|Phaser.Group|Phaser.Particles.Emitter|Phaser.Tilemap} object2 - The second object to check. Can be an instance of Phaser.Sprite, Phaser.Group, Phaser.Particles.Emitter or Phaser.Tilemap.
* @param {function} [collideCallback=null] - An optional callback function that is called if the objects collide. The two objects will be passed to this function in the same order in which you specified them.
* @param {function} [processCallback=null] - A callback function that lets you perform additional checks against the two objects if they overlap. If this is set then collision will only happen if processCallback returns true. The two objects will be passed to this function in the same order in which you specified them.
* @param {object} [callbackContext] - The context in which to run the callbacks.
@@ -332,6 +333,58 @@ Phaser.Physics.Arcade.prototype = {
this._result = false;
this._total = 0;
this.collideHandler(object1, object2, collideCallback, processCallback, callbackContext);
return (this._total > 0);
},
/**
* Checks for collision between a game object and an array of game objects. You can perform Sprite vs. Sprite, Sprite vs. Group, Group vs. Group, Sprite vs. Tilemap Layer or Group vs. Tilemap Layer collisions.
* The objects are also automatically separated. If you don't require separation then use ArcadePhysics.overlap instead.
* An optional processCallback can be provided. If given this function will be called when two sprites are found to be colliding. It is called before any separation takes place,
* giving you the chance to perform additional checks. If the function returns true then the collision and separation is carried out. If it returns false it is skipped.
* The collideCallback is an optional function that is only called if two sprites collide. If a processCallback has been set then it needs to return true for collideCallback to be called.
*
* @method Phaser.Physics.Arcade#collideArray
* @param {Phaser.Sprite|Phaser.Group|Phaser.Particles.Emitter|Phaser.Tilemap} object1 - The first object to check. Can be an instance of Phaser.Sprite, Phaser.Group, Phaser.Particles.Emitter, or Phaser.Tilemap.
* @param {<Phaser.Sprite>array|<Phaser.Group>array|<Phaser.Particles.Emitter>array|<Phaser.Tilemap>array} objectArray - An array of objects to check. Can contain instances of Phaser.Sprite, Phaser.Group, Phaser.Particles.Emitter or Phaser.Tilemap.
* @param {function} [collideCallback=null] - An optional callback function that is called if the objects collide. The two objects will be passed to this function in the same order in which you specified them.
* @param {function} [processCallback=null] - A callback function that lets you perform additional checks against the two objects if they overlap. If this is set then collision will only happen if processCallback returns true. The two objects will be passed to this function in the same order in which you specified them.
* @param {object} [callbackContext] - The context in which to run the callbacks.
* @returns {boolean} True if a collision occured otherwise false.
*/
collideArray: function (object1, objectArray, collideCallback, processCallback, callbackContext) {
collideCallback = collideCallback || null;
processCallback = processCallback || null;
callbackContext = callbackContext || collideCallback;
this._result = false;
this._total = 0;
for (var i = 0, len = objectArray.length; i < len; i++)
{
this.collideHandler(object1, objectArray[i], collideCallback, processCallback, callbackContext);
}
return (this._total > 0);
},
/**
* Internal collision handler.
*
* @method Phaser.Physics.Arcade#collideHandler
* @private
* @param {Phaser.Sprite|Phaser.Group|Phaser.Particles.Emitter|Phaser.Tilemap} object1 - The first object to check. Can be an instance of Phaser.Sprite, Phaser.Group, Phaser.Particles.Emitter, or Phaser.Tilemap.
* @param {Phaser.Sprite|Phaser.Group|Phaser.Particles.Emitter|Phaser.Tilemap} object2 - The second object to check. Can be an instance of Phaser.Sprite, Phaser.Group, Phaser.Particles.Emitter or Phaser.Tilemap. Can also be an array of objects to check.
* @param {function} [collideCallback=null] - An optional callback function that is called if the objects collide. The two objects will be passed to this function in the same order in which you specified them.
* @param {function} [processCallback=null] - A callback function that lets you perform additional checks against the two objects if they overlap. If this is set then collision will only happen if processCallback returns true. The two objects will be passed to this function in the same order in which you specified them.
* @param {object} [callbackContext] - The context in which to run the callbacks.
*/
collideHandler: function (object1, object2, collideCallback, processCallback, callbackContext) {
// Only collide valid objects
if (object1 && object2 && object1.exists && object2.exists)
{
@@ -397,8 +450,6 @@ Phaser.Physics.Arcade.prototype = {
}
}
return (this._total > 0);
},
/**
@@ -595,6 +646,36 @@ Phaser.Physics.Arcade.prototype = {
return false;
}
var r = false;
if (body1.deltaX() === 0 && body2.deltaX() === 0)
{
// They overlap but neither of them are moving
body1.embedded = true;
body2.embedded = true;
}
else if (body1.deltaX() != 0 && body2.deltaX() === 0)
{
r = body1.separate(body2);
}
else if (body2.deltaX() != 0 && body1.deltaX() === 0)
{
r = body2.separate(body1);
}
else
{
// Dual motion thingy
r = body1.separate(body2);
}
return r;
// return body2.separate(body1);
/*
// We got this far, so the bodies overlap and our process was good, which means we can separate ...
this._overlap = 0;
@@ -900,6 +981,7 @@ Phaser.Physics.Arcade.prototype = {
}
return true;
*/
},
+234 -74
View File
@@ -137,7 +137,7 @@ Phaser.Physics.Arcade.Body = function (sprite) {
/**
* @property {number} minBounceVelocity - The minimum bounce velocity (could just be the bounce value?).
*/
this.minBounceVelocity = 0.5;
// this.minBounceVelocity = 0.5;
this._debug = 0;
@@ -151,6 +151,12 @@ Phaser.Physics.Arcade.Body = function (sprite) {
*/
this.bounce = new Phaser.Point();
/**
* @property {Phaser.Point} minVelocity - When a body rebounds off another the minVelocity is checked, if the new velocity is lower than the minVelocity the body is stopped.
* @default
*/
this.minVelocity = new Phaser.Point(20, 20);
/**
* @property {Phaser.Point} maxVelocity - The maximum velocity in pixels per second sq. that the Body can reach.
* @default
@@ -200,6 +206,7 @@ Phaser.Physics.Arcade.Body = function (sprite) {
* @property {object} touching - An object containing touching results.
*/
this.touching = { none: true, up: false, down: false, left: false, right: false };
this.touchingPoint = new Phaser.Point(this.x, this.y);
/**
* This object is populated with previous touching values from the bodies previous collision.
@@ -295,6 +302,7 @@ Phaser.Physics.Arcade.Body = function (sprite) {
* @property {object} blocked - An object containing on which faces this Body is blocked from moving, if any.
*/
this.blocked = { up: false, down: false, left: false, right: false };
this.blockedPoint = new Phaser.Point();
/**
* @property {number} _dx - Internal cache var.
@@ -360,14 +368,6 @@ Phaser.Physics.Arcade.Body.prototype = {
this.wasTouching.left = this.touching.left;
this.wasTouching.right = this.touching.right;
this.touching.none = true;
this.touching.up = false;
this.touching.down = false;
this.touching.left = false;
this.touching.right = false;
this.embedded = false;
this.screenX = (this.sprite.worldTransform[2] - (this.sprite.anchor.x * this.width)) + this.offset.x;
this.screenY = (this.sprite.worldTransform[5] - (this.sprite.anchor.y * this.height)) + this.offset.y;
@@ -375,18 +375,32 @@ Phaser.Physics.Arcade.Body.prototype = {
this.preY = (this.sprite.world.y - (this.sprite.anchor.y * this.height)) + this.offset.y;
this.preRotation = this.sprite.angle;
this.x = this.preX;
this.y = this.preY;
this.rotation = this.preRotation;
this.speed = Math.sqrt(this.velocity.x * this.velocity.x + this.velocity.y * this.velocity.y);
this.angle = Math.atan2(this.velocity.y, this.velocity.x);
// This all needs to move - because a body may start the preUpdate already touching something
// See if we can reduce this down?
this.blocked.up = false;
this.blocked.down = false;
this.blocked.left = false;
this.blocked.right = false;
this.embedded = false;
this.x = this.preX;
this.y = this.preY;
this.rotation = this.preRotation;
// if (this.deltaX() == 0 && (this.touchingPoint.x !== this.x || this.touchingPoint.y !== this.y))
// {
// console.log('touch reset', this.x, this.y, this.touchingPoint);
// this.touching.none = true;
// this.touching.up = false;
// this.touching.down = false;
// this.touching.left = false;
// this.touching.right = false;
// }
this.speed = Math.sqrt(this.velocity.x * this.velocity.x + this.velocity.y * this.velocity.y);
this.angle = Math.atan2(this.velocity.y, this.velocity.x);
this._debug++;
if (this.moves)
@@ -403,6 +417,44 @@ Phaser.Physics.Arcade.Body.prototype = {
},
/**
* Internal method used to check the Body against the World Bounds.
*
* @method Phaser.Physics.Arcade#checkWorldBounds
* @protected
*/
checkWorldBounds: function () {
if (this.x <= this.game.world.bounds.x)
{
this.overlapX = this.game.world.bounds.x - this.x;
this.blocked.left = true;
// console.log(this._debug, 'cwl', this.overlapX, this.x, this.game.world.bounds.x);
}
else if (this.right >= this.game.world.bounds.right)
{
this.overlapX = this.right - this.game.world.bounds.right;
this.blocked.right = true;
// console.log(this._debug, 'cwr', this.overlapX, this.x, this.game.world.bounds.x);
}
if (this.y <= this.game.world.bounds.y)
{
this.overlapY = this.game.world.bounds.y - this.y;
this.blocked.up = true;
// console.log(this._debug, 'cwu', this.overlapY, this.y, this.height, this.bottom, this.game.world.bounds.bottom);
}
else if (this.bottom >= this.game.world.bounds.bottom)
{
this.overlapY = this.bottom - this.game.world.bounds.bottom;
this.blocked.down = true;
// console.log(this._debug, 'cwd', this.overlapY, this.y, this.height, this.bottom, this.game.world.bounds.bottom);
}
this.blockedPoint.setTo(this.x, this.y);
},
/**
* Internal method.
*
@@ -438,7 +490,8 @@ Phaser.Physics.Arcade.Body.prototype = {
this._dx = this.game.time.physicsElapsed * (this.velocity.x + this.motionVelocity.x / 2);
if (this._dx > this.minBounceVelocity)
// if (this._dx > this.minBounceVelocity)
if (Math.abs(this.velocity.x) > this.minVelocity.x)
{
this.x += this._dx;
this.velocity.x += this.motionVelocity.x;
@@ -493,7 +546,8 @@ Phaser.Physics.Arcade.Body.prototype = {
this._dy = this.game.time.physicsElapsed * (this.velocity.y + this.motionVelocity.y / 2);
if (this._dy > this.minBounceVelocity)
// if (this._dy > this.minBounceVelocity)
if (Math.abs(this.velocity.y) > this.minVelocity.y)
{
this.y += this._dy;
this.velocity.y += this.motionVelocity.y;
@@ -516,7 +570,8 @@ Phaser.Physics.Arcade.Body.prototype = {
this._dy = this.game.time.physicsElapsed * (this.velocity.y + this.motionVelocity.y / 2);
if (this._dy < -this.minBounceVelocity)
// if (this._dy < -this.minBounceVelocity)
if (Math.abs(this.velocity.y) > this.minVelocity.y)
{
this.y += this._dy;
this.velocity.y += this.motionVelocity.y;
@@ -557,6 +612,145 @@ Phaser.Physics.Arcade.Body.prototype = {
},
// This body is the one that is checking against 'body'
separateX: function (body) {
// Doesn't matter if this is moving or not
console.log('--- separateX -----------------------------------------------------------------------------');
console.log(this.sprite.name, 'testing against', body.sprite.name);
console.log(this.sprite.name, 'moving left:', (this.deltaX() < 0), 'moving right:', (this.deltaX() > 0));
this.overlapX = 0;
if (this.x < body.x)
{
this.overlapX = this.right - body.x;
}
else
{
this.overlapX = body.right - this.x;
}
// There are 5 possible collisions taking place
// if (this.deltaX() === 0 && body.deltaX())
// if (this.deltaX() < 0 )
// if (this.deltaX() < 0 && this.allowCollision.left && body.allowCollision.right)
// This body is moving, the body it collided with is not
},
separate: function (body) {
console.log('-----------------------------------------------------------------------------');
console.log(this.sprite.name, 'testing against', body.sprite.name);
console.log(this.sprite.name, 'moving left:', (this.deltaX() < 0), 'moving right:', (this.deltaX() > 0));
this.overlapX = 0;
this.overlapY = 0;
if (this.deltaX() < 0 && this.allowCollision.left && body.allowCollision.right)
{
// LEFT (will create a negative overlapX value)
this.overlapX = this.x - body.right;
console.log(this.sprite.name, 'check left', body.right, this.overlapX);
}
else if (this.deltaX() > 0 && this.allowCollision.right && body.allowCollision.left)
{
// RIGHT (will create a positive overlapX value)
this.overlapX = this.right - body.x;
console.log(this.sprite.name, 'check right', this.overlapX);
}
if (this.deltaY() < 0 && this.allowCollision.up && body.allowCollision.down)
{
// UP (will create a negative overlapY value)
this.overlapY = this.y - body.bottom;
}
else if (this.deltaY() > 0 && this.allowCollision.down && body.allowCollision.up)
{
// DOWN
this.overlapY = this.bottom - body.y;
}
if (this.overlapX === 0 && this.overlapY === 0)
{
return false;
}
else
{
this.overlapHandler(this.overlapX, this.overlapY, body);
body.overlapHandler(this.overlapX, this.overlapY, this);
return true;
}
},
overlapHandler: function (x, y, body) {
if (x !== 0)
{
// Both moving, so let's cut the overlap in half
if (this.deltaX() != 0 && body.deltaX() != 0)
{
console.log(this.sprite.name, 'both overlap, halving');
x * 0.5;
}
if (this.deltaX() !== 0)
{
// Not moving, caught impact from another object
}
else
{
}
if (this.deltaX() < 0 && !this.blocked.right)
{
// This body is moving left
this.x += x;
console.log(this.sprite.name, 'touch h-left', this.x, x);
}
else if (this.deltaX() > 0 && !this.body.blocked.left)
{
this.x -= x;
console.log(this.sprite.name, 'touch h-right', this.x);
}
else
{
}
if (this.bounce.x === 0)
{
this.velocity.x = 0;
}
else
{
this.velocity.x = -this.velocity.x * this.bounce.x;
if (Math.abs(this.velocity.x) < this.minVelocity.x)
{
this.velocity.x = 0;
}
}
}
this.sprite.x += this.deltaX();
this.sprite.y += this.deltaY();
console.log(this.sprite.name, 'separate finished. Sprite.x', this.sprite.x, 'body x', this.x);
// reset delta ready for next collision
this.preX = this.x;
this.preY = this.y;
},
/**
* Internal method. This is called directly before the sprites are sent to the renderer.
*
@@ -567,27 +761,28 @@ Phaser.Physics.Arcade.Body.prototype = {
if (this.moves)
{
if (this.deltaX() < 0)
{
this.facing = Phaser.LEFT;
this.sprite.x += this.deltaX();
}
else if (this.deltaX() > 0)
{
this.facing = Phaser.RIGHT;
this.sprite.x += this.deltaX();
}
// if (this.deltaX() < 0)
// {
// this.facing = Phaser.LEFT;
// }
// else if (this.deltaX() > 0)
// {
// this.facing = Phaser.RIGHT;
// this.sprite.x += this.deltaX();
// }
if (this.deltaY() < 0)
{
this.facing = Phaser.UP;
this.sprite.y += this.deltaY();
}
else if (this.deltaY() > 0)
{
this.facing = Phaser.DOWN;
this.sprite.y += this.deltaY();
}
// if (this.deltaY() < 0)
// {
// this.facing = Phaser.UP;
// this.sprite.y += this.deltaY();
// }
// else if (this.deltaY() > 0)
// {
// this.facing = Phaser.DOWN;
// }
this.sprite.x += this.deltaX();
this.sprite.y += this.deltaY();
this.center.setTo(this.x + this.halfWidth, this.y + this.halfHeight);
@@ -599,41 +794,6 @@ Phaser.Physics.Arcade.Body.prototype = {
},
/**
* Internal method used to check the Body against the World Bounds.
*
* @method Phaser.Physics.Arcade#checkWorldBounds
* @protected
*/
checkWorldBounds: function () {
if (this.x < this.game.world.bounds.x)
{
this.overlapX = this.game.world.bounds.x - this.x;
this.blocked.left = true;
// console.log(this._debug, 'cwl', this.overlapX, this.x, this.game.world.bounds.x);
}
else if (this.right > this.game.world.bounds.right)
{
this.overlapX = this.right - this.game.world.bounds.right;
this.blocked.right = true;
// console.log(this._debug, 'cwr', this.overlapX, this.x, this.game.world.bounds.x);
}
if (this.y < this.game.world.bounds.y)
{
this.overlapY = this.game.world.bounds.y - this.y;
this.blocked.up = true;
// console.log(this._debug, 'cwu', this.overlapY, this.y, this.height, this.bottom, this.game.world.bounds.bottom);
}
else if (this.bottom > this.game.world.bounds.bottom)
{
this.overlapY = this.bottom - this.game.world.bounds.bottom;
this.blocked.down = true;
// console.log(this._debug, 'cwd', this.overlapY, this.y, this.height, this.bottom, this.game.world.bounds.bottom);
}
},
/**
* You can modify the size of the physics Body to be any dimension you need.