2016-11-20 19:41:57 -05:00
|
|
|
var Example = Example || {};
|
2015-08-25 14:12:52 -04:00
|
|
|
|
2016-11-20 19:41:57 -05:00
|
|
|
Example.chains = function() {
|
|
|
|
var Engine = Matter.Engine,
|
|
|
|
Render = Matter.Render,
|
|
|
|
Runner = Matter.Runner,
|
2015-08-25 14:12:52 -04:00
|
|
|
Body = Matter.Body,
|
|
|
|
Composite = Matter.Composite,
|
|
|
|
Composites = Matter.Composites,
|
2016-11-20 19:41:57 -05:00
|
|
|
Constraint = Matter.Constraint,
|
|
|
|
MouseConstraint = Matter.MouseConstraint,
|
|
|
|
Mouse = Matter.Mouse,
|
|
|
|
Bodies = Matter.Bodies;
|
|
|
|
|
|
|
|
// create engine
|
|
|
|
var engine = Engine.create(),
|
|
|
|
world = engine.world;
|
|
|
|
|
|
|
|
// create renderer
|
|
|
|
var render = Render.create({
|
|
|
|
element: document.body,
|
|
|
|
engine: engine,
|
|
|
|
options: {
|
2017-07-08 08:28:19 -04:00
|
|
|
width: 800,
|
|
|
|
height: 600,
|
2016-11-20 19:41:57 -05:00
|
|
|
showAngleIndicator: true,
|
|
|
|
showCollisions: true,
|
|
|
|
showVelocity: true
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Render.run(render);
|
|
|
|
|
|
|
|
// create runner
|
|
|
|
var runner = Runner.create();
|
|
|
|
Runner.run(runner, engine);
|
|
|
|
|
|
|
|
// add bodies
|
|
|
|
var group = Body.nextGroup(true);
|
2015-08-25 14:12:52 -04:00
|
|
|
|
2017-05-03 16:31:29 -04:00
|
|
|
var ropeA = Composites.stack(100, 50, 8, 1, 10, 10, function(x, y) {
|
2016-11-20 19:41:57 -05:00
|
|
|
return Bodies.rectangle(x, y, 50, 20, { collisionFilter: { group: group } });
|
|
|
|
});
|
|
|
|
|
2017-06-27 19:21:17 -04:00
|
|
|
Composites.chain(ropeA, 0.5, 0, -0.5, 0, { stiffness: 0.8, length: 2, render: { type: 'line' } });
|
2016-11-20 19:41:57 -05:00
|
|
|
Composite.add(ropeA, Constraint.create({
|
|
|
|
bodyB: ropeA.bodies[0],
|
|
|
|
pointB: { x: -25, y: 0 },
|
2017-05-03 16:31:29 -04:00
|
|
|
pointA: { x: ropeA.bodies[0].position.x, y: ropeA.bodies[0].position.y },
|
2016-11-20 19:41:57 -05:00
|
|
|
stiffness: 0.5
|
|
|
|
}));
|
|
|
|
|
|
|
|
group = Body.nextGroup(true);
|
|
|
|
|
2017-05-03 16:31:29 -04:00
|
|
|
var ropeB = Composites.stack(350, 50, 10, 1, 10, 10, function(x, y) {
|
2016-11-20 19:41:57 -05:00
|
|
|
return Bodies.circle(x, y, 20, { collisionFilter: { group: group } });
|
|
|
|
});
|
|
|
|
|
2017-06-27 19:21:17 -04:00
|
|
|
Composites.chain(ropeB, 0.5, 0, -0.5, 0, { stiffness: 0.8, length: 2, render: { type: 'line' } });
|
2016-11-20 19:41:57 -05:00
|
|
|
Composite.add(ropeB, Constraint.create({
|
|
|
|
bodyB: ropeB.bodies[0],
|
|
|
|
pointB: { x: -20, y: 0 },
|
2017-05-03 16:31:29 -04:00
|
|
|
pointA: { x: ropeB.bodies[0].position.x, y: ropeB.bodies[0].position.y },
|
|
|
|
stiffness: 0.5
|
|
|
|
}));
|
|
|
|
|
|
|
|
group = Body.nextGroup(true);
|
|
|
|
|
|
|
|
var ropeC = Composites.stack(600, 50, 13, 1, 10, 10, function(x, y) {
|
|
|
|
return Bodies.rectangle(x - 20, y, 50, 20, { collisionFilter: { group: group }, chamfer: 5 });
|
|
|
|
});
|
|
|
|
|
|
|
|
Composites.chain(ropeC, 0.3, 0, -0.3, 0, { stiffness: 1, length: 0 });
|
|
|
|
Composite.add(ropeC, Constraint.create({
|
|
|
|
bodyB: ropeC.bodies[0],
|
|
|
|
pointB: { x: -20, y: 0 },
|
|
|
|
pointA: { x: ropeC.bodies[0].position.x, y: ropeC.bodies[0].position.y },
|
2016-11-20 19:41:57 -05:00
|
|
|
stiffness: 0.5
|
|
|
|
}));
|
|
|
|
|
2021-04-06 16:36:43 -04:00
|
|
|
Composite.add(world, [
|
2017-05-03 16:31:29 -04:00
|
|
|
ropeA,
|
|
|
|
ropeB,
|
|
|
|
ropeC,
|
|
|
|
Bodies.rectangle(400, 600, 1200, 50.5, { isStatic: true })
|
|
|
|
]);
|
2016-11-20 19:41:57 -05:00
|
|
|
|
|
|
|
// add mouse control
|
|
|
|
var mouse = Mouse.create(render.canvas),
|
|
|
|
mouseConstraint = MouseConstraint.create(engine, {
|
|
|
|
mouse: mouse,
|
|
|
|
constraint: {
|
|
|
|
stiffness: 0.2,
|
|
|
|
render: {
|
|
|
|
visible: false
|
|
|
|
}
|
|
|
|
}
|
2015-08-25 14:12:52 -04:00
|
|
|
});
|
|
|
|
|
2021-04-06 16:36:43 -04:00
|
|
|
Composite.add(world, mouseConstraint);
|
2016-11-20 19:41:57 -05:00
|
|
|
|
|
|
|
// keep the mouse in sync with rendering
|
|
|
|
render.mouse = mouse;
|
|
|
|
|
|
|
|
// fit the render viewport to the scene
|
|
|
|
Render.lookAt(render, {
|
|
|
|
min: { x: 0, y: 0 },
|
|
|
|
max: { x: 700, y: 600 }
|
|
|
|
});
|
|
|
|
|
|
|
|
// context for MatterTools.Demo
|
|
|
|
return {
|
|
|
|
engine: engine,
|
|
|
|
runner: runner,
|
|
|
|
render: render,
|
|
|
|
canvas: render.canvas,
|
|
|
|
stop: function() {
|
|
|
|
Matter.Render.stop(render);
|
|
|
|
Matter.Runner.stop(runner);
|
|
|
|
}
|
|
|
|
};
|
2019-09-14 13:54:50 -04:00
|
|
|
};
|
|
|
|
|
2021-02-07 18:56:08 -05:00
|
|
|
Example.chains.title = 'Chains';
|
2020-12-30 18:18:38 -05:00
|
|
|
Example.chains.for = '>=0.14.2';
|
|
|
|
|
2019-09-15 13:36:26 -04:00
|
|
|
if (typeof module !== 'undefined') {
|
2020-12-30 18:18:38 -05:00
|
|
|
module.exports = Example.chains;
|
|
|
|
}
|