0
0
Fork 0
mirror of https://github.com/liabru/matter-js.git synced 2024-11-23 09:26:51 -05:00
liabru-matter-js/examples/chains.js

131 lines
3.6 KiB
JavaScript
Raw Normal View History

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,
World = Matter.World,
Bodies = Matter.Bodies;
// create engine
var engine = Engine.create(),
world = engine.world;
// create renderer
var render = Render.create({
element: document.body,
engine: engine,
options: {
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
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 },
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);
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 },
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
}));
World.add(world, [
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
});
2016-11-20 19:41:57 -05:00
World.add(world, mouseConstraint);
// 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';
if (typeof module !== 'undefined') {
2020-12-30 18:18:38 -05:00
module.exports = Example.chains;
}