0
0
Fork 0
mirror of https://github.com/liabru/matter-js.git synced 2024-12-25 13:39:06 -05:00

moved all private functions to module namespaces

This commit is contained in:
liabru 2017-11-26 21:59:06 +00:00
parent 2dc7a0bf5e
commit 64be5a5e3e
8 changed files with 42 additions and 42 deletions

View file

@ -399,7 +399,7 @@ var Axes = require('../geometry/Axes');
} }
// sum the properties of all compound parts of the parent body // sum the properties of all compound parts of the parent body
var total = _totalProperties(body); var total = Body._totalProperties(body);
body.area = total.area; body.area = total.area;
body.parent = body; body.parent = body;
@ -658,7 +658,7 @@ var Axes = require('../geometry/Axes');
* @param {body} body * @param {body} body
* @return {} * @return {}
*/ */
var _totalProperties = function(body) { Body._totalProperties = function(body) {
// from equations at: // from equations at:
// https://ecourses.ou.edu/cgi-bin/ebook.cgi?doc=&topic=st&chap_sec=07.2&page=theory // https://ecourses.ou.edu/cgi-bin/ebook.cgi?doc=&topic=st&chap_sec=07.2&page=theory
// http://output.to/sideway/default.asp?qno=121100087 // http://output.to/sideway/default.asp?qno=121100087

View file

@ -82,7 +82,7 @@ var Common = require('../core/Common');
|| body.bounds.max.y < world.bounds.min.y || body.bounds.min.y > world.bounds.max.y) || body.bounds.max.y < world.bounds.min.y || body.bounds.min.y > world.bounds.max.y)
continue; continue;
var newRegion = _getRegion(grid, body); var newRegion = Grid._getRegion(grid, body);
// if the body has changed grid region // if the body has changed grid region
if (!body.region || newRegion.id !== body.region.id || forceUpdate) { if (!body.region || newRegion.id !== body.region.id || forceUpdate) {
@ -94,13 +94,13 @@ var Common = require('../core/Common');
if (!body.region || forceUpdate) if (!body.region || forceUpdate)
body.region = newRegion; body.region = newRegion;
var union = _regionUnion(newRegion, body.region); var union = Grid._regionUnion(newRegion, body.region);
// update grid buckets affected by region change // update grid buckets affected by region change
// iterate over the union of both regions // iterate over the union of both regions
for (col = union.startCol; col <= union.endCol; col++) { for (col = union.startCol; col <= union.endCol; col++) {
for (row = union.startRow; row <= union.endRow; row++) { for (row = union.startRow; row <= union.endRow; row++) {
bucketId = _getBucketId(col, row); bucketId = Grid._getBucketId(col, row);
bucket = buckets[bucketId]; bucket = buckets[bucketId];
var isInsideNewRegion = (col >= newRegion.startCol && col <= newRegion.endCol var isInsideNewRegion = (col >= newRegion.startCol && col <= newRegion.endCol
@ -113,15 +113,15 @@ var Common = require('../core/Common');
if (!isInsideNewRegion && isInsideOldRegion) { if (!isInsideNewRegion && isInsideOldRegion) {
if (isInsideOldRegion) { if (isInsideOldRegion) {
if (bucket) if (bucket)
_bucketRemoveBody(grid, bucket, body); Grid._bucketRemoveBody(grid, bucket, body);
} }
} }
// add to new region buckets // add to new region buckets
if (body.region === newRegion || (isInsideNewRegion && !isInsideOldRegion) || forceUpdate) { if (body.region === newRegion || (isInsideNewRegion && !isInsideOldRegion) || forceUpdate) {
if (!bucket) if (!bucket)
bucket = _createBucket(buckets, bucketId); bucket = Grid._createBucket(buckets, bucketId);
_bucketAddBody(grid, bucket, body); Grid._bucketAddBody(grid, bucket, body);
} }
} }
} }
@ -136,7 +136,7 @@ var Common = require('../core/Common');
// update pairs list only if pairs changed (i.e. a body changed region) // update pairs list only if pairs changed (i.e. a body changed region)
if (gridChanged) if (gridChanged)
grid.pairsList = _createActivePairsList(grid); grid.pairsList = Grid._createActivePairsList(grid);
}; };
/** /**
@ -158,13 +158,13 @@ var Common = require('../core/Common');
* @param {} regionB * @param {} regionB
* @return {} region * @return {} region
*/ */
var _regionUnion = function(regionA, regionB) { Grid._regionUnion = function(regionA, regionB) {
var startCol = Math.min(regionA.startCol, regionB.startCol), var startCol = Math.min(regionA.startCol, regionB.startCol),
endCol = Math.max(regionA.endCol, regionB.endCol), endCol = Math.max(regionA.endCol, regionB.endCol),
startRow = Math.min(regionA.startRow, regionB.startRow), startRow = Math.min(regionA.startRow, regionB.startRow),
endRow = Math.max(regionA.endRow, regionB.endRow); endRow = Math.max(regionA.endRow, regionB.endRow);
return _createRegion(startCol, endCol, startRow, endRow); return Grid._createRegion(startCol, endCol, startRow, endRow);
}; };
/** /**
@ -175,14 +175,14 @@ var Common = require('../core/Common');
* @param {} body * @param {} body
* @return {} region * @return {} region
*/ */
var _getRegion = function(grid, body) { Grid._getRegion = function(grid, body) {
var bounds = body.bounds, var bounds = body.bounds,
startCol = Math.floor(bounds.min.x / grid.bucketWidth), startCol = Math.floor(bounds.min.x / grid.bucketWidth),
endCol = Math.floor(bounds.max.x / grid.bucketWidth), endCol = Math.floor(bounds.max.x / grid.bucketWidth),
startRow = Math.floor(bounds.min.y / grid.bucketHeight), startRow = Math.floor(bounds.min.y / grid.bucketHeight),
endRow = Math.floor(bounds.max.y / grid.bucketHeight); endRow = Math.floor(bounds.max.y / grid.bucketHeight);
return _createRegion(startCol, endCol, startRow, endRow); return Grid._createRegion(startCol, endCol, startRow, endRow);
}; };
/** /**
@ -195,7 +195,7 @@ var Common = require('../core/Common');
* @param {} endRow * @param {} endRow
* @return {} region * @return {} region
*/ */
var _createRegion = function(startCol, endCol, startRow, endRow) { Grid._createRegion = function(startCol, endCol, startRow, endRow) {
return { return {
id: startCol + ',' + endCol + ',' + startRow + ',' + endRow, id: startCol + ',' + endCol + ',' + startRow + ',' + endRow,
startCol: startCol, startCol: startCol,
@ -213,7 +213,7 @@ var Common = require('../core/Common');
* @param {} row * @param {} row
* @return {string} bucket id * @return {string} bucket id
*/ */
var _getBucketId = function(column, row) { Grid._getBucketId = function(column, row) {
return 'C' + column + 'R' + row; return 'C' + column + 'R' + row;
}; };
@ -225,7 +225,7 @@ var Common = require('../core/Common');
* @param {} bucketId * @param {} bucketId
* @return {} bucket * @return {} bucket
*/ */
var _createBucket = function(buckets, bucketId) { Grid._createBucket = function(buckets, bucketId) {
var bucket = buckets[bucketId] = []; var bucket = buckets[bucketId] = [];
return bucket; return bucket;
}; };
@ -238,7 +238,7 @@ var Common = require('../core/Common');
* @param {} bucket * @param {} bucket
* @param {} body * @param {} body
*/ */
var _bucketAddBody = function(grid, bucket, body) { Grid._bucketAddBody = function(grid, bucket, body) {
// add new pairs // add new pairs
for (var i = 0; i < bucket.length; i++) { for (var i = 0; i < bucket.length; i++) {
var bodyB = bucket[i]; var bodyB = bucket[i];
@ -270,7 +270,7 @@ var Common = require('../core/Common');
* @param {} bucket * @param {} bucket
* @param {} body * @param {} body
*/ */
var _bucketRemoveBody = function(grid, bucket, body) { Grid._bucketRemoveBody = function(grid, bucket, body) {
// remove from bucket // remove from bucket
bucket.splice(Common.indexOf(bucket, body), 1); bucket.splice(Common.indexOf(bucket, body), 1);
@ -294,7 +294,7 @@ var Common = require('../core/Common');
* @param {} grid * @param {} grid
* @return [] pairs * @return [] pairs
*/ */
var _createActivePairsList = function(grid) { Grid._createActivePairsList = function(grid) {
var pairKeys, var pairKeys,
pair, pair,
pairs = []; pairs = [];

View file

@ -13,7 +13,7 @@ var Common = require('../core/Common');
(function() { (function() {
var _pairMaxIdleLife = 1000; Pairs._pairMaxIdleLife = 1000;
/** /**
* Creates a new pairs structure. * Creates a new pairs structure.
@ -124,7 +124,7 @@ var Common = require('../core/Common');
} }
// if pair is inactive for too long, mark it to be removed // if pair is inactive for too long, mark it to be removed
if (timestamp - pair.timeUpdated > _pairMaxIdleLife) { if (timestamp - pair.timeUpdated > Pairs._pairMaxIdleLife) {
indexesToRemove.push(i); indexesToRemove.push(i);
} }
} }

View file

@ -54,7 +54,7 @@ var Vector = require('../geometry/Vector');
axisBodyB = axisBodyA === bodyA ? bodyB : bodyA, axisBodyB = axisBodyA === bodyA ? bodyB : bodyA,
axes = [axisBodyA.axes[previousCollision.axisNumber]]; axes = [axisBodyA.axes[previousCollision.axisNumber]];
minOverlap = _overlapAxes(axisBodyA.vertices, axisBodyB.vertices, axes); minOverlap = SAT._overlapAxes(axisBodyA.vertices, axisBodyB.vertices, axes);
collision.reused = true; collision.reused = true;
if (minOverlap.overlap <= 0) { if (minOverlap.overlap <= 0) {
@ -64,14 +64,14 @@ var Vector = require('../geometry/Vector');
} else { } else {
// if we can't reuse a result, perform a full SAT test // if we can't reuse a result, perform a full SAT test
overlapAB = _overlapAxes(bodyA.vertices, bodyB.vertices, bodyA.axes); overlapAB = SAT._overlapAxes(bodyA.vertices, bodyB.vertices, bodyA.axes);
if (overlapAB.overlap <= 0) { if (overlapAB.overlap <= 0) {
collision.collided = false; collision.collided = false;
return collision; return collision;
} }
overlapBA = _overlapAxes(bodyB.vertices, bodyA.vertices, bodyB.axes); overlapBA = SAT._overlapAxes(bodyB.vertices, bodyA.vertices, bodyB.axes);
if (overlapBA.overlap <= 0) { if (overlapBA.overlap <= 0) {
collision.collided = false; collision.collided = false;
@ -120,7 +120,7 @@ var Vector = require('../geometry/Vector');
collision.penetration.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 = SAT._findSupports(bodyA, bodyB, collision.normal),
supports = []; supports = [];
// find the supports from bodyB that are inside bodyA // find the supports from bodyB that are inside bodyA
@ -132,7 +132,7 @@ var Vector = require('../geometry/Vector');
// find the supports from bodyA that are inside bodyB // find the supports from bodyA that are inside bodyB
if (supports.length < 2) { if (supports.length < 2) {
var verticesA = _findSupports(bodyB, bodyA, Vector.neg(collision.normal)); var verticesA = SAT._findSupports(bodyB, bodyA, Vector.neg(collision.normal));
if (Vertices.contains(bodyB.vertices, verticesA[0])) if (Vertices.contains(bodyB.vertices, verticesA[0]))
supports.push(verticesA[0]); supports.push(verticesA[0]);
@ -159,7 +159,7 @@ var Vector = require('../geometry/Vector');
* @param {} axes * @param {} axes
* @return result * @return result
*/ */
var _overlapAxes = function(verticesA, verticesB, axes) { SAT._overlapAxes = function(verticesA, verticesB, axes) {
var projectionA = Vector._temp[0], var projectionA = Vector._temp[0],
projectionB = Vector._temp[1], projectionB = Vector._temp[1],
result = { overlap: Number.MAX_VALUE }, result = { overlap: Number.MAX_VALUE },
@ -169,8 +169,8 @@ var Vector = require('../geometry/Vector');
for (var i = 0; i < axes.length; i++) { for (var i = 0; i < axes.length; i++) {
axis = axes[i]; axis = axes[i];
_projectToAxis(projectionA, verticesA, axis); SAT._projectToAxis(projectionA, verticesA, axis);
_projectToAxis(projectionB, verticesB, axis); SAT._projectToAxis(projectionB, verticesB, axis);
overlap = Math.min(projectionA.max - projectionB.min, projectionB.max - projectionA.min); overlap = Math.min(projectionA.max - projectionB.min, projectionB.max - projectionA.min);
@ -197,7 +197,7 @@ var Vector = require('../geometry/Vector');
* @param {} vertices * @param {} vertices
* @param {} axis * @param {} axis
*/ */
var _projectToAxis = function(projection, vertices, axis) { SAT._projectToAxis = function(projection, vertices, axis) {
var min = Vector.dot(vertices[0], axis), var min = Vector.dot(vertices[0], axis),
max = min; max = min;
@ -224,7 +224,7 @@ var Vector = require('../geometry/Vector');
* @param {} normal * @param {} normal
* @return [vector] * @return [vector]
*/ */
var _findSupports = function(bodyA, bodyB, normal) { SAT._findSupports = function(bodyA, bodyB, normal) {
var nearestDistance = Number.MAX_VALUE, var nearestDistance = Number.MAX_VALUE,
vertexToBody = Vector._temp[0], vertexToBody = Vector._temp[0],
vertices = bodyB.vertices, vertices = bodyB.vertices,

View file

@ -77,7 +77,7 @@ var Bounds = require('../geometry/Bounds');
Events.on(engine, 'beforeUpdate', function() { Events.on(engine, 'beforeUpdate', function() {
var allBodies = Composite.allBodies(engine.world); var allBodies = Composite.allBodies(engine.world);
MouseConstraint.update(mouseConstraint, allBodies); MouseConstraint.update(mouseConstraint, allBodies);
_triggerEvents(mouseConstraint); MouseConstraint._triggerEvents(mouseConstraint);
}); });
return mouseConstraint; return mouseConstraint;
@ -136,7 +136,7 @@ var Bounds = require('../geometry/Bounds');
* @private * @private
* @param {mouse} mouseConstraint * @param {mouse} mouseConstraint
*/ */
var _triggerEvents = function(mouseConstraint) { MouseConstraint._triggerEvents = function(mouseConstraint) {
var mouse = mouseConstraint.mouse, var mouse = mouseConstraint.mouse,
mouseEvents = mouse.sourceEvents; mouseEvents = mouse.sourceEvents;

View file

@ -424,14 +424,14 @@ module.exports = Common;
for (var node in graph) { for (var node in graph) {
if (!visited[node] && !temp[node]) { if (!visited[node] && !temp[node]) {
_topologicalSort(node, visited, temp, graph, result); Common._topologicalSort(node, visited, temp, graph, result);
} }
} }
return result; return result;
}; };
var _topologicalSort = function(node, visited, temp, graph, result) { Common._topologicalSort = function(node, visited, temp, graph, result) {
var neighbors = graph[node] || []; var neighbors = graph[node] || [];
temp[node] = true; temp[node] = true;
@ -444,7 +444,7 @@ module.exports = Common;
} }
if (!visited[neighbor]) { if (!visited[neighbor]) {
_topologicalSort(neighbor, visited, temp, graph, result); Common._topologicalSort(neighbor, visited, temp, graph, result);
} }
} }

View file

@ -44,7 +44,7 @@ var Common = require('../core/Common');
}; };
mouse.mousemove = function(event) { mouse.mousemove = function(event) {
var position = _getRelativeMousePosition(event, mouse.element, mouse.pixelRatio), var position = Mouse._getRelativeMousePosition(event, mouse.element, mouse.pixelRatio),
touches = event.changedTouches; touches = event.changedTouches;
if (touches) { if (touches) {
@ -60,7 +60,7 @@ var Common = require('../core/Common');
}; };
mouse.mousedown = function(event) { mouse.mousedown = function(event) {
var position = _getRelativeMousePosition(event, mouse.element, mouse.pixelRatio), var position = Mouse._getRelativeMousePosition(event, mouse.element, mouse.pixelRatio),
touches = event.changedTouches; touches = event.changedTouches;
if (touches) { if (touches) {
@ -80,7 +80,7 @@ var Common = require('../core/Common');
}; };
mouse.mouseup = function(event) { mouse.mouseup = function(event) {
var position = _getRelativeMousePosition(event, mouse.element, mouse.pixelRatio), var position = Mouse._getRelativeMousePosition(event, mouse.element, mouse.pixelRatio),
touches = event.changedTouches; touches = event.changedTouches;
if (touches) { if (touches) {
@ -176,7 +176,7 @@ var Common = require('../core/Common');
* @param {number} pixelRatio * @param {number} pixelRatio
* @return {} * @return {}
*/ */
var _getRelativeMousePosition = function(event, element, pixelRatio) { Mouse._getRelativeMousePosition = function(event, element, pixelRatio) {
var elementBounds = element.getBoundingClientRect(), var elementBounds = element.getBoundingClientRect(),
rootNode = (document.documentElement || document.body.parentNode || document.body), rootNode = (document.documentElement || document.body.parentNode || document.body),
scrollX = (window.pageXOffset !== undefined) ? window.pageXOffset : rootNode.scrollLeft, scrollX = (window.pageXOffset !== undefined) ? window.pageXOffset : rootNode.scrollLeft,

View file

@ -97,7 +97,7 @@ var Bounds = require('../geometry/Bounds');
}; };
// ensure path is absolute // ensure path is absolute
_svgPathToAbsolute(path); Svg._svgPathToAbsolute(path);
// get total length // get total length
total = path.getTotalLength(); total = path.getTotalLength();
@ -149,7 +149,7 @@ var Bounds = require('../geometry/Bounds');
return points; return points;
}; };
var _svgPathToAbsolute = function(path) { Svg._svgPathToAbsolute = function(path) {
// http://phrogz.net/convert-svg-path-to-all-absolute-commands // http://phrogz.net/convert-svg-path-to-all-absolute-commands
// Copyright (c) Gavin Kistner // Copyright (c) Gavin Kistner
// http://phrogz.net/js/_ReuseLicense.txt // http://phrogz.net/js/_ReuseLicense.txt