Skip to content

Commit f8fe81d

Browse files
committed
New setBody component for setting and change body shape
1 parent ff316be commit f8fe81d

4 files changed

Lines changed: 108 additions & 28 deletions

File tree

v3/src/physics/matter-js/MatterImage.js

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ var MatterImage = new Class({
1818
Components.Gravity,
1919
Components.Mass,
2020
Components.Sensor,
21+
Components.SetBody,
2122
Components.Sleep,
2223
Components.Static,
2324
Components.Transform,
@@ -35,28 +36,26 @@ var MatterImage = new Class({
3536
this.setSizeToFrame();
3637
this.setOrigin();
3738

38-
this._tempVec2 = new Vector2();
39+
this.world = world;
3940

40-
var isCircle = GetFastValue(options, 'isCircle', false);
41+
this._tempVec2 = new Vector2(x, y);
4142

42-
if (isCircle)
43-
{
44-
var radius = GetFastValue(options, 'radius', Math.max(this.width, this.height) / 2);
43+
var shape = GetFastValue(options, 'shape', null);
4544

46-
this.body = Bodies.circle(x, y, radius, options);
47-
}
48-
else
45+
if (!shape)
4946
{
5047
this.body = Bodies.rectangle(x, y, this.width, this.height, options);
51-
}
5248

53-
this.body.gameObject = this;
49+
this.body.gameObject = this;
5450

55-
this.world = world;
56-
57-
if (GetFastValue(options, 'addToWorld', true))
51+
if (GetFastValue(options, 'addToWorld', true))
52+
{
53+
world.add(this.body);
54+
}
55+
}
56+
else
5857
{
59-
world.add(this.body);
58+
this.setBody(shape, options);
6059
}
6160

6261
this.setPosition(x, y);

v3/src/physics/matter-js/MatterSprite.js

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ var MatterSprite = new Class({
1919
Components.Gravity,
2020
Components.Mass,
2121
Components.Sensor,
22+
Components.SetBody,
2223
Components.Sleep,
2324
Components.Static,
2425
Components.Transform,
@@ -38,28 +39,26 @@ var MatterSprite = new Class({
3839
this.setSizeToFrame();
3940
this.setOrigin();
4041

41-
this._tempVec2 = new Vector2();
42+
this.world = world;
4243

43-
var isCircle = GetFastValue(options, 'isCircle', false);
44+
this._tempVec2 = new Vector2(x, y);
4445

45-
if (isCircle)
46-
{
47-
var radius = GetFastValue(options, 'radius', Math.max(this.width, this.height) / 2);
46+
var shape = GetFastValue(options, 'shape', null);
4847

49-
this.body = Bodies.circle(x, y, radius, options);
50-
}
51-
else
48+
if (!shape)
5249
{
5350
this.body = Bodies.rectangle(x, y, this.width, this.height, options);
54-
}
5551

56-
this.body.gameObject = this;
52+
this.body.gameObject = this;
5753

58-
this.world = world;
59-
60-
if (GetFastValue(options, 'addToWorld', true))
54+
if (GetFastValue(options, 'addToWorld', true))
55+
{
56+
world.add(this.body);
57+
}
58+
}
59+
else
6160
{
62-
world.add(this.body);
61+
this.setBody(shape, options);
6362
}
6463

6564
this.setPosition(x, y);
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
var Bodies = require('../lib/factory/Bodies');
2+
var Body = require('../lib/body/Body');
3+
var GetFastValue = require('../../../utils/object/GetFastValue');
4+
5+
var SetBody = {
6+
7+
setBody: function (config, options)
8+
{
9+
// Existing body? Remove it.
10+
if (this.body)
11+
{
12+
this.world.remove(this.body);
13+
}
14+
15+
if (!config)
16+
{
17+
return this;
18+
}
19+
else
20+
{
21+
var shapeType = GetFastValue(config, 'type', 'rectangle');
22+
var bodyX = GetFastValue(config, 'x', this._tempVec2.x);
23+
var bodyY = GetFastValue(config, 'y', this._tempVec2.y);
24+
var bodyWidth = GetFastValue(config, 'width', this.width);
25+
var bodyHeight = GetFastValue(config, 'height', this.height);
26+
27+
switch (shapeType)
28+
{
29+
case 'rectangle':
30+
this.body = Bodies.rectangle(bodyX, bodyY, bodyWidth, bodyHeight, options);
31+
break;
32+
33+
case 'circle':
34+
var radius = GetFastValue(config, 'radius', Math.max(bodyWidth, bodyHeight) / 2);
35+
var maxSides = GetFastValue(config, 'maxSides', 25);
36+
this.body = Bodies.circle(bodyX, bodyY, radius, options, maxSides);
37+
break;
38+
39+
case 'trapezoid':
40+
var slope = GetFastValue(config, 'slope', 0.5);
41+
this.body = Bodies.trapezoid(bodyX, bodyY, bodyWidth, bodyHeight, slope, options);
42+
break;
43+
44+
case 'polygon':
45+
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);
48+
break;
49+
50+
case 'fromVertices':
51+
case 'fromVerts':
52+
var verts = GetFastValue(config, 'verts', []);
53+
54+
if (this.body)
55+
{
56+
Body.setVertices(this.body, verts);
57+
}
58+
else
59+
{
60+
var flagInternal = GetFastValue(config, 'flagInternal', false);
61+
var removeCollinear = GetFastValue(config, 'removeCollinear', 0.01);
62+
var minimumArea = GetFastValue(config, 'minimumArea', 10);
63+
this.body = Bodies.fromVertices(bodyX, bodyY, verts, options, flagInternal, removeCollinear, minimumArea);
64+
}
65+
break;
66+
}
67+
}
68+
69+
this.body.gameObject = this;
70+
71+
if (GetFastValue(config, 'addToWorld', true))
72+
{
73+
this.world.add(this.body);
74+
}
75+
76+
return this;
77+
}
78+
79+
};
80+
81+
module.exports = SetBody;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ module.exports = {
1010
Mass: require('./Mass'),
1111
Static: require('./Static'),
1212
Sensor: require('./Sensor'),
13+
SetBody: require('./SetBody'),
1314
Sleep: require('./Sleep'),
1415
Transform: require('./Transform'),
1516
Velocity: require('./Velocity')

0 commit comments

Comments
 (0)