From 818f354e9c19c268f7d3d1693d781a058aebb76b Mon Sep 17 00:00:00 2001 From: liabru Date: Tue, 6 Apr 2021 21:18:57 +0100 Subject: [PATCH] deprecated Composites.softBody and added to softBody and cloth examples --- examples/cloth.js | 43 ++++++++++++++++++++++++++++++++++----- examples/softBody.js | 41 ++++++++++++++++++++++++++++++++++--- src/factory/Composites.js | 2 ++ 3 files changed, 78 insertions(+), 8 deletions(-) diff --git a/examples/cloth.js b/examples/cloth.js index 320e2ab..9ff9ea8 100644 --- a/examples/cloth.js +++ b/examples/cloth.js @@ -31,11 +31,8 @@ Example.cloth = function() { var runner = Runner.create(); Runner.run(runner, engine); - // add bodies - var group = Body.nextGroup(true), - particleOptions = { friction: 0.00001, collisionFilter: { group: group }, render: { visible: false }}, - constraintOptions = { stiffness: 0.06 }, - cloth = Composites.softBody(200, 200, 20, 12, 5, 5, false, 8, particleOptions, constraintOptions); + // see cloth function defined later in this file + var cloth = Example.cloth.cloth(200, 200, 20, 12, 5, 5, false, 8); for (var i = 0; i < 20; i++) { cloth.bodies[i].isStatic = true; @@ -87,6 +84,42 @@ Example.cloth = function() { Example.cloth.title = 'Cloth'; Example.cloth.for = '>=0.14.2'; +/** +* Creates a simple cloth like object. +* @method cloth +* @param {number} xx +* @param {number} yy +* @param {number} columns +* @param {number} rows +* @param {number} columnGap +* @param {number} rowGap +* @param {boolean} crossBrace +* @param {number} particleRadius +* @param {} particleOptions +* @param {} constraintOptions +* @return {composite} A new composite cloth +*/ +Example.cloth.cloth = function(xx, yy, columns, rows, columnGap, rowGap, crossBrace, particleRadius, particleOptions, constraintOptions) { + var Body = Matter.Body, + Bodies = Matter.Bodies, + Common = Matter.Common, + Composites = Matter.Composites; + + var group = Body.nextGroup(true); + particleOptions = Common.extend({ inertia: Infinity, friction: 0.00001, collisionFilter: { group: group }, render: { visible: false }}, particleOptions); + constraintOptions = Common.extend({ stiffness: 0.06, render: { type: 'line', anchors: false } }, constraintOptions); + + var cloth = Composites.stack(xx, yy, columns, rows, columnGap, rowGap, function(x, y) { + return Bodies.circle(x, y, particleRadius, particleOptions); + }); + + Composites.mesh(cloth, columns, rows, crossBrace, constraintOptions); + + cloth.label = 'Cloth Body'; + + return cloth; +}; + if (typeof module !== 'undefined') { module.exports = Example.cloth; } diff --git a/examples/softBody.js b/examples/softBody.js index fb8d4d7..226df0b 100644 --- a/examples/softBody.js +++ b/examples/softBody.js @@ -39,9 +39,10 @@ Example.softBody = function() { }; World.add(world, [ - Composites.softBody(250, 100, 5, 5, 0, 0, true, 18, particleOptions), - Composites.softBody(400, 300, 8, 3, 0, 0, true, 15, particleOptions), - Composites.softBody(250, 400, 4, 4, 0, 0, true, 15, particleOptions), + // see softBody function defined later in this file + Example.softBody.softBody(250, 100, 5, 5, 0, 0, true, 18, particleOptions), + Example.softBody.softBody(400, 300, 8, 3, 0, 0, true, 15, particleOptions), + Example.softBody.softBody(250, 400, 4, 4, 0, 0, true, 15, particleOptions), // walls Bodies.rectangle(400, 0, 800, 50, { isStatic: true }), Bodies.rectangle(400, 600, 800, 50, { isStatic: true }), @@ -88,6 +89,40 @@ Example.softBody = function() { Example.softBody.title = 'Soft Body'; Example.softBody.for = '>=0.14.2'; +/** +* Creates a simple soft body like object. +* @method softBody +* @param {number} xx +* @param {number} yy +* @param {number} columns +* @param {number} rows +* @param {number} columnGap +* @param {number} rowGap +* @param {boolean} crossBrace +* @param {number} particleRadius +* @param {} particleOptions +* @param {} constraintOptions +* @return {composite} A new composite softBody +*/ +Example.softBody.softBody = function(xx, yy, columns, rows, columnGap, rowGap, crossBrace, particleRadius, particleOptions, constraintOptions) { + var Common = Matter.Common, + Composites = Matter.Composites, + Bodies = Matter.Bodies; + + particleOptions = Common.extend({ inertia: Infinity }, particleOptions); + constraintOptions = Common.extend({ stiffness: 0.2, render: { type: 'line', anchors: false } }, constraintOptions); + + var softBody = Composites.stack(xx, yy, columns, rows, columnGap, rowGap, function(x, y) { + return Bodies.circle(x, y, particleRadius, particleOptions); + }); + + Composites.mesh(softBody, columns, rows, crossBrace, constraintOptions); + + softBody.label = 'Soft Body'; + + return softBody; +}; + if (typeof module !== 'undefined') { module.exports = Example.softBody; } diff --git a/src/factory/Composites.js b/src/factory/Composites.js index 6b23d02..b9d8633 100644 --- a/src/factory/Composites.js +++ b/src/factory/Composites.js @@ -300,6 +300,7 @@ var deprecated = Common.deprecated; /** * Creates a simple soft body like object. + * @deprecated moved to softBody and cloth examples * @method softBody * @param {number} xx * @param {number} yy @@ -328,4 +329,5 @@ var deprecated = Common.deprecated; return softBody; }; + deprecated(Composites, 'softBody', 'Composites.softBody ➤ moved to softBody and cloth examples'); })();