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
*/
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) {
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;
}
};

View file

@ -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]))

View file

@ -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;