Skip to content

Commit ac9f5c4

Browse files
committed
Added quick support for setCircle, setRectangle, setPolygon and setTrapezoid.
1 parent 036e7ae commit ac9f5c4

1 file changed

Lines changed: 31 additions & 2 deletions

File tree

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

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,28 @@ var GetFastValue = require('../../../utils/object/GetFastValue');
44

55
var SetBody = {
66

7+
// Calling any of these methods resets previous properties you may have set on the body, including plugins, mass, etc
8+
9+
setRectangle: function (width, height, options)
10+
{
11+
return this.setBody({ type: 'rectangle', width: width, height: height }, options);
12+
},
13+
14+
setCircle: function (radius, options)
15+
{
16+
return this.setBody({ type: 'circle', radius: radius }, options);
17+
},
18+
19+
setPolygon: function (radius, sides, options)
20+
{
21+
return this.setBody({ type: 'polygon', sides: sides, radius: radius }, options);
22+
},
23+
24+
setTrapezoid: function (width, height, slope, options)
25+
{
26+
return this.setBody({ type: 'trapezoid', width: width, height: height, slope: slope }, options);
27+
},
28+
729
setBody: function (config, options)
830
{
931
// Existing body? Remove it.
@@ -18,6 +40,13 @@ var SetBody = {
1840
}
1941
else
2042
{
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+
2150
var shapeType = GetFastValue(config, 'type', 'rectangle');
2251
var bodyX = GetFastValue(config, 'x', this._tempVec2.x);
2352
var bodyY = GetFastValue(config, 'y', this._tempVec2.y);
@@ -43,8 +72,8 @@ var SetBody = {
4372

4473
case 'polygon':
4574
var sides = GetFastValue(config, 'sides', 5);
46-
var radius = GetFastValue(config, 'radius', Math.max(bodyWidth, bodyHeight) / 2);
47-
this.body = Bodies.polygon(bodyX, bodyY, sides, radius, options);
75+
var pradius = GetFastValue(config, 'radius', Math.max(bodyWidth, bodyHeight) / 2);
76+
this.body = Bodies.polygon(bodyX, bodyY, sides, pradius, options);
4877
break;
4978

5079
case 'fromVertices':

0 commit comments

Comments
 (0)