0
0
Fork 0
mirror of https://github.com/liabru/matter-js.git synced 2024-11-27 09:50:52 -05:00
liabru-matter-js/examples/doublePendulum.js

156 lines
3.9 KiB
JavaScript
Raw Normal View History

2017-05-03 19:33:21 -04:00
var Example = Example || {};
Example.doublePendulum = function() {
var Engine = Matter.Engine,
Events = Matter.Events,
Render = Matter.Render,
Runner = Matter.Runner,
Body = Matter.Body,
Composite = Matter.Composite,
Composites = Matter.Composites,
Constraint = Matter.Constraint,
MouseConstraint = Matter.MouseConstraint,
Mouse = Matter.Mouse,
Bodies = Matter.Bodies,
Vector = Matter.Vector;
// 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,
2020-12-08 18:57:14 -05:00
wireframes: false
2017-05-03 19:33:21 -04:00
}
});
Render.run(render);
// create runner
var runner = Runner.create();
Runner.run(runner, engine);
// add bodies
var group = Body.nextGroup(true),
length = 200,
width = 25;
2017-06-27 19:21:17 -04:00
var pendulum = Composites.stack(350, 160, 2, 1, -20, 0, function(x, y) {
2017-05-03 19:33:21 -04:00
return Bodies.rectangle(x, y, length, width, {
collisionFilter: { group: group },
frictionAir: 0,
chamfer: 5,
render: {
fillStyle: 'transparent',
lineWidth: 1
}
});
});
engine.gravity.scale = 0.002;
2017-05-03 19:33:21 -04:00
2017-06-27 19:21:17 -04:00
Composites.chain(pendulum, 0.45, 0, -0.45, 0, {
2017-07-06 16:53:31 -04:00
stiffness: 0.9,
2017-05-03 19:33:21 -04:00
length: 0,
angularStiffness: 0.7,
render: {
2017-06-27 19:21:17 -04:00
strokeStyle: '#4a485b'
2017-05-03 19:33:21 -04:00
}
});
Composite.add(pendulum, Constraint.create({
bodyB: pendulum.bodies[0],
2017-06-27 19:21:17 -04:00
pointB: { x: -length * 0.42, y: 0 },
pointA: { x: pendulum.bodies[0].position.x - length * 0.42, y: pendulum.bodies[0].position.y },
2017-07-06 16:53:31 -04:00
stiffness: 0.9,
2017-05-03 19:33:21 -04:00
length: 0,
render: {
2017-06-27 19:21:17 -04:00
strokeStyle: '#4a485b'
2017-05-03 19:33:21 -04:00
}
}));
var lowerArm = pendulum.bodies[1];
Body.rotate(lowerArm, -Math.PI * 0.3, {
x: lowerArm.position.x - 100,
y: lowerArm.position.y
});
Composite.add(world, pendulum);
2017-05-03 19:33:21 -04:00
var trail = [];
Events.on(render, 'afterRender', function() {
trail.unshift({
position: Vector.clone(lowerArm.position),
speed: lowerArm.speed
});
Render.startViewTransform(render);
render.context.globalAlpha = 0.7;
for (var i = 0; i < trail.length; i += 1) {
var point = trail[i].position,
speed = trail[i].speed;
var hue = 250 + Math.round((1 - Math.min(1, speed / 10)) * 170);
render.context.fillStyle = 'hsl(' + hue + ', 100%, 55%)';
render.context.fillRect(point.x, point.y, 2, 2);
}
render.context.globalAlpha = 1;
Render.endViewTransform(render);
if (trail.length > 2000) {
trail.pop();
}
});
// add mouse control
var mouse = Mouse.create(render.canvas),
mouseConstraint = MouseConstraint.create(engine, {
mouse: mouse,
constraint: {
stiffness: 0.2,
render: {
visible: false
}
}
});
Composite.add(world, mouseConstraint);
2017-05-03 19:33:21 -04: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.doublePendulum.title = 'Double Pendulum';
Example.doublePendulum.for = '>0.16.1';
2020-12-30 18:18:38 -05:00
if (typeof module !== 'undefined') {
2020-12-30 18:18:38 -05:00
module.exports = Example.doublePendulum;
}