0
0
Fork 0
mirror of https://github.com/liabru/matter-js.git synced 2024-11-23 09:26:51 -05:00

fix v8 optimisation issues

This commit is contained in:
liabru 2016-11-23 11:04:49 +00:00
parent 422c1e4dff
commit 86c4a61279
4 changed files with 37 additions and 29 deletions

View file

@ -214,7 +214,7 @@ var Common = require('../core/Common');
* @return {string} bucket id * @return {string} bucket id
*/ */
var _getBucketId = function(column, row) { var _getBucketId = function(column, row) {
return column + ',' + row; return 'C' + column + 'R' + row;
}; };
/** /**

View file

@ -117,9 +117,9 @@ var Contact = require('./Contact');
*/ */
Pair.id = function(bodyA, bodyB) { Pair.id = function(bodyA, bodyB) {
if (bodyA.id < bodyB.id) { if (bodyA.id < bodyB.id) {
return bodyA.id + '_' + bodyB.id; return 'A' + bodyA.id + 'B' + bodyB.id;
} else { } else {
return bodyB.id + '_' + bodyA.id; return 'A' + bodyB.id + 'B' + bodyA.id;
} }
}; };

View file

@ -28,10 +28,9 @@ var Vector = require('../geometry/Vector');
overlapBA, overlapBA,
minOverlap, minOverlap,
collision, collision,
prevCol = previousCollision,
canReusePrevCol = false; canReusePrevCol = false;
if (prevCol) { if (previousCollision) {
// estimate total motion // estimate total motion
var parentA = bodyA.parent, var parentA = bodyA.parent,
parentB = bodyB.parent, parentB = bodyB.parent,
@ -40,20 +39,20 @@ var Vector = require('../geometry/Vector');
// we may be able to (partially) reuse collision result // we may be able to (partially) reuse collision result
// but only safe if collision was resting // but only safe if collision was resting
canReusePrevCol = prevCol && prevCol.collided && motion < 0.2; canReusePrevCol = previousCollision && previousCollision.collided && motion < 0.2;
// reuse collision object // reuse collision object
collision = prevCol; collision = previousCollision;
} else { } else {
collision = { collided: false, bodyA: bodyA, bodyB: bodyB }; collision = { collided: false, bodyA: bodyA, bodyB: bodyB };
} }
if (prevCol && canReusePrevCol) { if (previousCollision && canReusePrevCol) {
// if we can reuse the collision result // if we can reuse the collision result
// we only need to test the previously found axis // we only need to test the previously found axis
var axisBodyA = collision.axisBody, var axisBodyA = collision.axisBody,
axisBodyB = axisBodyA === bodyA ? bodyB : bodyA, axisBodyB = axisBodyA === bodyA ? bodyB : bodyA,
axes = [axisBodyA.axes[prevCol.axisNumber]]; axes = [axisBodyA.axes[previousCollision.axisNumber]];
minOverlap = _overlapAxes(axisBodyA.vertices, axisBodyB.vertices, axes); minOverlap = _overlapAxes(axisBodyA.vertices, axisBodyB.vertices, axes);
collision.reused = true; collision.reused = true;
@ -94,7 +93,6 @@ var Vector = require('../geometry/Vector');
collision.bodyA = bodyA.id < bodyB.id ? bodyA : bodyB; collision.bodyA = bodyA.id < bodyB.id ? bodyA : bodyB;
collision.bodyB = bodyA.id < bodyB.id ? bodyB : bodyA; collision.bodyB = bodyA.id < bodyB.id ? bodyB : bodyA;
collision.collided = true; collision.collided = true;
collision.normal = minOverlap.axis;
collision.depth = minOverlap.overlap; collision.depth = minOverlap.overlap;
collision.parentA = collision.bodyA.parent; collision.parentA = collision.bodyA.parent;
collision.parentB = collision.bodyB.parent; collision.parentB = collision.bodyB.parent;
@ -103,20 +101,27 @@ var Vector = require('../geometry/Vector');
bodyB = collision.bodyB; bodyB = collision.bodyB;
// ensure normal is facing away from bodyA // ensure normal is facing away from bodyA
if (Vector.dot(collision.normal, Vector.sub(bodyB.position, bodyA.position)) > 0) if (Vector.dot(minOverlap.axis, Vector.sub(bodyB.position, bodyA.position)) < 0) {
collision.normal = Vector.neg(collision.normal); 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.tangent = Vector.perp(collision.normal);
collision.penetration = { collision.penetration = collision.penetration || {};
x: collision.normal.x * collision.depth, collision.penetration.x = collision.normal.x * collision.depth;
y: collision.normal.y * collision.depth collision.penetration.y = collision.normal.y * collision.depth;
};
// find support points, there is always either exactly one or two // find support points, there is always either exactly one or two
var verticesB = _findSupports(bodyA, bodyB, collision.normal), var verticesB = _findSupports(bodyA, bodyB, collision.normal),
supports = collision.supports || []; supports = [];
supports.length = 0;
// find the supports from bodyB that are inside bodyA // find the supports from bodyB that are inside bodyA
if (Vertices.contains(bodyA.vertices, verticesB[0])) if (Vertices.contains(bodyA.vertices, verticesB[0]))

View file

@ -33,10 +33,8 @@ module.exports = Common;
deepClone = true; deepClone = true;
} }
args = Array.prototype.slice.call(arguments, argsStart); for (var i = argsStart; i < arguments.length; i++) {
var source = arguments[i];
for (var i = 0; i < args.length; i++) {
var source = args[i];
if (source) { if (source) {
for (var prop in 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. * @return {function} A new function that calls the passed functions in order.
*/ */
Common.chain = function() { Common.chain = function() {
var args = Array.prototype.slice.call(arguments), var funcs = [];
funcs = [];
for (var i = 0; i < args.length; i += 1) { for (var i = 0; i < arguments.length; i += 1) {
var func = args[i]; var func = arguments[i];
if (func._chained) { if (func._chained) {
// flatten already chained functions // flatten already chained functions
@ -508,10 +505,16 @@ module.exports = Common;
} }
var chain = function() { 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) { for (var i = 0, l = arguments.length; i < l; i++) {
var result = funcs[i].apply(lastResult, arguments); args[i] = arguments[i];
}
for (i = 0; i < funcs.length; i += 1) {
var result = funcs[i].apply(lastResult, args);
if (typeof result !== 'undefined') { if (typeof result !== 'undefined') {
lastResult = result; lastResult = result;