2013-09-03 14:35:40 +00:00
|
|
|
Phaser.Physics = {};
|
|
|
|
|
|
|
|
Phaser.Physics.Arcade = function (game) {
|
|
|
|
|
|
|
|
this.game = game;
|
|
|
|
|
2013-09-04 00:10:01 +00:00
|
|
|
this.gravity = new Phaser.Point;
|
|
|
|
this.bounds = new Phaser.Rectangle(0, 0, game.world.width, game.world.height);
|
2013-09-03 14:35:40 +00:00
|
|
|
|
|
|
|
/**
|
2013-09-04 02:48:15 +00:00
|
|
|
* Used by the QuadTree to set the maximum number of objects
|
2013-09-03 14:35:40 +00:00
|
|
|
* @type {number}
|
|
|
|
*/
|
2013-09-04 02:48:15 +00:00
|
|
|
this.maxObjects = 10;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used by the QuadTree to set the maximum number of levels
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
this.maxLevels = 4;
|
2013-09-03 14:35:40 +00:00
|
|
|
|
2013-09-03 16:28:12 +00:00
|
|
|
this.NONE = 0;
|
2013-09-04 12:54:55 +00:00
|
|
|
this.UP = 1;
|
|
|
|
this.DOWN = 2;
|
|
|
|
this.LEFT = 3;
|
|
|
|
this.RIGHT = 4;
|
|
|
|
this.CEILING = 1;
|
|
|
|
this.FLOOR = 2;
|
|
|
|
this.WALL = 5;
|
|
|
|
this.ANY = 6;
|
2013-09-04 00:10:01 +00:00
|
|
|
|
2013-09-03 18:34:38 +00:00
|
|
|
this.OVERLAP_BIAS = 4;
|
|
|
|
this.TILE_OVERLAP = false;
|
|
|
|
|
2013-09-04 12:54:55 +00:00
|
|
|
this.quadTree = null;
|
|
|
|
this.quadTreeID = 0;
|
2013-09-04 02:48:15 +00:00
|
|
|
|
2013-09-04 00:10:01 +00:00
|
|
|
// avoid gc spikes by caching these values for re-use
|
2013-09-03 18:34:38 +00:00
|
|
|
this._obj1Bounds = new Phaser.Rectangle;
|
|
|
|
this._obj2Bounds = new Phaser.Rectangle;
|
|
|
|
this._overlap = 0;
|
|
|
|
this._maxOverlap = 0;
|
|
|
|
this._obj1Velocity = 0;
|
|
|
|
this._obj2Velocity = 0;
|
|
|
|
this._obj1NewVelocity = 0;
|
|
|
|
this._obj2NewVelocity = 0;
|
|
|
|
this._average = 0;
|
2013-09-03 16:28:12 +00:00
|
|
|
|
2013-09-03 14:35:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Phaser.Physics.Arcade.prototype = {
|
|
|
|
|
2013-09-03 16:28:12 +00:00
|
|
|
collide: function (objectOrGroup1, objectOrGroup2, notifyCallback) {
|
|
|
|
|
|
|
|
return this.overlap(objectOrGroup1, objectOrGroup2, notifyCallback, this.separate);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-09-03 14:35:40 +00:00
|
|
|
updateMotion: function (body) {
|
|
|
|
|
2013-09-04 00:10:01 +00:00
|
|
|
// Rotation
|
|
|
|
this._velocityDelta = (this.computeVelocity(0, false, body.angularVelocity, body.angularAcceleration, body.angularDrag, body.maxAngular) - body.angularVelocity) / 2;
|
2013-09-03 14:35:40 +00:00
|
|
|
body.angularVelocity += this._velocityDelta;
|
2013-09-04 00:10:01 +00:00
|
|
|
body.rotation += body.angularVelocity * this.game.time.physicsElapsed;
|
2013-09-03 14:35:40 +00:00
|
|
|
|
2013-09-04 00:10:01 +00:00
|
|
|
// Horizontal
|
|
|
|
this._velocityDelta = (this.computeVelocity(1, body, body.velocity.x, body.acceleration.x, body.drag.x) - body.velocity.x) / 2;
|
2013-09-03 14:35:40 +00:00
|
|
|
body.velocity.x += this._velocityDelta;
|
2013-09-04 00:10:01 +00:00
|
|
|
this._delta = body.velocity.x * this.game.time.physicsElapsed;
|
2013-09-03 18:34:38 +00:00
|
|
|
body.x += this._delta;
|
2013-09-03 14:35:40 +00:00
|
|
|
|
2013-09-04 00:10:01 +00:00
|
|
|
// Vertical
|
|
|
|
this._velocityDelta = (this.computeVelocity(2, body, body.velocity.y, body.acceleration.y, body.drag.y) - body.velocity.y) / 2;
|
2013-09-03 14:35:40 +00:00
|
|
|
body.velocity.y += this._velocityDelta;
|
2013-09-03 16:07:05 +00:00
|
|
|
this._delta = body.velocity.y * this.game.time.physicsElapsed;
|
2013-09-03 18:34:38 +00:00
|
|
|
body.y += this._delta;
|
2013-09-03 14:35:40 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A tween-like function that takes a starting velocity and some other factors and returns an altered velocity.
|
|
|
|
*
|
|
|
|
* @param {number} Velocity Any component of velocity (e.g. 20).
|
|
|
|
* @param {number} Acceleration Rate at which the velocity is changing.
|
|
|
|
* @param {number} Drag Really kind of a deceleration, this is how much the velocity changes if Acceleration is not set.
|
|
|
|
* @param {number} Max An absolute value cap for the velocity.
|
|
|
|
*
|
|
|
|
* @return {number} The altered Velocity value.
|
|
|
|
*/
|
2013-09-04 00:10:01 +00:00
|
|
|
computeVelocity: function (axis, body, velocity, acceleration, drag, max) {
|
2013-09-03 14:35:40 +00:00
|
|
|
|
|
|
|
max = max || 10000;
|
|
|
|
|
2013-09-04 00:10:01 +00:00
|
|
|
if (axis == 1 && body.allowGravity)
|
|
|
|
{
|
|
|
|
velocity += this.gravity.x + body.gravity.x;
|
|
|
|
}
|
|
|
|
else if (axis == 2 && body.allowGravity)
|
|
|
|
{
|
|
|
|
velocity += this.gravity.y + body.gravity.y;
|
|
|
|
}
|
|
|
|
|
2013-09-03 14:35:40 +00:00
|
|
|
if (acceleration !== 0)
|
|
|
|
{
|
2013-09-04 00:10:01 +00:00
|
|
|
velocity += acceleration * this.game.time.physicsElapsed;
|
2013-09-03 14:35:40 +00:00
|
|
|
}
|
|
|
|
else if (drag !== 0)
|
|
|
|
{
|
2013-09-03 16:07:05 +00:00
|
|
|
this._drag = drag * this.game.time.physicsElapsed;
|
2013-09-03 14:35:40 +00:00
|
|
|
|
|
|
|
if (velocity - this._drag > 0)
|
|
|
|
{
|
|
|
|
velocity = velocity - this._drag;
|
|
|
|
}
|
|
|
|
else if (velocity + this._drag < 0)
|
|
|
|
{
|
|
|
|
velocity += this._drag;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
velocity = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-04 00:10:01 +00:00
|
|
|
if (velocity != 0)
|
2013-09-03 14:35:40 +00:00
|
|
|
{
|
|
|
|
if (velocity > max)
|
|
|
|
{
|
|
|
|
velocity = max;
|
|
|
|
}
|
|
|
|
else if (velocity < -max)
|
|
|
|
{
|
|
|
|
velocity = -max;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return velocity;
|
|
|
|
|
2013-09-04 02:48:15 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
preUpdate: function () {
|
|
|
|
|
|
|
|
// Create our tree which all of the Physics bodies will add themselves to
|
2013-09-04 12:54:55 +00:00
|
|
|
this.quadTreeID = 0;
|
|
|
|
this.quadTree = new Phaser.QuadTree(this, this.game.world.bounds.x, this.game.world.bounds.y, this.game.world.bounds.width, this.game.world.bounds.height, this.maxObjects, this.maxLevels);
|
2013-09-04 02:48:15 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
postUpdate: function () {
|
|
|
|
|
|
|
|
// Clear the tree ready for the next update
|
|
|
|
this.quadTree.clear();
|
|
|
|
|
2013-09-03 14:35:40 +00:00
|
|
|
},
|
|
|
|
|
2013-09-03 16:28:12 +00:00
|
|
|
/**
|
|
|
|
* Checks for overlaps between two objects using the world QuadTree. Can be GameObject vs. GameObject, GameObject vs. Group or Group vs. Group.
|
|
|
|
* Note: Does not take the objects scrollFactor into account. All overlaps are check in world space.
|
|
|
|
* @param object1 The first GameObject or Group to check. If null the world.group is used.
|
|
|
|
* @param object2 The second GameObject or Group to check.
|
|
|
|
* @param notifyCallback A callback function that is called if the objects overlap. The two objects will be passed to this function in the same order in which you passed them to Collision.overlap.
|
|
|
|
* @param processCallback A callback function that lets you perform additional checks against the two objects if they overlap. If this is set then notifyCallback will only be called if processCallback returns true.
|
|
|
|
* @returns {boolean} true if the objects overlap, otherwise false.
|
|
|
|
*/
|
|
|
|
overlap: function (object1, object2, notifyCallback, processCallback) {
|
|
|
|
|
2013-09-04 12:54:55 +00:00
|
|
|
object2 = object2 || null;
|
|
|
|
notifyCallback = notifyCallback || null;
|
|
|
|
processCallback = processCallback || this.separate;
|
|
|
|
|
|
|
|
// Get the ships top-most ID. If the length of that ID is 1 then we can ignore every other result,
|
|
|
|
// it's simply not colliding with anything :)
|
|
|
|
var potentials = this.quadTree.retrieve(object1);
|
|
|
|
var output = [];
|
|
|
|
|
|
|
|
for (var i = 0, len = potentials.length; i < len; i++)
|
|
|
|
{
|
|
|
|
if (this.separate(object1.body, potentials[i]).body)
|
|
|
|
{
|
|
|
|
output.push(potentials[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (output.length > 0)
|
|
|
|
{
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
2013-09-03 16:28:12 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The core Collision separation function used by Collision.overlap.
|
|
|
|
* @param object1 The first GameObject to separate
|
|
|
|
* @param object2 The second GameObject to separate
|
|
|
|
* @returns {boolean} Returns true if the objects were separated, otherwise false.
|
|
|
|
*/
|
|
|
|
separate: function (object1, object2) {
|
|
|
|
|
2013-09-04 00:10:01 +00:00
|
|
|
return this.separateX(object1, object2) || this.separateY(object1, object2)
|
2013-09-03 16:28:12 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Separates the two objects on their x axis
|
|
|
|
* @param object1 The first GameObject to separate
|
|
|
|
* @param object2 The second GameObject to separate
|
|
|
|
* @returns {boolean} Whether the objects in fact touched and were separated along the X axis.
|
|
|
|
*/
|
|
|
|
separateX: function (object1, object2) {
|
|
|
|
|
|
|
|
// Can't separate two immovable objects
|
|
|
|
if (object1.immovable && object2.immovable)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// First, get the two object deltas
|
2013-09-03 18:34:38 +00:00
|
|
|
this._overlap = 0;
|
2013-09-03 16:28:12 +00:00
|
|
|
|
2013-09-03 18:34:38 +00:00
|
|
|
if (object1.deltaX() != object2.deltaX())
|
2013-09-03 16:28:12 +00:00
|
|
|
{
|
|
|
|
// Check if the X hulls actually overlap
|
|
|
|
|
2013-09-03 18:34:38 +00:00
|
|
|
this._obj1Bounds.setTo(object1.x - ((object1.deltaX() > 0) ? object1.deltaX() : 0), object1.lastY, object1.width + ((object1.deltaX() > 0) ? object1.deltaX() : -object1.deltaX()), object1.height);
|
|
|
|
this._obj2Bounds.setTo(object2.x - ((object2.deltaX() > 0) ? object2.deltaX() : 0), object2.lastY, object2.width + ((object2.deltaX() > 0) ? object2.deltaX() : -object2.deltaX()), object2.height);
|
|
|
|
|
2013-09-04 00:10:01 +00:00
|
|
|
if ((this._obj1Bounds.right > this._obj2Bounds.x) && (this._obj1Bounds.x < this._obj2Bounds.right) && (this._obj1Bounds.bottom > this._obj2Bounds.y) && (this._obj1Bounds.y < this._obj2Bounds.bottom))
|
2013-09-03 16:28:12 +00:00
|
|
|
{
|
2013-09-03 18:34:38 +00:00
|
|
|
this._maxOverlap = object1.deltaAbsX() + object2.deltaAbsX() + this.OVERLAP_BIAS;
|
2013-09-03 16:28:12 +00:00
|
|
|
|
|
|
|
// If they did overlap (and can), figure out by how much and flip the corresponding flags
|
2013-09-04 12:54:55 +00:00
|
|
|
if (object1.deltaX() > object2.deltaX())
|
2013-09-03 16:28:12 +00:00
|
|
|
{
|
2013-09-03 18:34:38 +00:00
|
|
|
this._overlap = object1.x + object1.width - object2.x;
|
2013-09-03 16:28:12 +00:00
|
|
|
|
2013-09-04 12:54:55 +00:00
|
|
|
if ((this._overlap > this._maxOverlap) || object1.allowCollision.right == false || object2.allowCollision.left == false)
|
2013-09-03 16:28:12 +00:00
|
|
|
{
|
2013-09-03 18:34:38 +00:00
|
|
|
this._overlap = 0;
|
2013-09-03 16:28:12 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-09-04 12:54:55 +00:00
|
|
|
object1.touching.right = true;
|
|
|
|
object2.touching.left = true;
|
2013-09-03 16:28:12 +00:00
|
|
|
}
|
|
|
|
}
|
2013-09-03 18:34:38 +00:00
|
|
|
else if (object1.deltaX() < object2.deltaX())
|
2013-09-03 16:28:12 +00:00
|
|
|
{
|
2013-09-03 18:34:38 +00:00
|
|
|
this._overlap = object1.x - object2.width - object2.x;
|
2013-09-03 16:28:12 +00:00
|
|
|
|
2013-09-04 12:54:55 +00:00
|
|
|
if ((-this._overlap > this._maxOverlap) || object1.allowCollision.left == false || object2.allowCollision.right == false)
|
2013-09-03 16:28:12 +00:00
|
|
|
{
|
2013-09-03 18:34:38 +00:00
|
|
|
this._overlap = 0;
|
2013-09-03 16:28:12 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-09-04 12:54:55 +00:00
|
|
|
object1.touching.left = true;
|
|
|
|
object2.touching.right = true;
|
2013-09-03 16:28:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Then adjust their positions and velocities accordingly (if there was any overlap)
|
2013-09-03 18:34:38 +00:00
|
|
|
if (this._overlap != 0)
|
2013-09-03 16:28:12 +00:00
|
|
|
{
|
2013-09-03 18:34:38 +00:00
|
|
|
this._obj1Velocity = object1.velocity.x;
|
|
|
|
this._obj2Velocity = object2.velocity.x;
|
2013-09-03 16:28:12 +00:00
|
|
|
|
|
|
|
if (!object1.immovable && !object2.immovable)
|
|
|
|
{
|
2013-09-03 18:34:38 +00:00
|
|
|
this._overlap *= 0.5;
|
|
|
|
object1.x = object1.x - this._overlap;
|
|
|
|
object2.x += this._overlap;
|
|
|
|
|
|
|
|
this._obj1NewVelocity = Math.sqrt((this._obj2Velocity * this._obj2Velocity * object2.mass) / object1.mass) * ((this._obj2Velocity > 0) ? 1 : -1);
|
|
|
|
this._obj2NewVelocity = Math.sqrt((this._obj1Velocity * this._obj1Velocity * object1.mass) / object2.mass) * ((this._obj1Velocity > 0) ? 1 : -1);
|
|
|
|
this._average = (this._obj1NewVelocity + this._obj2NewVelocity) * 0.5;
|
|
|
|
this._obj1NewVelocity -= this._average;
|
|
|
|
this._obj2NewVelocity -= this._average;
|
|
|
|
object1.velocity.x = this._average + this._obj1NewVelocity * object1.bounce.x;
|
|
|
|
object2.velocity.x = this._average + this._obj2NewVelocity * object2.bounce.x;
|
2013-09-03 16:28:12 +00:00
|
|
|
}
|
|
|
|
else if (!object1.immovable)
|
|
|
|
{
|
2013-09-03 18:34:38 +00:00
|
|
|
object1.x = object1.x - this._overlap;
|
|
|
|
object1.velocity.x = this._obj2Velocity - this._obj1Velocity * object1.bounce.x;
|
2013-09-03 16:28:12 +00:00
|
|
|
}
|
|
|
|
else if (!object2.immovable)
|
|
|
|
{
|
2013-09-03 18:34:38 +00:00
|
|
|
object2.x += this._overlap;
|
|
|
|
object2.velocity.x = this._obj1Velocity - this._obj2Velocity * object2.bounce.x;
|
2013-09-03 16:28:12 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Separates the two objects on their y axis
|
|
|
|
* @param object1 The first GameObject to separate
|
|
|
|
* @param object2 The second GameObject to separate
|
|
|
|
* @returns {boolean} Whether the objects in fact touched and were separated along the Y axis.
|
|
|
|
*/
|
|
|
|
separateY: function (object1, object2) {
|
|
|
|
|
|
|
|
// Can't separate two immovable objects
|
2013-09-04 00:10:01 +00:00
|
|
|
if (object1.immovable && object2.immovable)
|
|
|
|
{
|
2013-09-03 16:28:12 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// First, get the two object deltas
|
2013-09-04 00:10:01 +00:00
|
|
|
this._overlap = 0;
|
2013-09-03 16:28:12 +00:00
|
|
|
|
2013-09-04 00:10:01 +00:00
|
|
|
if (object1.deltaY() != object2.deltaY())
|
2013-09-03 16:28:12 +00:00
|
|
|
{
|
|
|
|
// Check if the Y hulls actually overlap
|
2013-09-04 00:10:01 +00:00
|
|
|
this._obj1Bounds.setTo(object1.x, object1.y - ((object1.deltaY() > 0) ? object1.deltaY() : 0), object1.width, object1.height + object1.deltaAbsY());
|
|
|
|
this._obj2Bounds.setTo(object2.x, object2.y - ((object2.deltaY() > 0) ? object2.deltaY() : 0), object2.width, object2.height + object2.deltaAbsY());
|
2013-09-03 16:28:12 +00:00
|
|
|
|
2013-09-04 00:10:01 +00:00
|
|
|
if ((this._obj1Bounds.right > this._obj2Bounds.x) && (this._obj1Bounds.x < this._obj2Bounds.right) && (this._obj1Bounds.bottom > this._obj2Bounds.y) && (this._obj1Bounds.y < this._obj2Bounds.bottom))
|
2013-09-03 16:28:12 +00:00
|
|
|
{
|
2013-09-04 00:10:01 +00:00
|
|
|
this._maxOverlap = object1.deltaAbsY() + object2.deltaAbsY() + this.OVERLAP_BIAS;
|
2013-09-03 16:28:12 +00:00
|
|
|
|
|
|
|
// If they did overlap (and can), figure out by how much and flip the corresponding flags
|
2013-09-04 00:10:01 +00:00
|
|
|
if (object1.deltaY() > object2.deltaY())
|
2013-09-03 16:28:12 +00:00
|
|
|
{
|
2013-09-04 00:10:01 +00:00
|
|
|
this._overlap = object1.y + object1.height - object2.y;
|
2013-09-03 16:28:12 +00:00
|
|
|
|
2013-09-04 12:54:55 +00:00
|
|
|
if ((this._overlap > this._maxOverlap) || object1.allowCollision.down == false || object2.allowCollision.up == false)
|
2013-09-03 16:28:12 +00:00
|
|
|
{
|
2013-09-04 00:10:01 +00:00
|
|
|
this._overlap = 0;
|
2013-09-03 16:28:12 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-09-04 12:54:55 +00:00
|
|
|
object1.touching.down = true;
|
|
|
|
object2.touching.up = true;
|
2013-09-03 16:28:12 +00:00
|
|
|
}
|
|
|
|
}
|
2013-09-04 00:10:01 +00:00
|
|
|
else if (object1.deltaY() < object2.deltaY())
|
2013-09-03 16:28:12 +00:00
|
|
|
{
|
2013-09-04 00:10:01 +00:00
|
|
|
this._overlap = object1.y - object2.height - object2.y;
|
2013-09-03 16:28:12 +00:00
|
|
|
|
2013-09-04 12:54:55 +00:00
|
|
|
if ((-this._overlap > this._maxOverlap) || object1.allowCollision.up == false || object2.allowCollision.down == false)
|
2013-09-03 16:28:12 +00:00
|
|
|
{
|
2013-09-04 00:10:01 +00:00
|
|
|
this._overlap = 0;
|
2013-09-03 16:28:12 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-09-04 12:54:55 +00:00
|
|
|
object1.touching.up = true;
|
|
|
|
object2.touching.down = true;
|
2013-09-03 16:28:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Then adjust their positions and velocities accordingly (if there was any overlap)
|
2013-09-04 00:10:01 +00:00
|
|
|
if (this._overlap != 0)
|
2013-09-03 16:28:12 +00:00
|
|
|
{
|
2013-09-03 18:34:38 +00:00
|
|
|
this._obj1Velocity = object1.velocity.y;
|
|
|
|
this._obj2Velocity = object2.velocity.y;
|
2013-09-03 16:28:12 +00:00
|
|
|
|
|
|
|
if (!object1.immovable && !object2.immovable)
|
|
|
|
{
|
2013-09-04 00:10:01 +00:00
|
|
|
this._overlap *= 0.5;
|
|
|
|
object1.y = object1.y - this._overlap;
|
|
|
|
object2.y += this._overlap;
|
2013-09-03 16:28:12 +00:00
|
|
|
|
2013-09-03 18:34:38 +00:00
|
|
|
this._obj1NewVelocity = Math.sqrt((this._obj2Velocity * this._obj2Velocity * object2.mass) / object1.mass) * ((this._obj2Velocity > 0) ? 1 : -1);
|
|
|
|
this._obj2NewVelocity = Math.sqrt((this._obj1Velocity * this._obj1Velocity * object1.mass) / object2.mass) * ((this._obj1Velocity > 0) ? 1 : -1);
|
|
|
|
this._average = (this._obj1NewVelocity + this._obj2NewVelocity) * 0.5;
|
|
|
|
this._obj1NewVelocity -= this._average;
|
|
|
|
this._obj2NewVelocity -= this._average;
|
2013-09-04 00:10:01 +00:00
|
|
|
object1.velocity.y = this._average + this._obj1NewVelocity * object1.bounce.y;
|
|
|
|
object2.velocity.y = this._average + this._obj2NewVelocity * object2.bounce.y;
|
2013-09-03 16:28:12 +00:00
|
|
|
}
|
|
|
|
else if (!object1.immovable)
|
|
|
|
{
|
2013-09-04 00:10:01 +00:00
|
|
|
object1.y = object1.y - this._overlap;
|
|
|
|
object1.velocity.y = this._obj2Velocity - this._obj1Velocity * object1.bounce.y;
|
2013-09-03 16:28:12 +00:00
|
|
|
// This is special case code that handles things like horizontal moving platforms you can ride
|
2013-09-04 00:10:01 +00:00
|
|
|
if (object2.active && object2.moves && (object1.deltaY() > object2.deltaY()))
|
2013-09-03 16:28:12 +00:00
|
|
|
{
|
2013-09-04 00:10:01 +00:00
|
|
|
object1.x += object2.x - object2.lastX;
|
2013-09-03 16:28:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (!object2.immovable)
|
|
|
|
{
|
2013-09-04 00:10:01 +00:00
|
|
|
object2.y += this._overlap;
|
|
|
|
object2.velocity.y = this._obj1Velocity - this._obj2Velocity * object2.bounce.y;
|
2013-09-03 16:28:12 +00:00
|
|
|
// This is special case code that handles things like horizontal moving platforms you can ride
|
2013-09-04 00:10:01 +00:00
|
|
|
if (object1.sprite.active && object1.moves && (object1.deltaY() < object2.deltaY()))
|
2013-09-03 16:28:12 +00:00
|
|
|
{
|
2013-09-04 00:10:01 +00:00
|
|
|
object2.x += object1.x - object1.lastX;
|
2013-09-03 16:28:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-09-03 14:35:40 +00:00
|
|
|
};
|