Skip to content

Commit 65fb366

Browse files
committed
Matter updated to 0.14.2. Fix phaserjs#3929
1 parent 0dccc3a commit 65fb366

19 files changed

Lines changed: 192 additions & 125 deletions

File tree

CHANGELOG.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,23 @@ The four draw orders are:
9999
* ParseJSONTiled now extracts the `renderorder` property from the Tiled JSON.
100100
* MapData has a new `renderOrder` property, which is populated by the Tiled Parser.
101101

102+
### Matter.js Updates
103+
104+
The version of Matter.js used by Phaser has been updated from 0.13.1 to 0.14.2. To clarify why we don't include Matter via npm, it's because we use a customized version of Matter that includes extra features and optimizations not yet found in the official library.
105+
106+
Most of the updates were about documentation and module namespacing, however those relevant to Phaser are listed below. You can also view the full [Matter Change Log](https://github.com/liabru/matter-js/blob/master/CHANGELOG.md).
107+
108+
* fix Composite.bounds global issue, closes #627, closes #544 ([f7f77b4](https://github.com/liabru/matter-js/commit/f7f77b4)), closes [#627](https://github.com/liabru/matter-js/issues/627) [#544](https://github.com/liabru/matter-js/issues/544)
109+
* updated pathseg library, closes #548, closes #602, closes #424 ([1e5758f](https://github.com/liabru/matter-js/commit/1e5758f)), closes [#548](https://github.com/liabru/matter-js/issues/548) [#602](https://github.com/liabru/matter-js/issues/602) [#424](https://github.com/liabru/matter-js/issues/424)
110+
* fix Common.isElement on node, closes #535 ([ec38eeb](https://github.com/liabru/matter-js/commit/ec38eeb)), closes [#535](https://github.com/liabru/matter-js/issues/535)
111+
* added Query.collides, closes #478 ([6593a72](https://github.com/liabru/matter-js/commit/6593a72)), closes [#478](https://github.com/liabru/matter-js/issues/478)
112+
* fix `point` argument of Body.scale, closes #428 ([894c1ef](https://github.com/liabru/matter-js/commit/894c1ef)), closes [#428](https://github.com/liabru/matter-js/issues/428)
113+
* fix Body.scale for compound bodies ([50a89d0](https://github.com/liabru/matter-js/commit/50a89d0))
114+
* fix centroid for static compound bodies, closes #483 ([ece66e6](https://github.com/liabru/matter-js/commit/ece66e6)), closes [#483](https://github.com/liabru/matter-js/issues/483)
115+
* fix Common.isElement, closes #501, closes #507, closes #459, closes #468, closes #517 ([18a0845](https://github.com/liabru/matter-js/commit/18a0845)), closes [#501](https://github.com/liabru/matter-js/issues/501) [#507](https://github.com/liabru/matter-js/issues/507) [#459](https://github.com/liabru/matter-js/issues/459) [#468](https://github.com/liabru/matter-js/issues/468) [#517](https://github.com/liabru/matter-js/issues/517)
116+
* fix inertia change in Body.setMass, closes #378 ([f7d1877](https://github.com/liabru/matter-js/commit/f7d1877)), closes [#378](https://github.com/liabru/matter-js/issues/378)
117+
* fix Vertices.chamfer radius argument, closes #467 ([3bceef4](https://github.com/liabru/matter-js/commit/3bceef4)), closes [#467](https://github.com/liabru/matter-js/issues/467)
118+
102119
### New Features
103120

104121
* `Camera.resolution` is a new read-only property that holds the current game config resolution that the camera is using. This is used internally for viewport calculations.

src/physics/matter-js/lib/body/Body.js

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -184,11 +184,11 @@ var Axes = require('../geometry/Axes');
184184
* Prefer to use the actual setter functions in performance critical situations.
185185
* @method set
186186
* @param {body} body
187-
* @param {object} settings A map of properties and values to set on the body.
187+
* @param {} settings A property name (or map of properties and values) to set on the body.
188+
* @param {} value The value to set if `settings` is a single property name.
188189
*/
189-
Body.set = function(body, settings) {
190-
var property,
191-
value;
190+
Body.set = function(body, settings, value) {
191+
var property;
192192

193193
if (typeof settings === 'string') {
194194
property = settings;
@@ -417,7 +417,7 @@ var Axes = require('../geometry/Axes');
417417
}
418418

419419
// sum the properties of all compound parts of the parent body
420-
var total = _totalProperties(body);
420+
var total = Body._totalProperties(body);
421421

422422
body.area = total.area;
423423
body.parent = body;
@@ -543,35 +543,50 @@ var Axes = require('../geometry/Axes');
543543
* @param {vector} [point]
544544
*/
545545
Body.scale = function(body, scaleX, scaleY, point) {
546+
var totalArea = 0,
547+
totalInertia = 0;
548+
546549
point = point || body.position;
547550

548551
for (var i = 0; i < body.parts.length; i++) {
549552
var part = body.parts[i];
550553

551-
// scale position
552-
part.position.x = point.x + (part.position.x - point.x) * scaleX;
553-
part.position.y = point.y + (part.position.y - point.y) * scaleY;
554-
555554
// scale vertices
556555
Vertices.scale(part.vertices, scaleX, scaleY, point);
557556

558557
// update properties
559558
part.axes = Axes.fromVertices(part.vertices);
559+
part.area = Vertices.area(part.vertices);
560+
Body.setMass(part, body.density * part.area);
560561

561-
if (!body.isStatic) {
562-
part.area = Vertices.area(part.vertices);
563-
Body.setMass(part, body.density * part.area);
562+
// update inertia (requires vertices to be at origin)
563+
Vertices.translate(part.vertices, { x: -part.position.x, y: -part.position.y });
564+
Body.setInertia(part, Body._inertiaScale * Vertices.inertia(part.vertices, part.mass));
565+
Vertices.translate(part.vertices, { x: part.position.x, y: part.position.y });
564566

565-
// update inertia (requires vertices to be at origin)
566-
Vertices.translate(part.vertices, { x: -part.position.x, y: -part.position.y });
567-
Body.setInertia(part, Vertices.inertia(part.vertices, part.mass));
568-
Vertices.translate(part.vertices, { x: part.position.x, y: part.position.y });
567+
if (i > 0) {
568+
totalArea += part.area;
569+
totalInertia += part.inertia;
569570
}
570571

572+
// scale position
573+
part.position.x = point.x + (part.position.x - point.x) * scaleX;
574+
part.position.y = point.y + (part.position.y - point.y) * scaleY;
575+
571576
// update bounds
572577
Bounds.update(part.bounds, part.vertices, body.velocity);
573578
}
574579

580+
// handle parent body
581+
if (body.parts.length > 1) {
582+
body.area = totalArea;
583+
584+
if (!body.isStatic) {
585+
Body.setMass(body, body.density * totalArea);
586+
Body.setInertia(body, totalInertia);
587+
}
588+
}
589+
575590
// handle circles
576591
if (body.circleRadius) {
577592
if (scaleX === scaleY) {
@@ -581,13 +596,6 @@ var Axes = require('../geometry/Axes');
581596
body.circleRadius = null;
582597
}
583598
}
584-
585-
if (!body.isStatic) {
586-
var total = _totalProperties(body);
587-
body.area = total.area;
588-
Body.setMass(body, total.mass);
589-
Body.setInertia(body, total.inertia);
590-
}
591599
};
592600

593601
/**
@@ -668,7 +676,7 @@ var Axes = require('../geometry/Axes');
668676
* @param {body} body
669677
* @return {}
670678
*/
671-
var _totalProperties = function(body) {
679+
Body._totalProperties = function(body) {
672680
// from equations at:
673681
// https://ecourses.ou.edu/cgi-bin/ebook.cgi?doc=&topic=st&chap_sec=07.2&page=theory
674682
// http://output.to/sideway/default.asp?qno=121100087
@@ -685,7 +693,7 @@ var Axes = require('../geometry/Axes');
685693
var part = body.parts[i],
686694
mass = part.mass !== Infinity ? part.mass : 1;
687695

688-
properties.mass += part.mass;
696+
properties.mass += mass;
689697
properties.area += part.area;
690698
properties.inertia += part.inertia;
691699
properties.centre = Vector.add(properties.centre, Vector.mult(part.position, mass));

src/physics/matter-js/lib/body/Composite.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ module.exports = Composite;
1515

1616
var Events = require('../core/Events');
1717
var Common = require('../core/Common');
18+
var Bounds = require('../geometry/Bounds');
1819
var Body = require('./Body');
1920

2021
(function() {
@@ -539,15 +540,15 @@ var Body = require('./Body');
539540
* @returns {bounds} The composite bounds.
540541
*/
541542
Composite.bounds = function(composite) {
542-
var bodies = Matter.Composite.allBodies(composite),
543+
var bodies = Composite.allBodies(composite),
543544
vertices = [];
544545

545546
for (var i = 0; i < bodies.length; i += 1) {
546547
var body = bodies[i];
547548
vertices.push(body.bounds.min, body.bounds.max);
548549
}
549550

550-
return Matter.Bounds.create(vertices);
551+
return Bounds.create(vertices);
551552
};
552553

553554
/*

src/physics/matter-js/lib/body/World.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,23 @@ var Common = require('../core/Common');
9696
// World is a Composite body
9797
// see src/module/Outro.js for these aliases:
9898

99+
/**
100+
* An alias for Composite.add
101+
* @method add
102+
* @param {world} world
103+
* @param {} object
104+
* @return {composite} The original world with the objects added
105+
*/
106+
107+
/**
108+
* An alias for Composite.remove
109+
* @method remove
110+
* @param {world} world
111+
* @param {} object
112+
* @param {boolean} [deep=false]
113+
* @return {composite} The original world with the objects removed
114+
*/
115+
99116
/**
100117
* An alias for Composite.clear
101118
* @method clear
@@ -104,7 +121,7 @@ var Common = require('../core/Common');
104121
*/
105122

106123
/**
107-
* An alias for Composite.add
124+
* An alias for Composite.addComposite
108125
* @method addComposite
109126
* @param {world} world
110127
* @param {composite} composite

src/physics/matter-js/lib/collision/Grid.js

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ var Common = require('../core/Common');
8282
|| body.bounds.max.y < world.bounds.min.y || body.bounds.min.y > world.bounds.max.y)
8383
continue;
8484

85-
var newRegion = _getRegion(grid, body);
85+
var newRegion = Grid._getRegion(grid, body);
8686

8787
// if the body has changed grid region
8888
if (!body.region || newRegion.id !== body.region.id || forceUpdate) {
@@ -94,13 +94,13 @@ var Common = require('../core/Common');
9494
if (!body.region || forceUpdate)
9595
body.region = newRegion;
9696

97-
var union = _regionUnion(newRegion, body.region);
97+
var union = Grid._regionUnion(newRegion, body.region);
9898

9999
// update grid buckets affected by region change
100100
// iterate over the union of both regions
101101
for (col = union.startCol; col <= union.endCol; col++) {
102102
for (row = union.startRow; row <= union.endRow; row++) {
103-
bucketId = _getBucketId(col, row);
103+
bucketId = Grid._getBucketId(col, row);
104104
bucket = buckets[bucketId];
105105

106106
var isInsideNewRegion = (col >= newRegion.startCol && col <= newRegion.endCol
@@ -113,15 +113,15 @@ var Common = require('../core/Common');
113113
if (!isInsideNewRegion && isInsideOldRegion) {
114114
if (isInsideOldRegion) {
115115
if (bucket)
116-
_bucketRemoveBody(grid, bucket, body);
116+
Grid._bucketRemoveBody(grid, bucket, body);
117117
}
118118
}
119119

120120
// add to new region buckets
121121
if (body.region === newRegion || (isInsideNewRegion && !isInsideOldRegion) || forceUpdate) {
122122
if (!bucket)
123-
bucket = _createBucket(buckets, bucketId);
124-
_bucketAddBody(grid, bucket, body);
123+
bucket = Grid._createBucket(buckets, bucketId);
124+
Grid._bucketAddBody(grid, bucket, body);
125125
}
126126
}
127127
}
@@ -136,7 +136,7 @@ var Common = require('../core/Common');
136136

137137
// update pairs list only if pairs changed (i.e. a body changed region)
138138
if (gridChanged)
139-
grid.pairsList = _createActivePairsList(grid);
139+
grid.pairsList = Grid._createActivePairsList(grid);
140140
};
141141

142142
/**
@@ -158,13 +158,13 @@ var Common = require('../core/Common');
158158
* @param {} regionB
159159
* @return {} region
160160
*/
161-
var _regionUnion = function(regionA, regionB) {
161+
Grid._regionUnion = function(regionA, regionB) {
162162
var startCol = Math.min(regionA.startCol, regionB.startCol),
163163
endCol = Math.max(regionA.endCol, regionB.endCol),
164164
startRow = Math.min(regionA.startRow, regionB.startRow),
165165
endRow = Math.max(regionA.endRow, regionB.endRow);
166166

167-
return _createRegion(startCol, endCol, startRow, endRow);
167+
return Grid._createRegion(startCol, endCol, startRow, endRow);
168168
};
169169

170170
/**
@@ -175,14 +175,14 @@ var Common = require('../core/Common');
175175
* @param {} body
176176
* @return {} region
177177
*/
178-
var _getRegion = function(grid, body) {
178+
Grid._getRegion = function(grid, body) {
179179
var bounds = body.bounds,
180180
startCol = Math.floor(bounds.min.x / grid.bucketWidth),
181181
endCol = Math.floor(bounds.max.x / grid.bucketWidth),
182182
startRow = Math.floor(bounds.min.y / grid.bucketHeight),
183183
endRow = Math.floor(bounds.max.y / grid.bucketHeight);
184184

185-
return _createRegion(startCol, endCol, startRow, endRow);
185+
return Grid._createRegion(startCol, endCol, startRow, endRow);
186186
};
187187

188188
/**
@@ -195,7 +195,7 @@ var Common = require('../core/Common');
195195
* @param {} endRow
196196
* @return {} region
197197
*/
198-
var _createRegion = function(startCol, endCol, startRow, endRow) {
198+
Grid._createRegion = function(startCol, endCol, startRow, endRow) {
199199
return {
200200
id: startCol + ',' + endCol + ',' + startRow + ',' + endRow,
201201
startCol: startCol,
@@ -213,7 +213,7 @@ var Common = require('../core/Common');
213213
* @param {} row
214214
* @return {string} bucket id
215215
*/
216-
var _getBucketId = function(column, row) {
216+
Grid._getBucketId = function(column, row) {
217217
return 'C' + column + 'R' + row;
218218
};
219219

@@ -225,7 +225,7 @@ var Common = require('../core/Common');
225225
* @param {} bucketId
226226
* @return {} bucket
227227
*/
228-
var _createBucket = function(buckets, bucketId) {
228+
Grid._createBucket = function(buckets, bucketId) {
229229
var bucket = buckets[bucketId] = [];
230230
return bucket;
231231
};
@@ -238,7 +238,7 @@ var Common = require('../core/Common');
238238
* @param {} bucket
239239
* @param {} body
240240
*/
241-
var _bucketAddBody = function(grid, bucket, body) {
241+
Grid._bucketAddBody = function(grid, bucket, body) {
242242
// add new pairs
243243
for (var i = 0; i < bucket.length; i++) {
244244
var bodyB = bucket[i];
@@ -270,9 +270,9 @@ var Common = require('../core/Common');
270270
* @param {} bucket
271271
* @param {} body
272272
*/
273-
var _bucketRemoveBody = function(grid, bucket, body) {
273+
Grid._bucketRemoveBody = function(grid, bucket, body) {
274274
// remove from bucket
275-
bucket.splice(bucket.indexOf(body), 1);
275+
bucket.splice(Common.indexOf(bucket, body), 1);
276276

277277
// update pair counts
278278
for (var i = 0; i < bucket.length; i++) {
@@ -294,7 +294,7 @@ var Common = require('../core/Common');
294294
* @param {} grid
295295
* @return [] pairs
296296
*/
297-
var _createActivePairsList = function(grid) {
297+
Grid._createActivePairsList = function(grid) {
298298
var pairKeys,
299299
pair,
300300
pairs = [];

src/physics/matter-js/lib/collision/Pair.js

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ module.exports = Pair;
3232
isSensor: bodyA.isSensor || bodyB.isSensor,
3333
timeCreated: timestamp,
3434
timeUpdated: timestamp,
35-
3635
collision: null,
3736
inverseMass: 0,
3837
friction: 0,
@@ -54,22 +53,9 @@ module.exports = Pair;
5453
* @param {number} timestamp
5554
*/
5655
Pair.update = function(pair, collision, timestamp) {
57-
// var contacts = pair.contacts,
58-
// supports = collision.supports,
59-
// activeContacts = pair.activeContacts,
60-
// parentA = collision.parentA,
61-
// parentB = collision.parentB;
62-
6356
pair.collision = collision;
64-
// pair.inverseMass = parentA.inverseMass + parentB.inverseMass;
65-
// pair.friction = Math.min(parentA.friction, parentB.friction);
66-
// pair.frictionStatic = Math.max(parentA.frictionStatic, parentB.frictionStatic);
67-
// pair.restitution = Math.max(parentA.restitution, parentB.restitution);
68-
// pair.slop = Math.max(parentA.slop, parentB.slop);
69-
// activeContacts.length = 0;
70-
71-
if (collision.collided) {
7257

58+
if (collision.collided) {
7359
var supports = collision.supports,
7460
activeContacts = pair.activeContacts,
7561
parentA = collision.parentA,
@@ -85,8 +71,8 @@ module.exports = Pair;
8571
activeContacts[i] = supports[i].contact;
8672
}
8773

74+
// optimise array size
8875
var supportCount = supports.length;
89-
9076
if (supportCount < activeContacts.length) {
9177
activeContacts.length = supportCount;
9278
}

0 commit comments

Comments
 (0)