Skip to content

Commit 9c5876f

Browse files
committed
Skip conditional if fromVerts given. Correctly set parts reference.
1 parent 80f7539 commit 9c5876f

2 files changed

Lines changed: 37 additions & 15 deletions

File tree

src/physics/matter-js/MatterGameObject.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ var MatterGameObject = function (world, gameObject, options)
4444

4545
// Temp body pos to avoid body null checks
4646
gameObject.body = {
47+
temp: true,
4748
position: {
4849
x: x,
4950
y: y

src/physics/matter-js/components/SetBody.js

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ var Bodies = require('../lib/factory/Bodies');
88
var Body = require('../lib/body/Body');
99
var GetFastValue = require('../../../utils/object/GetFastValue');
1010
var PhysicsEditorParser = require('../PhysicsEditorParser');
11+
var Vertices = require('../lib/geometry/Vertices');
1112

1213
/**
1314
* [description]
@@ -111,7 +112,11 @@ var SetBody = {
111112
}
112113

113114
this.body = body;
114-
this.body.gameObject = this;
115+
116+
for (var i = 0; i < body.parts.length; i++)
117+
{
118+
body.parts[i].gameObject = this;
119+
}
115120

116121
var _this = this;
117122

@@ -186,34 +191,50 @@ var SetBody = {
186191

187192
case 'polygon':
188193
var sides = GetFastValue(config, 'sides', 5);
189-
var pradius = GetFastValue(config, 'radius', Math.max(bodyWidth, bodyHeight) / 2);
190-
body = Bodies.polygon(bodyX, bodyY, sides, pradius, options);
194+
var pRadius = GetFastValue(config, 'radius', Math.max(bodyWidth, bodyHeight) / 2);
195+
body = Bodies.polygon(bodyX, bodyY, sides, pRadius, options);
191196
break;
192197

193198
case 'fromVertices':
194199
case 'fromVerts':
195-
var verts = GetFastValue(config, 'verts', []);
196200

197-
if (this.body)
198-
{
199-
Body.setVertices(this.body, verts);
200-
body = this.body;
201-
}
202-
else
201+
var verts = GetFastValue(config, 'verts', null);
202+
203+
if (verts)
203204
{
204-
var flagInternal = GetFastValue(config, 'flagInternal', false);
205-
var removeCollinear = GetFastValue(config, 'removeCollinear', 0.01);
206-
var minimumArea = GetFastValue(config, 'minimumArea', 10);
207-
body = Bodies.fromVertices(bodyX, bodyY, verts, options, flagInternal, removeCollinear, minimumArea);
205+
// Has the verts array come from Vertices.fromPath, or is it raw?
206+
if (typeof verts === 'string')
207+
{
208+
verts = Vertices.fromPath(verts);
209+
}
210+
211+
if (this.body && !this.body.hasOwnProperty('temp'))
212+
{
213+
Body.setVertices(this.body, verts);
214+
215+
body = this.body;
216+
}
217+
else
218+
{
219+
var flagInternal = GetFastValue(config, 'flagInternal', false);
220+
var removeCollinear = GetFastValue(config, 'removeCollinear', 0.01);
221+
var minimumArea = GetFastValue(config, 'minimumArea', 10);
222+
223+
body = Bodies.fromVertices(bodyX, bodyY, verts, options, flagInternal, removeCollinear, minimumArea);
224+
}
208225
}
226+
209227
break;
210228

211229
case 'fromPhysicsEditor':
212230
body = PhysicsEditorParser.parseBody(bodyX, bodyY, bodyWidth, bodyHeight, config);
213231
break;
214232
}
215233

216-
this.setExistingBody(body, config.addToWorld);
234+
if (body)
235+
{
236+
this.setExistingBody(body, config.addToWorld);
237+
}
217238

218239
return this;
219240
}

0 commit comments

Comments
 (0)