From cd9c5d43dbafc011fed15b15b6e326a6a3aef4da Mon Sep 17 00:00:00 2001 From: liabru Date: Tue, 6 Apr 2021 21:13:27 +0100 Subject: [PATCH] deprecated Composites.car and added to car example --- examples/car.js | 77 ++++++++++++++++++++++++++++++++++++++- src/factory/Composites.js | 6 ++- 2 files changed, 80 insertions(+), 3 deletions(-) diff --git a/examples/car.js b/examples/car.js index 179d0bb..522fc42 100644 --- a/examples/car.js +++ b/examples/car.js @@ -41,11 +41,12 @@ Example.car = function() { Bodies.rectangle(0, 300, 50, 600, { isStatic: true }) ]); + // see car function defined later in this file var scale = 0.9; - World.add(world, Composites.car(150, 100, 150 * scale, 30 * scale, 30 * scale)); + World.add(world, Example.car.car(150, 100, 150 * scale, 30 * scale, 30 * scale)); scale = 0.8; - World.add(world, Composites.car(350, 300, 150 * scale, 30 * scale, 30 * scale)); + World.add(world, Example.car.car(350, 300, 150 * scale, 30 * scale, 30 * scale)); World.add(world, [ Bodies.rectangle(200, 150, 400, 20, { isStatic: true, angle: Math.PI * 0.06, render: { fillStyle: '#060a19' }}), @@ -92,6 +93,78 @@ Example.car = function() { Example.car.title = 'Car'; Example.car.for = '>=0.14.2'; +/** +* Creates a composite with simple car setup of bodies and constraints. +* @method car +* @param {number} xx +* @param {number} yy +* @param {number} width +* @param {number} height +* @param {number} wheelSize +* @return {composite} A new composite car body +*/ +Example.car.car = function(xx, yy, width, height, wheelSize) { + var Body = Matter.Body, + Bodies = Matter.Bodies, + Composite = Matter.Composite, + Constraint = Matter.Constraint; + + var group = Body.nextGroup(true), + wheelBase = 20, + wheelAOffset = -width * 0.5 + wheelBase, + wheelBOffset = width * 0.5 - wheelBase, + wheelYOffset = 0; + + var car = Composite.create({ label: 'Car' }), + body = Bodies.rectangle(xx, yy, width, height, { + collisionFilter: { + group: group + }, + chamfer: { + radius: height * 0.5 + }, + density: 0.0002 + }); + + var wheelA = Bodies.circle(xx + wheelAOffset, yy + wheelYOffset, wheelSize, { + collisionFilter: { + group: group + }, + friction: 0.8 + }); + + var wheelB = Bodies.circle(xx + wheelBOffset, yy + wheelYOffset, wheelSize, { + collisionFilter: { + group: group + }, + friction: 0.8 + }); + + var axelA = Constraint.create({ + bodyB: body, + pointB: { x: wheelAOffset, y: wheelYOffset }, + bodyA: wheelA, + stiffness: 1, + length: 0 + }); + + var axelB = Constraint.create({ + bodyB: body, + pointB: { x: wheelBOffset, y: wheelYOffset }, + bodyA: wheelB, + stiffness: 1, + length: 0 + }); + + Composite.addBody(car, body); + Composite.addBody(car, wheelA); + Composite.addBody(car, wheelB); + Composite.addConstraint(car, axelA); + Composite.addConstraint(car, axelB); + + return car; +}; + if (typeof module !== 'undefined') { module.exports = Example.car; } diff --git a/src/factory/Composites.js b/src/factory/Composites.js index cc509f0..6b23d02 100644 --- a/src/factory/Composites.js +++ b/src/factory/Composites.js @@ -16,6 +16,7 @@ var Constraint = require('../constraint/Constraint'); var Common = require('../core/Common'); var Body = require('../body/Body'); var Bodies = require('./Bodies'); +var deprecated = Common.deprecated; (function() { @@ -226,9 +227,10 @@ var Bodies = require('./Bodies'); return newtonsCradle; }; - + /** * Creates a composite with simple car setup of bodies and constraints. + * @deprecated moved to car example * @method car * @param {number} xx * @param {number} yy @@ -294,6 +296,8 @@ var Bodies = require('./Bodies'); return car; }; + deprecated(Composites, 'car', 'Composites.car ➤ moved to car example'); + /** * Creates a simple soft body like object. * @method softBody