2019-11-07 17:34:59 -05:00
|
|
|
/* eslint-env es6 */
|
|
|
|
/* eslint no-global-assign: 0 */
|
|
|
|
"use strict";
|
|
|
|
|
2021-01-31 12:32:47 -05:00
|
|
|
const mock = require('mock-require');
|
2024-03-03 16:01:45 -05:00
|
|
|
const { requireUncached, serialize, smoothExp } = require('./TestTools');
|
2021-11-28 16:25:11 -05:00
|
|
|
const consoleOriginal = global.console;
|
2024-03-03 16:01:45 -05:00
|
|
|
const DateOriginal = global.Date;
|
2019-11-07 17:34:59 -05:00
|
|
|
|
2021-11-30 18:16:20 -05:00
|
|
|
const runExample = options => {
|
2021-12-22 18:33:57 -05:00
|
|
|
const {
|
|
|
|
Matter,
|
|
|
|
logs,
|
|
|
|
frameCallbacks
|
|
|
|
} = prepareEnvironment(options);
|
2021-11-28 16:39:11 -05:00
|
|
|
|
2024-03-03 16:01:45 -05:00
|
|
|
let memoryDeltaAverage = 0;
|
|
|
|
let timeDeltaAverage = 0;
|
2021-11-30 18:16:20 -05:00
|
|
|
let overlapTotal = 0;
|
|
|
|
let overlapCount = 0;
|
2021-12-20 16:17:02 -05:00
|
|
|
let i;
|
2023-08-13 08:10:09 -04:00
|
|
|
let j;
|
2021-11-28 16:39:11 -05:00
|
|
|
|
2021-12-20 16:17:02 -05:00
|
|
|
try {
|
2023-08-13 08:10:09 -04:00
|
|
|
let runner;
|
|
|
|
let engine;
|
|
|
|
let render;
|
2024-03-03 16:01:45 -05:00
|
|
|
let extrinsicCapture;
|
|
|
|
|
|
|
|
const bodyOverlap = (bodyA, bodyB) => {
|
|
|
|
const collision = Matter.Collision.collides(bodyA, bodyB);
|
|
|
|
return collision ? collision.depth : 0;
|
|
|
|
};
|
2021-12-07 18:23:34 -05:00
|
|
|
|
2023-08-13 08:10:09 -04:00
|
|
|
for (i = 0; i < options.repeats; i += 1) {
|
|
|
|
if (global.gc) {
|
|
|
|
global.gc();
|
|
|
|
}
|
2021-11-30 18:16:20 -05:00
|
|
|
|
2023-08-13 08:10:09 -04:00
|
|
|
const Examples = requireUncached('../examples/index');
|
|
|
|
const example = Examples[options.name]();
|
2021-12-22 18:33:57 -05:00
|
|
|
|
2023-08-13 08:10:09 -04:00
|
|
|
runner = example.runner;
|
|
|
|
engine = example.engine;
|
|
|
|
render = example.render;
|
2024-03-03 16:01:45 -05:00
|
|
|
|
2023-08-13 08:10:09 -04:00
|
|
|
for (j = 0; j < options.updates; j += 1) {
|
|
|
|
const time = j * runner.delta;
|
|
|
|
const callbackCount = frameCallbacks.length;
|
|
|
|
|
2024-03-03 16:01:45 -05:00
|
|
|
global.timeNow = time;
|
|
|
|
|
2023-08-13 08:10:09 -04:00
|
|
|
for (let p = 0; p < callbackCount; p += 1) {
|
2024-03-03 16:01:45 -05:00
|
|
|
const frameCallback = frameCallbacks.shift();
|
|
|
|
const memoryBefore = process.memoryUsage().heapUsed;
|
|
|
|
const timeBefore = process.hrtime();
|
2023-08-13 08:10:09 -04:00
|
|
|
|
2024-03-03 16:01:45 -05:00
|
|
|
frameCallback(time);
|
2023-08-13 08:10:09 -04:00
|
|
|
|
2024-03-03 16:01:45 -05:00
|
|
|
const timeDuration = process.hrtime(timeBefore);
|
|
|
|
const timeDelta = timeDuration[0] * 1e9 + timeDuration[1];
|
|
|
|
const memoryAfter = process.memoryUsage().heapUsed;
|
|
|
|
const memoryDelta = Math.max(memoryAfter - memoryBefore, 0);
|
|
|
|
|
|
|
|
memoryDeltaAverage = smoothExp(memoryDeltaAverage, memoryDelta);
|
|
|
|
timeDeltaAverage = smoothExp(timeDeltaAverage, timeDelta);
|
2023-08-13 08:10:09 -04:00
|
|
|
}
|
2021-11-30 18:16:20 -05:00
|
|
|
|
2024-03-03 16:01:45 -05:00
|
|
|
if (j === 1) {
|
|
|
|
const pairsList = engine.pairs.list;
|
|
|
|
const pairsListLength = engine.pairs.list.length;
|
2021-11-30 18:16:20 -05:00
|
|
|
|
2024-03-03 16:01:45 -05:00
|
|
|
for (let p = 0; p < pairsListLength; p += 1) {
|
|
|
|
const pair = pairsList[p];
|
2021-11-30 18:16:20 -05:00
|
|
|
|
2024-03-03 16:01:45 -05:00
|
|
|
if (pair.isActive && !pair.isSensor) {
|
|
|
|
overlapTotal += bodyOverlap(pair.bodyA, pair.bodyB);
|
|
|
|
overlapCount += 1;
|
|
|
|
}
|
2023-08-13 08:10:09 -04:00
|
|
|
}
|
2021-11-30 18:16:20 -05:00
|
|
|
}
|
2024-03-03 16:01:45 -05:00
|
|
|
|
|
|
|
if (!extrinsicCapture && engine.timing.timestamp >= 1000) {
|
|
|
|
extrinsicCapture = captureExtrinsics(engine, Matter);
|
|
|
|
extrinsicCapture.updates = j;
|
|
|
|
}
|
2021-11-30 18:16:20 -05:00
|
|
|
}
|
2021-12-20 16:17:02 -05:00
|
|
|
}
|
2021-12-22 18:33:57 -05:00
|
|
|
|
|
|
|
resetEnvironment();
|
|
|
|
|
|
|
|
return {
|
|
|
|
name: options.name,
|
2024-03-03 16:01:45 -05:00
|
|
|
duration: timeDeltaAverage,
|
|
|
|
memory: memoryDeltaAverage,
|
2021-12-22 18:33:57 -05:00
|
|
|
overlap: overlapTotal / (overlapCount || 1),
|
2024-03-03 16:01:45 -05:00
|
|
|
extrinsic: extrinsicCapture,
|
2021-12-22 18:33:57 -05:00
|
|
|
intrinsic: captureIntrinsics(engine, Matter),
|
2024-03-03 16:01:45 -05:00
|
|
|
state: captureState(engine, runner, render),
|
|
|
|
logs
|
2021-12-22 18:33:57 -05:00
|
|
|
};
|
|
|
|
|
2021-12-20 16:17:02 -05:00
|
|
|
} catch (err) {
|
2023-08-13 08:10:09 -04:00
|
|
|
err.message = `On example '${options.name}' update ${j}:\n\n ${err.message}`;
|
2021-12-20 16:17:02 -05:00
|
|
|
throw err;
|
2021-11-30 18:16:20 -05:00
|
|
|
}
|
|
|
|
};
|
2021-11-28 16:39:11 -05:00
|
|
|
|
2021-11-28 16:25:11 -05:00
|
|
|
const prepareMatter = (options) => {
|
|
|
|
const Matter = requireUncached(options.useDev ? '../build/matter.dev' : '../build/matter');
|
2019-11-07 17:34:59 -05:00
|
|
|
|
2021-11-28 16:25:11 -05:00
|
|
|
if (Matter.Common._nextId !== 0) {
|
|
|
|
throw 'Matter instance has already been used.';
|
|
|
|
}
|
|
|
|
|
|
|
|
Matter.Common.info = Matter.Common.warn = Matter.Common.log;
|
2021-11-30 18:16:20 -05:00
|
|
|
|
|
|
|
if (options.stableSort) {
|
2021-12-12 05:45:50 -05:00
|
|
|
if (Matter.Collision) {
|
|
|
|
const MatterCollisionCollides = Matter.Collision.collides;
|
|
|
|
Matter.Collision.collides = function(bodyA, bodyB, pairs) {
|
|
|
|
const _bodyA = bodyA.id < bodyB.id ? bodyA : bodyB;
|
|
|
|
const _bodyB = bodyA.id < bodyB.id ? bodyB : bodyA;
|
|
|
|
return MatterCollisionCollides(_bodyA, _bodyB, pairs);
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
const MatterSATCollides = Matter.SAT.collides;
|
|
|
|
Matter.SAT.collides = function(bodyA, bodyB, previousCollision, pairActive) {
|
|
|
|
const _bodyA = bodyA.id < bodyB.id ? bodyA : bodyB;
|
|
|
|
const _bodyB = bodyA.id < bodyB.id ? bodyB : bodyA;
|
|
|
|
return MatterSATCollides(_bodyA, _bodyB, previousCollision, pairActive);
|
|
|
|
};
|
|
|
|
}
|
2021-11-30 18:16:20 -05:00
|
|
|
|
|
|
|
Matter.after('Detector.collisions', function() { this.sort(collisionCompareId); });
|
|
|
|
Matter.after('Composite.allBodies', function() { sortById(this); });
|
|
|
|
Matter.after('Composite.allConstraints', function() { sortById(this); });
|
|
|
|
Matter.after('Composite.allComposites', function() { sortById(this); });
|
|
|
|
|
|
|
|
Matter.before('Pairs.update', function(pairs) {
|
|
|
|
pairs.list.sort((pairA, pairB) => collisionCompareId(pairA.collision, pairB.collision));
|
|
|
|
});
|
|
|
|
|
|
|
|
Matter.after('Pairs.update', function(pairs) {
|
|
|
|
pairs.list.sort((pairA, pairB) => collisionCompareId(pairA.collision, pairB.collision));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.jitter) {
|
|
|
|
Matter.after('Body.create', function() {
|
|
|
|
Matter.Body.applyForce(this, this.position, {
|
|
|
|
x: Math.cos(this.id * this.id) * options.jitter * this.mass,
|
|
|
|
y: Math.sin(this.id * this.id) * options.jitter * this.mass
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2021-11-28 16:25:11 -05:00
|
|
|
|
|
|
|
return Matter;
|
|
|
|
};
|
|
|
|
|
2021-12-22 18:33:57 -05:00
|
|
|
const prepareEnvironment = options => {
|
2021-11-28 16:25:11 -05:00
|
|
|
const logs = [];
|
2021-12-22 18:33:57 -05:00
|
|
|
const frameCallbacks = [];
|
|
|
|
|
|
|
|
global.document = global.window = {
|
2024-03-03 16:01:45 -05:00
|
|
|
performance: {},
|
2021-12-22 18:33:57 -05:00
|
|
|
addEventListener: () => {},
|
|
|
|
requestAnimationFrame: callback => {
|
|
|
|
frameCallbacks.push(callback);
|
|
|
|
return frameCallbacks.length;
|
|
|
|
},
|
|
|
|
createElement: () => ({
|
|
|
|
parentNode: {},
|
|
|
|
width: 800,
|
|
|
|
height: 600,
|
|
|
|
style: {},
|
|
|
|
addEventListener: () => {},
|
|
|
|
getAttribute: name => ({
|
|
|
|
'data-pixel-ratio': '1'
|
|
|
|
}[name]),
|
|
|
|
getContext: () => new Proxy({}, {
|
|
|
|
get() { return () => {}; }
|
|
|
|
})
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
global.document.body = global.document.createElement();
|
|
|
|
|
|
|
|
global.Image = function Image() { };
|
|
|
|
|
2021-01-28 19:09:27 -05:00
|
|
|
global.console = {
|
|
|
|
log: (...args) => {
|
|
|
|
logs.push(args.join(' '));
|
|
|
|
}
|
|
|
|
};
|
2019-11-07 17:34:59 -05:00
|
|
|
|
2024-03-03 16:01:45 -05:00
|
|
|
global.Math.random = () => {
|
|
|
|
throw new Error("Math.random was called during tests, output can not be compared.");
|
|
|
|
};
|
|
|
|
|
|
|
|
global.timeNow = 0;
|
|
|
|
|
|
|
|
global.window.performance.now = () => global.timeNow;
|
|
|
|
|
|
|
|
global.Date = function() {
|
|
|
|
this.toString = () => global.timeNow.toString();
|
|
|
|
this.valueOf = () => global.timeNow;
|
|
|
|
};
|
|
|
|
|
|
|
|
global.Date.now = () => global.timeNow;
|
|
|
|
|
2021-12-22 18:33:57 -05:00
|
|
|
const Matter = prepareMatter(options);
|
|
|
|
mock('matter-js', Matter);
|
|
|
|
global.Matter = Matter;
|
|
|
|
|
|
|
|
return {
|
|
|
|
Matter,
|
|
|
|
logs,
|
|
|
|
frameCallbacks
|
|
|
|
};
|
2021-11-28 16:25:11 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
const resetEnvironment = () => {
|
|
|
|
global.console = consoleOriginal;
|
2024-03-03 16:01:45 -05:00
|
|
|
global.Date = DateOriginal;
|
2021-11-28 16:25:11 -05:00
|
|
|
global.window = undefined;
|
|
|
|
global.document = undefined;
|
|
|
|
global.Matter = undefined;
|
|
|
|
mock.stopAll();
|
|
|
|
};
|
|
|
|
|
2021-11-30 18:16:20 -05:00
|
|
|
const captureExtrinsics = ({ world }, Matter) => ({
|
|
|
|
bodies: Matter.Composite.allBodies(world).reduce((bodies, body) => {
|
2024-03-03 16:01:45 -05:00
|
|
|
bodies[body.id] = {
|
|
|
|
position: { x: body.position.x, y: body.position.y },
|
|
|
|
vertices: body.vertices.map(vertex => ({ x: vertex.x, y: vertex.y }))
|
|
|
|
};
|
2021-11-28 16:39:11 -05:00
|
|
|
|
|
|
|
return bodies;
|
|
|
|
}, {})
|
|
|
|
});
|
|
|
|
|
2024-03-03 16:01:45 -05:00
|
|
|
const captureIntrinsics = ({ world, timing }, Matter) => serialize({
|
|
|
|
engine: {
|
|
|
|
timing: {
|
|
|
|
timeScale: timing.timeScale,
|
|
|
|
timestamp: timing.timestamp
|
|
|
|
}
|
|
|
|
},
|
2021-11-30 18:16:20 -05:00
|
|
|
bodies: Matter.Composite.allBodies(world).reduce((bodies, body) => {
|
2021-11-28 16:39:11 -05:00
|
|
|
bodies[body.id] = body;
|
|
|
|
return bodies;
|
|
|
|
}, {}),
|
2021-11-30 18:16:20 -05:00
|
|
|
constraints: Matter.Composite.allConstraints(world).reduce((constraints, constraint) => {
|
2021-11-28 16:39:11 -05:00
|
|
|
constraints[constraint.id] = constraint;
|
|
|
|
return constraints;
|
|
|
|
}, {}),
|
2021-11-30 18:16:20 -05:00
|
|
|
composites: Matter.Composite.allComposites(world).reduce((composites, composite) => {
|
2021-11-28 16:39:11 -05:00
|
|
|
composites[composite.id] = {
|
2021-11-30 18:16:20 -05:00
|
|
|
bodies: Matter.Composite.allBodies(composite).map(body => body.id),
|
|
|
|
constraints: Matter.Composite.allConstraints(composite).map(constraint => constraint.id),
|
|
|
|
composites: Matter.Composite.allComposites(composite).map(composite => composite.id)
|
2021-11-28 16:39:11 -05:00
|
|
|
};
|
|
|
|
return composites;
|
|
|
|
}, {})
|
2021-12-22 18:33:57 -05:00
|
|
|
}, (key) => !Number.isInteger(parseInt(key)) && !intrinsicProperties.includes(key));
|
2021-11-28 16:39:11 -05:00
|
|
|
|
2021-12-22 18:33:57 -05:00
|
|
|
const captureState = (engine, runner, render, excludeKeys=excludeStateProperties) => (
|
|
|
|
serialize({ engine, runner, render }, (key) => excludeKeys.includes(key))
|
|
|
|
);
|
2021-11-28 16:39:11 -05:00
|
|
|
|
2021-11-30 18:16:20 -05:00
|
|
|
const intrinsicProperties = [
|
2021-12-22 18:33:57 -05:00
|
|
|
// Composite
|
|
|
|
'bodies', 'constraints', 'composites',
|
|
|
|
|
2021-11-30 18:16:20 -05:00
|
|
|
// Common
|
|
|
|
'id', 'label',
|
2021-11-20 07:27:14 -05:00
|
|
|
|
2021-11-30 18:16:20 -05:00
|
|
|
// Constraint
|
|
|
|
'angularStiffness', 'bodyA', 'bodyB', 'damping', 'length', 'stiffness',
|
2019-11-07 17:34:59 -05:00
|
|
|
|
2021-11-30 18:16:20 -05:00
|
|
|
// Body
|
2021-12-22 18:33:57 -05:00
|
|
|
'area', 'collisionFilter', 'category', 'mask', 'group', 'density', 'friction',
|
|
|
|
'frictionAir', 'frictionStatic', 'inertia', 'inverseInertia', 'inverseMass',
|
|
|
|
'isSensor', 'isSleeping', 'isStatic', 'mass', 'parent', 'parts', 'restitution',
|
|
|
|
'sleepThreshold', 'slop', 'timeScale',
|
2020-03-09 16:56:06 -04:00
|
|
|
|
2021-11-30 18:16:20 -05:00
|
|
|
// Composite
|
|
|
|
'bodies', 'constraints', 'composites'
|
|
|
|
];
|
2021-11-21 12:00:53 -05:00
|
|
|
|
2021-12-22 18:33:57 -05:00
|
|
|
const extrinsicProperties = [
|
|
|
|
'axes',
|
|
|
|
'vertices',
|
|
|
|
'bounds',
|
|
|
|
'angle',
|
|
|
|
'anglePrev',
|
|
|
|
'angularVelocity',
|
|
|
|
'angularSpeed',
|
|
|
|
'speed',
|
|
|
|
'velocity',
|
|
|
|
'position',
|
|
|
|
'positionPrev',
|
2024-03-03 16:01:45 -05:00
|
|
|
'motion',
|
|
|
|
'sleepCounter',
|
|
|
|
'positionImpulse'
|
2021-12-22 18:33:57 -05:00
|
|
|
];
|
|
|
|
|
|
|
|
const excludeStateProperties = [
|
|
|
|
'cache',
|
|
|
|
'grid',
|
|
|
|
'context',
|
|
|
|
'broadphase',
|
|
|
|
'metrics',
|
|
|
|
'controller',
|
|
|
|
'detector',
|
|
|
|
'pairs',
|
|
|
|
'lastElapsed',
|
|
|
|
'deltaHistory',
|
|
|
|
'elapsedHistory',
|
|
|
|
'engineDeltaHistory',
|
|
|
|
'engineElapsedHistory',
|
|
|
|
'timestampElapsedHistory',
|
|
|
|
].concat(extrinsicProperties);
|
|
|
|
|
2021-11-30 18:16:20 -05:00
|
|
|
const collisionId = (collision) =>
|
|
|
|
Math.min(collision.bodyA.id, collision.bodyB.id) + Math.max(collision.bodyA.id, collision.bodyB.id) * 10000;
|
2020-03-09 16:56:06 -04:00
|
|
|
|
2021-11-30 18:16:20 -05:00
|
|
|
const collisionCompareId = (collisionA, collisionB) => collisionId(collisionA) - collisionId(collisionB);
|
2019-11-07 17:34:59 -05:00
|
|
|
|
2021-11-30 18:16:20 -05:00
|
|
|
const sortById = (objs) => objs.sort((objA, objB) => objA.id - objB.id);
|
2019-11-07 17:34:59 -05:00
|
|
|
|
2021-12-22 18:33:57 -05:00
|
|
|
module.exports = { runExample };
|