2015-10-22 05:11:05 -04:00
|
|
|
(function() {
|
|
|
|
|
|
|
|
var World = Matter.World,
|
|
|
|
Bodies = Matter.Bodies,
|
|
|
|
Common = Matter.Common,
|
|
|
|
Events = Matter.Events;
|
|
|
|
|
2015-12-28 15:30:56 -05:00
|
|
|
Example.sensors = function(demo) {
|
2015-10-22 05:11:05 -04:00
|
|
|
var engine = demo.engine,
|
|
|
|
world = engine.world,
|
|
|
|
sceneEvents = demo.sceneEvents;
|
|
|
|
|
|
|
|
var redColor = '#C44D58',
|
|
|
|
greenColor = '#C7F464';
|
|
|
|
|
|
|
|
var collider = Bodies.rectangle(400, 300, 500, 50, {
|
2015-12-28 15:30:56 -05:00
|
|
|
isSensor: true,
|
2015-10-22 05:11:05 -04:00
|
|
|
isStatic: true,
|
|
|
|
render: {
|
|
|
|
strokeStyle: redColor,
|
|
|
|
fillStyle: 'transparent'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
World.add(world, collider);
|
|
|
|
|
|
|
|
World.add(world,
|
|
|
|
Bodies.circle(400, 40, 30, {
|
|
|
|
render: {
|
|
|
|
strokeStyle: greenColor,
|
|
|
|
fillStyle: 'transparent'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
sceneEvents.push(
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
var renderOptions = engine.render.options;
|
|
|
|
renderOptions.wireframes = false;
|
|
|
|
renderOptions.background = '#222';
|
|
|
|
renderOptions.showAngleIndicator = false;
|
|
|
|
};
|
|
|
|
})();
|