diff --git a/examples/newtonsCradle.js b/examples/newtonsCradle.js index e45f94b..6e08aca 100644 --- a/examples/newtonsCradle.js +++ b/examples/newtonsCradle.js @@ -31,12 +31,12 @@ Example.newtonsCradle = function() { var runner = Runner.create(); Runner.run(runner, engine); - // add bodies - var cradle = Composites.newtonsCradle(280, 100, 5, 30, 200); + // see newtonsCradle function defined later in this file + var cradle = Example.newtonsCradle.newtonsCradle(280, 100, 5, 30, 200); World.add(world, cradle); Body.translate(cradle.bodies[0], { x: -180, y: -100 }); - cradle = Composites.newtonsCradle(280, 380, 7, 20, 140); + cradle = Example.newtonsCradle.newtonsCradle(280, 380, 7, 20, 140); World.add(world, cradle); Body.translate(cradle.bodies[0], { x: -140, y: -100 }); @@ -79,6 +79,36 @@ Example.newtonsCradle = function() { Example.newtonsCradle.title = 'Newton\'s Cradle'; Example.newtonsCradle.for = '>=0.14.2'; +/** +* Creates a composite with a Newton's Cradle setup of bodies and constraints. +* @method newtonsCradle +* @param {number} xx +* @param {number} yy +* @param {number} number +* @param {number} size +* @param {number} length +* @return {composite} A new composite newtonsCradle body +*/ +Example.newtonsCradle.newtonsCradle = function(xx, yy, number, size, length) { + var Composite = Matter.Composite, + Constraint = Matter.Constraint, + Bodies = Matter.Bodies; + + var newtonsCradle = Composite.create({ label: 'Newtons Cradle' }); + + for (var i = 0; i < number; i++) { + var separation = 1.9, + circle = Bodies.circle(xx + i * (size * separation), yy + length, size, + { inertia: Infinity, restitution: 1, friction: 0, frictionAir: 0.0001, slop: 1 }), + constraint = Constraint.create({ pointA: { x: xx + i * (size * separation), y: yy }, bodyB: circle }); + + Composite.addBody(newtonsCradle, circle); + Composite.addConstraint(newtonsCradle, constraint); + } + + return newtonsCradle; +}; + if (typeof module !== 'undefined') { module.exports = Example.newtonsCradle; } diff --git a/src/factory/Composites.js b/src/factory/Composites.js index b9d8633..bd0ce71 100644 --- a/src/factory/Composites.js +++ b/src/factory/Composites.js @@ -204,6 +204,7 @@ var deprecated = Common.deprecated; /** * Creates a composite with a Newton's Cradle setup of bodies and constraints. + * @deprecated moved to newtonsCradle example * @method newtonsCradle * @param {number} xx * @param {number} yy @@ -228,6 +229,8 @@ var deprecated = Common.deprecated; return newtonsCradle; }; + deprecated(Composites, 'newtonsCradle', 'Composites.newtonsCradle ➤ moved to newtonsCradle example'); + /** * Creates a composite with simple car setup of bodies and constraints. * @deprecated moved to car example