2016-11-20 19:41:57 -05:00
|
|
|
var Example = Example || {};
|
2015-10-22 05:11:05 -04:00
|
|
|
|
2016-11-20 19:41:57 -05:00
|
|
|
Example.sensors = function() {
|
|
|
|
var Engine = Matter.Engine,
|
|
|
|
Render = Matter.Render,
|
|
|
|
Runner = Matter.Runner,
|
|
|
|
Events = Matter.Events,
|
|
|
|
MouseConstraint = Matter.MouseConstraint,
|
|
|
|
Mouse = Matter.Mouse,
|
|
|
|
World = Matter.World,
|
|
|
|
Bodies = Matter.Bodies;
|
2015-10-22 05:11:05 -04:00
|
|
|
|
2016-11-20 19:41:57 -05:00
|
|
|
// create engine
|
|
|
|
var engine = Engine.create(),
|
|
|
|
world = engine.world;
|
2015-10-22 05:11:05 -04:00
|
|
|
|
2016-11-20 19:41:57 -05:00
|
|
|
// 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
|
|
|
wireframes: false,
|
|
|
|
background: '#111'
|
|
|
|
}
|
|
|
|
});
|
2015-10-22 05:11:05 -04:00
|
|
|
|
2016-11-20 19:41:57 -05:00
|
|
|
Render.run(render);
|
|
|
|
|
|
|
|
// create runner
|
|
|
|
var runner = Runner.create();
|
|
|
|
Runner.run(runner, engine);
|
|
|
|
|
|
|
|
// add bodies
|
|
|
|
var redColor = '#C44D58',
|
|
|
|
greenColor = '#C7F464';
|
|
|
|
|
|
|
|
var collider = Bodies.rectangle(400, 300, 500, 50, {
|
|
|
|
isSensor: true,
|
|
|
|
isStatic: true,
|
|
|
|
render: {
|
|
|
|
strokeStyle: redColor,
|
2017-01-19 18:36:34 -05:00
|
|
|
fillStyle: 'transparent',
|
|
|
|
lineWidth: 1
|
2016-11-20 19:41:57 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
World.add(world, [
|
|
|
|
collider,
|
|
|
|
Bodies.rectangle(400, 620, 800, 50, {
|
2015-10-22 05:11:05 -04:00
|
|
|
isStatic: true,
|
|
|
|
render: {
|
2017-01-19 18:36:34 -05:00
|
|
|
fillStyle: 'transparent',
|
|
|
|
lineWidth: 1
|
2015-10-22 05:11:05 -04:00
|
|
|
}
|
2016-11-20 19:41:57 -05:00
|
|
|
})
|
|
|
|
]);
|
2015-10-22 05:11:05 -04:00
|
|
|
|
2016-11-20 19:41:57 -05:00
|
|
|
World.add(world,
|
|
|
|
Bodies.circle(400, 40, 30, {
|
|
|
|
render: {
|
|
|
|
strokeStyle: greenColor,
|
2017-01-19 18:36:34 -05:00
|
|
|
fillStyle: 'transparent',
|
|
|
|
lineWidth: 1
|
2016-11-20 19:41:57 -05:00
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
Events.on(engine, 'collisionStart', function(event) {
|
|
|
|
var pairs = event.pairs;
|
|
|
|
|
|
|
|
for (var i = 0, j = pairs.length; i != j; ++i) {
|
|
|
|
var pair = pairs[i];
|
|
|
|
|
|
|
|
if (pair.bodyA === collider) {
|
|
|
|
pair.bodyB.render.strokeStyle = redColor;
|
|
|
|
} else if (pair.bodyB === collider) {
|
|
|
|
pair.bodyA.render.strokeStyle = redColor;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2015-10-22 05:11:05 -04:00
|
|
|
|
2016-11-20 19:41:57 -05:00
|
|
|
Events.on(engine, 'collisionEnd', function(event) {
|
|
|
|
var pairs = event.pairs;
|
|
|
|
|
|
|
|
for (var i = 0, j = pairs.length; i != j; ++i) {
|
|
|
|
var pair = pairs[i];
|
|
|
|
|
|
|
|
if (pair.bodyA === collider) {
|
|
|
|
pair.bodyB.render.strokeStyle = greenColor;
|
|
|
|
} else if (pair.bodyB === collider) {
|
|
|
|
pair.bodyA.render.strokeStyle = greenColor;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// add mouse control
|
|
|
|
var mouse = Mouse.create(render.canvas),
|
|
|
|
mouseConstraint = MouseConstraint.create(engine, {
|
|
|
|
mouse: mouse,
|
|
|
|
constraint: {
|
|
|
|
stiffness: 0.2,
|
2015-10-22 05:11:05 -04:00
|
|
|
render: {
|
2016-11-20 19:41:57 -05:00
|
|
|
visible: false
|
2015-10-22 05:11:05 -04:00
|
|
|
}
|
2016-11-20 19:41:57 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
World.add(world, mouseConstraint);
|
|
|
|
|
|
|
|
// keep the mouse in sync with rendering
|
|
|
|
render.mouse = mouse;
|
2017-01-19 18:36:34 -05: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 }
|
|
|
|
});
|
2015-10-22 05:11:05 -04:00
|
|
|
|
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);
|
|
|
|
}
|
2015-10-22 05:11:05 -04:00
|
|
|
};
|
2016-11-20 19:41:57 -05:00
|
|
|
};
|