diff --git a/src/collision/Grid.js b/src/collision/Grid.js index bd1b30c..317eea2 100644 --- a/src/collision/Grid.js +++ b/src/collision/Grid.js @@ -214,7 +214,7 @@ var Common = require('../core/Common'); * @return {string} bucket id */ var _getBucketId = function(column, row) { - return column + ',' + row; + return 'C' + column + 'R' + row; }; /** diff --git a/src/collision/Pair.js b/src/collision/Pair.js index ac856b9..7f654b5 100644 --- a/src/collision/Pair.js +++ b/src/collision/Pair.js @@ -117,9 +117,9 @@ var Contact = require('./Contact'); */ Pair.id = function(bodyA, bodyB) { if (bodyA.id < bodyB.id) { - return bodyA.id + '_' + bodyB.id; + return 'A' + bodyA.id + 'B' + bodyB.id; } else { - return bodyB.id + '_' + bodyA.id; + return 'A' + bodyB.id + 'B' + bodyA.id; } }; diff --git a/src/collision/SAT.js b/src/collision/SAT.js index 54374cc..5c3d669 100644 --- a/src/collision/SAT.js +++ b/src/collision/SAT.js @@ -28,10 +28,9 @@ var Vector = require('../geometry/Vector'); overlapBA, minOverlap, collision, - prevCol = previousCollision, canReusePrevCol = false; - if (prevCol) { + if (previousCollision) { // estimate total motion var parentA = bodyA.parent, parentB = bodyB.parent, @@ -40,20 +39,20 @@ var Vector = require('../geometry/Vector'); // we may be able to (partially) reuse collision result // but only safe if collision was resting - canReusePrevCol = prevCol && prevCol.collided && motion < 0.2; + canReusePrevCol = previousCollision && previousCollision.collided && motion < 0.2; // reuse collision object - collision = prevCol; + collision = previousCollision; } else { collision = { collided: false, bodyA: bodyA, bodyB: bodyB }; } - if (prevCol && canReusePrevCol) { + if (previousCollision && canReusePrevCol) { // if we can reuse the collision result // we only need to test the previously found axis var axisBodyA = collision.axisBody, axisBodyB = axisBodyA === bodyA ? bodyB : bodyA, - axes = [axisBodyA.axes[prevCol.axisNumber]]; + axes = [axisBodyA.axes[previousCollision.axisNumber]]; minOverlap = _overlapAxes(axisBodyA.vertices, axisBodyB.vertices, axes); collision.reused = true; @@ -94,7 +93,6 @@ var Vector = require('../geometry/Vector'); collision.bodyA = bodyA.id < bodyB.id ? bodyA : bodyB; collision.bodyB = bodyA.id < bodyB.id ? bodyB : bodyA; collision.collided = true; - collision.normal = minOverlap.axis; collision.depth = minOverlap.overlap; collision.parentA = collision.bodyA.parent; collision.parentB = collision.bodyB.parent; @@ -103,20 +101,27 @@ var Vector = require('../geometry/Vector'); bodyB = collision.bodyB; // ensure normal is facing away from bodyA - if (Vector.dot(collision.normal, Vector.sub(bodyB.position, bodyA.position)) > 0) - collision.normal = Vector.neg(collision.normal); + if (Vector.dot(minOverlap.axis, Vector.sub(bodyB.position, bodyA.position)) < 0) { + collision.normal = { + x: minOverlap.axis.x, + y: minOverlap.axis.y + }; + } else { + collision.normal = { + x: -minOverlap.axis.x, + y: -minOverlap.axis.y + }; + } collision.tangent = Vector.perp(collision.normal); - collision.penetration = { - x: collision.normal.x * collision.depth, - y: collision.normal.y * collision.depth - }; + collision.penetration = collision.penetration || {}; + collision.penetration.x = collision.normal.x * collision.depth; + collision.penetration.y = collision.normal.y * collision.depth; // find support points, there is always either exactly one or two var verticesB = _findSupports(bodyA, bodyB, collision.normal), - supports = collision.supports || []; - supports.length = 0; + supports = []; // find the supports from bodyB that are inside bodyA if (Vertices.contains(bodyA.vertices, verticesB[0])) diff --git a/src/core/Common.js b/src/core/Common.js index a740f9a..0a02679 100644 --- a/src/core/Common.js +++ b/src/core/Common.js @@ -33,10 +33,8 @@ module.exports = Common; deepClone = true; } - args = Array.prototype.slice.call(arguments, argsStart); - - for (var i = 0; i < args.length; i++) { - var source = args[i]; + for (var i = argsStart; i < arguments.length; i++) { + var source = arguments[i]; if (source) { for (var prop in source) { @@ -493,11 +491,10 @@ module.exports = Common; * @return {function} A new function that calls the passed functions in order. */ Common.chain = function() { - var args = Array.prototype.slice.call(arguments), - funcs = []; + var funcs = []; - for (var i = 0; i < args.length; i += 1) { - var func = args[i]; + for (var i = 0; i < arguments.length; i += 1) { + var func = arguments[i]; if (func._chained) { // flatten already chained functions @@ -508,10 +505,16 @@ module.exports = Common; } var chain = function() { - var lastResult; + // https://github.com/GoogleChrome/devtools-docs/issues/53#issuecomment-51941358 + var lastResult, + args = new Array(arguments.length); - for (var i = 0; i < funcs.length; i += 1) { - var result = funcs[i].apply(lastResult, arguments); + for (var i = 0, l = arguments.length; i < l; i++) { + args[i] = arguments[i]; + } + + for (i = 0; i < funcs.length; i += 1) { + var result = funcs[i].apply(lastResult, args); if (typeof result !== 'undefined') { lastResult = result;