0
0
Fork 0
mirror of https://github.com/liabru/matter-js.git synced 2024-12-26 13:49:01 -05:00
liabru-matter-js/examples/raycasting.js

136 lines
3.9 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.raycasting = function() {
var Engine = Matter.Engine,
Render = Matter.Render,
Runner = Matter.Runner,
2015-08-25 14:12:52 -04:00
Composite = Matter.Composite,
Composites = Matter.Composites,
Common = Matter.Common,
2016-11-20 19:41:57 -05:00
Query = Matter.Query,
MouseConstraint = Matter.MouseConstraint,
Mouse = Matter.Mouse,
2015-08-25 14:12:52 -04:00
Events = Matter.Events,
2016-11-20 19:41:57 -05:00
World = Matter.World,
2015-08-25 14:12:52 -04:00
Vertices = Matter.Vertices,
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: {
width: 800,
height: 600,
2016-11-20 19:41:57 -05:00
showAngleIndicator: true
}
});
Render.run(render);
// create runner
var runner = Runner.create();
Runner.run(runner, engine);
// add bodies
var stack = Composites.stack(20, 20, 12, 4, 0, 0, function(x, y) {
switch (Math.round(Common.random(0, 1))) {
case 0:
if (Common.random() < 0.8) {
return Bodies.rectangle(x, y, Common.random(20, 50), Common.random(20, 50));
} else {
return Bodies.rectangle(x, y, Common.random(80, 120), Common.random(20, 30));
}
case 1:
var sides = Math.round(Common.random(1, 8));
sides = (sides === 3) ? 4 : sides;
return Bodies.polygon(x, y, sides, Common.random(20, 50));
}
});
var star = Vertices.fromPath('50 0 63 38 100 38 69 59 82 100 50 75 18 100 31 59 0 38 37 38'),
concave = Bodies.fromVertices(200, 200, star);
World.add(world, [
stack,
concave,
2016-11-27 20:13:22 -05:00
// walls
Bodies.rectangle(400, 0, 800, 50, { isStatic: true }),
Bodies.rectangle(400, 600, 800, 50, { isStatic: true }),
2016-11-20 19:41:57 -05:00
Bodies.rectangle(800, 300, 50, 600, { isStatic: true }),
Bodies.rectangle(0, 300, 50, 600, { isStatic: true })
]);
Events.on(render, 'afterRender', function() {
var mouse = mouseConstraint.mouse,
context = render.context,
bodies = Composite.allBodies(engine.world),
startPoint = { x: 400, y: 100 },
endPoint = mouse.position;
var collisions = Query.ray(bodies, startPoint, endPoint);
Render.startViewTransform(render);
context.beginPath();
context.moveTo(startPoint.x, startPoint.y);
context.lineTo(endPoint.x, endPoint.y);
if (collisions.length > 0) {
context.strokeStyle = '#fff';
} else {
context.strokeStyle = '#555';
}
context.lineWidth = 0.5;
context.stroke();
for (var i = 0; i < collisions.length; i++) {
var collision = collisions[i];
context.rect(collision.bodyA.position.x - 4.5, collision.bodyA.position.y - 4.5, 8, 8);
}
context.fillStyle = 'rgba(255,165,0,0.7)';
context.fill();
Render.endViewTransform(render);
});
// 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);
2015-08-25 14:12:52 -04:00
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 }
});
2015-08-25 14:12:52 -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);
}
};
};