diff --git a/v3/src/physics/matter-js/MatterImage.js b/v3/src/physics/matter-js/MatterImage.js index a8707e353..9f0f56019 100644 --- a/v3/src/physics/matter-js/MatterImage.js +++ b/v3/src/physics/matter-js/MatterImage.js @@ -18,6 +18,7 @@ var MatterImage = new Class({ Components.Gravity, Components.Mass, Components.Sensor, + Components.SetBody, Components.Sleep, Components.Static, Components.Transform, @@ -35,28 +36,26 @@ var MatterImage = new Class({ this.setSizeToFrame(); this.setOrigin(); - this._tempVec2 = new Vector2(); + this.world = world; - var isCircle = GetFastValue(options, 'isCircle', false); + this._tempVec2 = new Vector2(x, y); - if (isCircle) + var shape = GetFastValue(options, 'shape', null); + + if (!shape) { - var radius = GetFastValue(options, 'radius', Math.max(this.width, this.height) / 2); + this.body = Bodies.rectangle(x, y, this.width, this.height, options); - this.body = Bodies.circle(x, y, radius, options); + this.body.gameObject = this; + + if (GetFastValue(options, 'addToWorld', true)) + { + world.add(this.body); + } } else { - this.body = Bodies.rectangle(x, y, this.width, this.height, options); - } - - this.body.gameObject = this; - - this.world = world; - - if (GetFastValue(options, 'addToWorld', true)) - { - world.add(this.body); + this.setBody(shape, options); } this.setPosition(x, y); diff --git a/v3/src/physics/matter-js/MatterSprite.js b/v3/src/physics/matter-js/MatterSprite.js index 4fc06e32c..45265b195 100644 --- a/v3/src/physics/matter-js/MatterSprite.js +++ b/v3/src/physics/matter-js/MatterSprite.js @@ -19,6 +19,7 @@ var MatterSprite = new Class({ Components.Gravity, Components.Mass, Components.Sensor, + Components.SetBody, Components.Sleep, Components.Static, Components.Transform, @@ -38,28 +39,26 @@ var MatterSprite = new Class({ this.setSizeToFrame(); this.setOrigin(); - this._tempVec2 = new Vector2(); + this.world = world; - var isCircle = GetFastValue(options, 'isCircle', false); + this._tempVec2 = new Vector2(x, y); - if (isCircle) + var shape = GetFastValue(options, 'shape', null); + + if (!shape) { - var radius = GetFastValue(options, 'radius', Math.max(this.width, this.height) / 2); + this.body = Bodies.rectangle(x, y, this.width, this.height, options); - this.body = Bodies.circle(x, y, radius, options); + this.body.gameObject = this; + + if (GetFastValue(options, 'addToWorld', true)) + { + world.add(this.body); + } } else { - this.body = Bodies.rectangle(x, y, this.width, this.height, options); - } - - this.body.gameObject = this; - - this.world = world; - - if (GetFastValue(options, 'addToWorld', true)) - { - world.add(this.body); + this.setBody(shape, options); } this.setPosition(x, y); diff --git a/v3/src/physics/matter-js/components/SetBody.js b/v3/src/physics/matter-js/components/SetBody.js new file mode 100644 index 000000000..47546e7e6 --- /dev/null +++ b/v3/src/physics/matter-js/components/SetBody.js @@ -0,0 +1,81 @@ +var Bodies = require('../lib/factory/Bodies'); +var Body = require('../lib/body/Body'); +var GetFastValue = require('../../../utils/object/GetFastValue'); + +var SetBody = { + + setBody: function (config, options) + { + // Existing body? Remove it. + if (this.body) + { + this.world.remove(this.body); + } + + if (!config) + { + return this; + } + else + { + var shapeType = GetFastValue(config, 'type', 'rectangle'); + var bodyX = GetFastValue(config, 'x', this._tempVec2.x); + var bodyY = GetFastValue(config, 'y', this._tempVec2.y); + var bodyWidth = GetFastValue(config, 'width', this.width); + var bodyHeight = GetFastValue(config, 'height', this.height); + + switch (shapeType) + { + case 'rectangle': + this.body = Bodies.rectangle(bodyX, bodyY, bodyWidth, bodyHeight, options); + break; + + case 'circle': + var radius = GetFastValue(config, 'radius', Math.max(bodyWidth, bodyHeight) / 2); + var maxSides = GetFastValue(config, 'maxSides', 25); + this.body = Bodies.circle(bodyX, bodyY, radius, options, maxSides); + break; + + case 'trapezoid': + var slope = GetFastValue(config, 'slope', 0.5); + this.body = Bodies.trapezoid(bodyX, bodyY, bodyWidth, bodyHeight, slope, options); + break; + + case 'polygon': + var sides = GetFastValue(config, 'sides', 5); + var radius = GetFastValue(config, 'radius', Math.max(bodyWidth, bodyHeight) / 2); + this.body = Bodies.polygon(bodyX, bodyY, sides, radius, options); + break; + + case 'fromVertices': + case 'fromVerts': + var verts = GetFastValue(config, 'verts', []); + + if (this.body) + { + Body.setVertices(this.body, verts); + } + else + { + var flagInternal = GetFastValue(config, 'flagInternal', false); + var removeCollinear = GetFastValue(config, 'removeCollinear', 0.01); + var minimumArea = GetFastValue(config, 'minimumArea', 10); + this.body = Bodies.fromVertices(bodyX, bodyY, verts, options, flagInternal, removeCollinear, minimumArea); + } + break; + } + } + + this.body.gameObject = this; + + if (GetFastValue(config, 'addToWorld', true)) + { + this.world.add(this.body); + } + + return this; + } + +}; + +module.exports = SetBody; diff --git a/v3/src/physics/matter-js/components/index.js b/v3/src/physics/matter-js/components/index.js index 85058553f..dcb5673fe 100644 --- a/v3/src/physics/matter-js/components/index.js +++ b/v3/src/physics/matter-js/components/index.js @@ -10,6 +10,7 @@ module.exports = { Mass: require('./Mass'), Static: require('./Static'), Sensor: require('./Sensor'), + SetBody: require('./SetBody'), Sleep: require('./Sleep'), Transform: require('./Transform'), Velocity: require('./Velocity')