Skip to content

Commit 944e03d

Browse files
committed
P2.Body.loadPolygon has been updated to correct center of mass issues (thanks @georgiee, fix phaserjs#749)
1 parent 7788365 commit 944e03d

3 files changed

Lines changed: 34 additions & 21 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ By Richard Davey, [Photon Storm](http://www.photonstorm.com)
1919
[Subscribe to our new Phaser Newsletter](https://confirmsubscription.com/h/r/369DE48E3E86AF1E). We'll email you when new versions are released as well as send you our regular Phaser game making magazine.
2020

2121
[![Build Status](https://travis-ci.org/photonstorm/phaser.png?branch=dev)](https://travis-ci.org/photonstorm/phaser)
22-
[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/photonstorm/phaser/trend.png)](https://bitdeli.com/free "Bitdeli Badge")
2322

2423

2524
## What's new in 2.0.4?
@@ -104,6 +103,7 @@ Version 2.0.4 - "Mos Shirare" - in development
104103
* Destroying an object with an input handler during its onDown event would throw Signals dispatch errors (thanks @jflowers45, fix #746)
105104
* Circle.distance used an incorrect Math call if you wanted a rounded distance value (thanks @OpherV, fix #745)
106105
* Point.distance used an incorrect Math call if you wanted a rounded distance value (thanks @OpherV, fix #745)
106+
* P2.Body.loadPolygon has been updated to correct center of mass issues (thanks @georgiee, fix #749)
107107

108108

109109
### ToDo

src/core/LinkedList.js

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ Phaser.LinkedList.prototype = {
5656
add: function (child) {
5757

5858
// If the list is empty
59-
if (this.total === 0 && this.first == null && this.last == null)
59+
if (this.total === 0 && this.first === null && this.last === null)
6060
{
6161
this.first = child;
6262
this.last = child;
@@ -66,7 +66,7 @@ Phaser.LinkedList.prototype = {
6666
return child;
6767
}
6868

69-
// Get gets appended to the end of the list, regardless of anything, and it won't have any children of its own (non-nested list)
69+
// Gets appended to the end of the list, regardless of anything, and it won't have any children of its own (non-nested list)
7070
this.last.next = child;
7171

7272
child.prev = this.last;
@@ -79,6 +79,21 @@ Phaser.LinkedList.prototype = {
7979

8080
},
8181

82+
/**
83+
* Resets the first, last, next and previous node pointers in this list.
84+
*
85+
* @method Phaser.LinkedList#reset
86+
*/
87+
reset: function () {
88+
89+
this.first = null;
90+
this.last = null;
91+
this.next = null;
92+
this.prev = null;
93+
this.total = 0;
94+
95+
},
96+
8297
/**
8398
* Removes the given element from this linked list if it exists.
8499
*
@@ -87,12 +102,19 @@ Phaser.LinkedList.prototype = {
87102
*/
88103
remove: function (child) {
89104

90-
if (child == this.first)
105+
if (this.total === 1)
106+
{
107+
this.reset();
108+
child.next = child.prev = null;
109+
return;
110+
}
111+
112+
if (child === this.first)
91113
{
92114
// It was 'first', make 'first' point to first.next
93115
this.first = this.first.next;
94116
}
95-
else if (child == this.last)
117+
else if (child === this.last)
96118
{
97119
// It was 'last', make 'last' point to last.prev
98120
this.last = this.last.prev;
@@ -112,7 +134,7 @@ Phaser.LinkedList.prototype = {
112134

113135
child.next = child.prev = null;
114136

115-
if (this.first == null )
137+
if (this.first === null )
116138
{
117139
this.last = null;
118140
}

src/physics/p2/Body.js

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,7 +1131,7 @@ Phaser.Physics.P2.Body.prototype = {
11311131
},
11321132

11331133
/**
1134-
* Add a polygon fixture. This is used during #loadPhaserPolygon.
1134+
* Add a polygon fixture. This is used during #loadPolygon.
11351135
*
11361136
* @method Phaser.Physics.P2.Body#addFixture
11371137
* @param {string} fixtureData - The data for the fixture. It contains: isSensor, filter (collision) and the actual polygon shapes.
@@ -1208,13 +1208,9 @@ Phaser.Physics.P2.Body.prototype = {
12081208
* @method Phaser.Physics.P2.Body#loadPolygon
12091209
* @param {string} key - The key of the Physics Data file as stored in Game.Cache.
12101210
* @param {string} object - The key of the object within the Physics data file that you wish to load the shape data from.
1211-
* @param {object} options - An object containing the build options. Note that this isn't used if the data file contains multiple shapes.
1212-
* @param {boolean} [options.optimalDecomp=false] - Set to true if you need optimal decomposition. Warning: very slow for polygons with more than 10 vertices.
1213-
* @param {boolean} [options.skipSimpleCheck=false] - Set to true if you already know that the path is not intersecting itself.
1214-
* @param {boolean|number} [options.removeCollinearPoints=false] - Set to a number (angle threshold value) to remove collinear points, or false to keep all points.
12151211
* @return {boolean} True on success, else false.
12161212
*/
1217-
loadPolygon: function (key, object, options) {
1213+
loadPolygon: function (key, object) {
12181214

12191215
var data = this.game.cache.getPhysicsData(key, object);
12201216

@@ -1260,26 +1256,21 @@ Phaser.Physics.P2.Body.prototype = {
12601256

12611257
/**
12621258
* Reads the physics data from a physics data file stored in the Game.Cache.
1263-
* It will add the shape data to this Body, as well as set the density (mass), friction and bounce (restitution) values.
1259+
* It will add the shape data to this Body, as well as set the density (mass).
12641260
*
1265-
* @method Phaser.Physics.P2.Body#loadPolygon
1261+
* @method Phaser.Physics.P2.Body#loadData
12661262
* @param {string} key - The key of the Physics Data file as stored in Game.Cache.
12671263
* @param {string} object - The key of the object within the Physics data file that you wish to load the shape data from.
1268-
* @param {object} options - An object containing the build options:
1269-
* @param {boolean} [options.optimalDecomp=false] - Set to true if you need optimal decomposition. Warning: very slow for polygons with more than 10 vertices.
1270-
* @param {boolean} [options.skipSimpleCheck=false] - Set to true if you already know that the path is not intersecting itself.
1271-
* @param {boolean|number} [options.removeCollinearPoints=false] - Set to a number (angle threshold value) to remove collinear points, or false to keep all points.
12721264
* @return {boolean} True on success, else false.
12731265
*/
1274-
loadData: function (key, object, options) {
1266+
loadData: function (key, object) {
12751267

12761268
var data = this.game.cache.getPhysicsData(key, object);
12771269

12781270
if (data && data.shape)
12791271
{
12801272
this.mass = data.density;
1281-
this.loadPolygon(key, object, options);
1282-
// TODO set friction + bounce here
1273+
return this.loadPolygon(key, object);
12831274
}
12841275

12851276
}

0 commit comments

Comments
 (0)