More collision test cases and fixing a few issues as I go :)

This commit is contained in:
Richard Davey
2013-09-13 03:07:39 +01:00
parent f812b92b8a
commit ecc91fb4e0
7 changed files with 167 additions and 9 deletions
+3
View File
@@ -167,6 +167,9 @@ Phaser.Sprite = function (game, x, y, key, frame) {
// Set-up the physics body
this.body = new Phaser.Physics.Arcade.Body(this);
this.velocity = this.body.velocity;
this.acceleration = this.body.acceleration;
// World bounds check
this.inWorld = Phaser.Rectangle.intersects(this.bounds, this.game.world.bounds);
this.inWorldThreshold = 0;
+25 -2
View File
@@ -424,11 +424,12 @@ Phaser.Physics.Arcade.prototype = {
*/
separate: function (body1, body2) {
if (this.separateX(body1, body2) || this.separateY(body1, body2))
this._result = (this.separateX(body1, body2) || this.separateY(body1, body2));
if (this._result)
{
body1.postUpdate();
body2.postUpdate();
this._result = true;
}
},
@@ -496,12 +497,21 @@ Phaser.Physics.Arcade.prototype = {
// Then adjust their positions and velocities accordingly (if there was any overlap)
if (this._overlap != 0)
{
body1.overlapX = this._overlap;
body2.overlapX = this._overlap;
if (body1.customSeparateX || body2.customSeparateX)
{
return true;
}
this._velocity1 = body1.velocity.x;
this._velocity2 = body2.velocity.x;
if (!body1.immovable && !body2.immovable)
{
this._overlap *= 0.5;
body1.x = body1.x - this._overlap;
body2.x += this._overlap;
@@ -510,6 +520,7 @@ Phaser.Physics.Arcade.prototype = {
this._average = (this._newVelocity1 + this._newVelocity2) * 0.5;
this._newVelocity1 -= this._average;
this._newVelocity2 -= this._average;
body1.velocity.x = this._average + this._newVelocity1 * body1.bounce.x;
body2.velocity.x = this._average + this._newVelocity2 * body2.bounce.x;
}
@@ -595,12 +606,21 @@ Phaser.Physics.Arcade.prototype = {
// Then adjust their positions and velocities accordingly (if there was any overlap)
if (this._overlap != 0)
{
body1.overlapY = this._overlap;
body2.overlapY = this._overlap;
if (body1.customSeparateY || body2.customSeparateY)
{
return true;
}
this._velocity1 = body1.velocity.y;
this._velocity2 = body2.velocity.y;
if (!body1.immovable && !body2.immovable)
{
this._overlap *= 0.5;
body1.y = body1.y - this._overlap;
body2.y += this._overlap;
@@ -609,6 +629,7 @@ Phaser.Physics.Arcade.prototype = {
this._average = (this._newVelocity1 + this._newVelocity2) * 0.5;
this._newVelocity1 -= this._average;
this._newVelocity2 -= this._average;
body1.velocity.y = this._average + this._newVelocity1 * body1.bounce.y;
body2.velocity.y = this._average + this._newVelocity2 * body2.bounce.y;
}
@@ -616,6 +637,7 @@ Phaser.Physics.Arcade.prototype = {
{
body1.y = body1.y - this._overlap;
body1.velocity.y = this._velocity2 - this._velocity1 * body1.bounce.y;
// This is special case code that handles things like horizontal moving platforms you can ride
if (body2.active && body2.moves && (body1.deltaY() > body2.deltaY()))
{
@@ -626,6 +648,7 @@ Phaser.Physics.Arcade.prototype = {
{
body2.y += this._overlap;
body2.velocity.y = this._velocity1 - this._velocity2 * body2.bounce.y;
// This is special case code that handles things like horizontal moving platforms you can ride
if (body1.sprite.active && body1.moves && (body1.deltaY() < body2.deltaY()))
{
+11
View File
@@ -49,6 +49,17 @@ Phaser.Physics.Arcade.Body = function (sprite) {
this.allowRotation = true;
this.allowGravity = true;
// These two flags allow you to disable the custom separation that takes place
// Used in combination with your own collision processHandler you can create whatever
// type of collision response you need.
this.customSeparateX = false;
this.customSeparateY = false;
// When this body collides with another the amount of overlap is stored in here
// These values are useful if you want to provide your own custom separation logic.
this.overlapX = 0;
this.overlapY = 0;
this.collideWorldBounds = false;
this.lastX = sprite.x;