Skip to content

Commit ec444f8

Browse files
committed
Extend SetBody component with setExistingBody method
This makes it easy to add compound bodies to a MatterSprite/MatterImage
1 parent 0d84860 commit ec444f8

1 file changed

Lines changed: 73 additions & 60 deletions

File tree

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

Lines changed: 73 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -26,82 +26,95 @@ var SetBody = {
2626
return this.setBody({ type: 'trapezoid', width: width, height: height, slope: slope }, options);
2727
},
2828

29-
setBody: function (config, options)
29+
setExistingBody: function (body, addToWorld)
3030
{
31-
// Existing body? Remove it.
31+
if (addToWorld === undefined)
32+
{
33+
addToWorld = true;
34+
}
35+
3236
if (this.body)
3337
{
3438
this.world.remove(this.body);
3539
}
3640

41+
this.body = body;
42+
this.body.gameObject = this;
43+
44+
if (addToWorld)
45+
{
46+
this.world.add(this.body);
47+
}
48+
49+
return this;
50+
},
51+
52+
setBody: function (config, options)
53+
{
3754
if (!config)
3855
{
3956
return this;
4057
}
41-
else
58+
59+
var body;
60+
61+
// Allow them to do: shape: 'circle' instead of shape: { type: 'circle' }
62+
if (typeof config === 'string')
4263
{
43-
// Allow them to do: shape: 'circle' instead of shape: { type: 'circle' }
44-
if (typeof config === 'string')
45-
{
46-
// Using defaults
47-
config = { type: config };
48-
}
49-
50-
var shapeType = GetFastValue(config, 'type', 'rectangle');
51-
var bodyX = GetFastValue(config, 'x', this._tempVec2.x);
52-
var bodyY = GetFastValue(config, 'y', this._tempVec2.y);
53-
var bodyWidth = GetFastValue(config, 'width', this.width);
54-
var bodyHeight = GetFastValue(config, 'height', this.height);
55-
56-
switch (shapeType)
57-
{
58-
case 'rectangle':
59-
this.body = Bodies.rectangle(bodyX, bodyY, bodyWidth, bodyHeight, options);
60-
break;
61-
62-
case 'circle':
63-
var radius = GetFastValue(config, 'radius', Math.max(bodyWidth, bodyHeight) / 2);
64-
var maxSides = GetFastValue(config, 'maxSides', 25);
65-
this.body = Bodies.circle(bodyX, bodyY, radius, options, maxSides);
66-
break;
67-
68-
case 'trapezoid':
69-
var slope = GetFastValue(config, 'slope', 0.5);
70-
this.body = Bodies.trapezoid(bodyX, bodyY, bodyWidth, bodyHeight, slope, options);
71-
break;
72-
73-
case 'polygon':
74-
var sides = GetFastValue(config, 'sides', 5);
75-
var pradius = GetFastValue(config, 'radius', Math.max(bodyWidth, bodyHeight) / 2);
76-
this.body = Bodies.polygon(bodyX, bodyY, sides, pradius, options);
77-
break;
78-
79-
case 'fromVertices':
80-
case 'fromVerts':
81-
var verts = GetFastValue(config, 'verts', []);
82-
83-
if (this.body)
84-
{
85-
Body.setVertices(this.body, verts);
86-
}
87-
else
88-
{
89-
var flagInternal = GetFastValue(config, 'flagInternal', false);
90-
var removeCollinear = GetFastValue(config, 'removeCollinear', 0.01);
91-
var minimumArea = GetFastValue(config, 'minimumArea', 10);
92-
this.body = Bodies.fromVertices(bodyX, bodyY, verts, options, flagInternal, removeCollinear, minimumArea);
93-
}
94-
break;
95-
}
64+
// Using defaults
65+
config = { type: config };
9666
}
9767

98-
this.body.gameObject = this;
99-
100-
if (GetFastValue(config, 'addToWorld', true))
68+
var shapeType = GetFastValue(config, 'type', 'rectangle');
69+
var bodyX = GetFastValue(config, 'x', this._tempVec2.x);
70+
var bodyY = GetFastValue(config, 'y', this._tempVec2.y);
71+
var bodyWidth = GetFastValue(config, 'width', this.width);
72+
var bodyHeight = GetFastValue(config, 'height', this.height);
73+
74+
switch (shapeType)
10175
{
102-
this.world.add(this.body);
76+
case 'rectangle':
77+
body = Bodies.rectangle(bodyX, bodyY, bodyWidth, bodyHeight, options);
78+
break;
79+
80+
case 'circle':
81+
var radius = GetFastValue(config, 'radius', Math.max(bodyWidth, bodyHeight) / 2);
82+
var maxSides = GetFastValue(config, 'maxSides', 25);
83+
body = Bodies.circle(bodyX, bodyY, radius, options, maxSides);
84+
break;
85+
86+
case 'trapezoid':
87+
var slope = GetFastValue(config, 'slope', 0.5);
88+
body = Bodies.trapezoid(bodyX, bodyY, bodyWidth, bodyHeight, slope, options);
89+
break;
90+
91+
case 'polygon':
92+
var sides = GetFastValue(config, 'sides', 5);
93+
var pradius = GetFastValue(config, 'radius', Math.max(bodyWidth, bodyHeight) / 2);
94+
body = Bodies.polygon(bodyX, bodyY, sides, pradius, options);
95+
break;
96+
97+
case 'fromVertices':
98+
case 'fromVerts':
99+
var verts = GetFastValue(config, 'verts', []);
100+
101+
if (this.body)
102+
{
103+
Body.setVertices(this.body, verts);
104+
body = this.body;
105+
}
106+
else
107+
{
108+
var flagInternal = GetFastValue(config, 'flagInternal', false);
109+
var removeCollinear = GetFastValue(config, 'removeCollinear', 0.01);
110+
var minimumArea = GetFastValue(config, 'minimumArea', 10);
111+
body = Bodies.fromVertices(bodyX, bodyY, verts, options, flagInternal, removeCollinear, minimumArea);
112+
}
113+
break;
103114
}
104115

116+
this.setExisiting(body, config.addToWorld);
117+
105118
return this;
106119
}
107120

0 commit comments

Comments
 (0)