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.manipulation = function() {
|
|
|
|
var Engine = Matter.Engine,
|
|
|
|
Render = Matter.Render,
|
|
|
|
Runner = Matter.Runner,
|
2015-08-25 14:12:52 -04:00
|
|
|
Body = Matter.Body,
|
2016-11-20 19:41:57 -05:00
|
|
|
Events = Matter.Events,
|
|
|
|
MouseConstraint = Matter.MouseConstraint,
|
|
|
|
Mouse = Matter.Mouse,
|
2021-04-06 16:36:43 -04:00
|
|
|
Composite = Matter.Composite,
|
2016-11-20 19:41:57 -05:00
|
|
|
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
|
|
|
showAxes: true,
|
|
|
|
showCollisions: true,
|
|
|
|
showConvexHulls: true
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Render.run(render);
|
|
|
|
|
|
|
|
// create runner
|
|
|
|
var runner = Runner.create();
|
|
|
|
Runner.run(runner, engine);
|
|
|
|
|
|
|
|
// add bodies
|
2021-12-31 06:06:49 -05:00
|
|
|
var bodyA = Bodies.rectangle(100, 300, 50, 50, { isStatic: true, render: { fillStyle: '#060a19' } }),
|
2016-11-20 19:41:57 -05:00
|
|
|
bodyB = Bodies.rectangle(200, 200, 50, 50),
|
|
|
|
bodyC = Bodies.rectangle(300, 200, 50, 50),
|
|
|
|
bodyD = Bodies.rectangle(400, 200, 50, 50),
|
|
|
|
bodyE = Bodies.rectangle(550, 200, 50, 50),
|
|
|
|
bodyF = Bodies.rectangle(700, 200, 50, 50),
|
2021-12-31 06:06:49 -05:00
|
|
|
bodyG = Bodies.circle(400, 100, 25, { render: { fillStyle: '#060a19' } });
|
|
|
|
|
|
|
|
// add compound body
|
|
|
|
var partA = Bodies.rectangle(600, 200, 120 * 0.8, 50 * 0.8, { render: { fillStyle: '#060a19' } }),
|
2020-12-31 12:29:37 -05:00
|
|
|
partB = Bodies.rectangle(660, 200, 50 * 0.8, 190 * 0.8, { render: { fillStyle: '#060a19' } }),
|
2016-11-20 19:41:57 -05:00
|
|
|
compound = Body.create({
|
|
|
|
parts: [partA, partB],
|
|
|
|
isStatic: true
|
|
|
|
});
|
|
|
|
|
2021-12-31 06:06:49 -05:00
|
|
|
Body.setPosition(compound, { x: 600, y: 300 });
|
|
|
|
|
2021-04-06 16:36:43 -04:00
|
|
|
Composite.add(world, [bodyA, bodyB, bodyC, bodyD, bodyE, bodyF, bodyG, compound]);
|
2016-11-20 19:41:57 -05:00
|
|
|
|
2021-04-06 16:36:43 -04:00
|
|
|
Composite.add(world, [
|
2016-11-27 20:13:22 -05:00
|
|
|
// walls
|
|
|
|
Bodies.rectangle(400, 0, 800, 50, { isStatic: true }),
|
2016-11-20 19:41:57 -05:00
|
|
|
Bodies.rectangle(400, 600, 800, 50, { isStatic: true }),
|
|
|
|
Bodies.rectangle(800, 300, 50, 600, { isStatic: true }),
|
|
|
|
Bodies.rectangle(0, 300, 50, 600, { isStatic: true })
|
|
|
|
]);
|
|
|
|
|
2019-09-01 08:36:16 -04:00
|
|
|
var lastTime = 0,
|
|
|
|
scaleRate = 0.6;
|
2016-11-20 19:41:57 -05:00
|
|
|
|
|
|
|
Events.on(engine, 'beforeUpdate', function(event) {
|
2020-03-11 19:47:07 -04:00
|
|
|
var timeScale = (event.delta || (1000 / 60)) / 1000;
|
2016-11-20 19:41:57 -05:00
|
|
|
|
2019-09-01 08:36:16 -04:00
|
|
|
if (scaleRate > 0) {
|
|
|
|
Body.scale(bodyF, 1 + (scaleRate * timeScale), 1 + (scaleRate * timeScale));
|
2016-11-20 19:41:57 -05:00
|
|
|
|
|
|
|
// modify bodyE vertices
|
2019-09-01 08:36:16 -04:00
|
|
|
bodyE.vertices[0].x -= 0.2 * timeScale;
|
|
|
|
bodyE.vertices[0].y -= 0.2 * timeScale;
|
|
|
|
bodyE.vertices[1].x += 0.2 * timeScale;
|
|
|
|
bodyE.vertices[1].y -= 0.2 * timeScale;
|
2016-11-20 19:41:57 -05:00
|
|
|
Body.setVertices(bodyE, bodyE.vertices);
|
|
|
|
}
|
|
|
|
|
|
|
|
// make bodyA move up and down
|
|
|
|
var py = 300 + 100 * Math.sin(engine.timing.timestamp * 0.002);
|
2021-12-31 06:06:49 -05:00
|
|
|
|
|
|
|
// manual update velocity required for older releases
|
|
|
|
if (Matter.version === '0.18.0') {
|
|
|
|
Body.setVelocity(bodyA, { x: 0, y: py - bodyA.position.y });
|
|
|
|
Body.setVelocity(compound, { x: 0, y: py - compound.position.y });
|
|
|
|
Body.setAngularVelocity(compound, 1 * Math.PI * timeScale);
|
|
|
|
}
|
|
|
|
|
|
|
|
// move body and update velocity
|
2019-09-01 08:36:16 -04:00
|
|
|
Body.setPosition(bodyA, { x: 100, y: py }, true);
|
2016-11-20 19:41:57 -05:00
|
|
|
|
2021-12-31 06:06:49 -05:00
|
|
|
// move compound body move up and down and update velocity
|
2019-09-01 08:36:16 -04:00
|
|
|
Body.setPosition(compound, { x: 600, y: py }, true);
|
2021-12-31 06:06:49 -05:00
|
|
|
|
|
|
|
// rotate compound body and update angular velocity
|
2019-09-01 08:36:16 -04:00
|
|
|
Body.rotate(compound, 1 * Math.PI * timeScale, null, true);
|
|
|
|
|
|
|
|
// after first 0.8 sec (simulation time)
|
|
|
|
if (engine.timing.timestamp >= 800)
|
|
|
|
Body.setStatic(bodyG, true);
|
2016-11-20 19:41:57 -05:00
|
|
|
|
2019-09-01 08:36:16 -04:00
|
|
|
// every 1.5 sec (simulation time)
|
|
|
|
if (engine.timing.timestamp - lastTime >= 1500) {
|
2016-11-20 19:41:57 -05:00
|
|
|
Body.setVelocity(bodyB, { x: 0, y: -10 });
|
|
|
|
Body.setAngle(bodyC, -Math.PI * 0.26);
|
|
|
|
Body.setAngularVelocity(bodyD, 0.2);
|
|
|
|
|
2019-09-01 08:36:16 -04:00
|
|
|
// stop scaling
|
|
|
|
scaleRate = 0;
|
|
|
|
|
|
|
|
// update last time
|
|
|
|
lastTime = engine.timing.timestamp;
|
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
|
|
|
}
|
|
|
|
});
|
|
|
|
|
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;
|
2015-08-25 14:12:52 -04:00
|
|
|
|
2016-11-20 19:41:57 -05:00
|
|
|
// fit the render viewport to the scene
|
2016-11-27 20:13:22 -05:00
|
|
|
Render.lookAt(render, {
|
|
|
|
min: { x: 0, y: 0 },
|
|
|
|
max: { x: 800, y: 600 }
|
|
|
|
});
|
2016-11-20 19:41:57 -05:00
|
|
|
|
|
|
|
// 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.manipulation.title = 'Manipulation';
|
2020-12-30 18:18:38 -05:00
|
|
|
Example.manipulation.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.manipulation;
|
|
|
|
}
|