Skip to content

Commit

Permalink
deprecated Composites.car and added to car example
Browse files Browse the repository at this point in the history
  • Loading branch information
liabru committed Apr 6, 2021
1 parent 313c150 commit cd9c5d4
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 3 deletions.
77 changes: 75 additions & 2 deletions examples/car.js
Expand Up @@ -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' }}),
Expand Down Expand Up @@ -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;
}
6 changes: 5 additions & 1 deletion src/factory/Composites.js
Expand Up @@ -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() {

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit cd9c5d4

Please sign in to comment.