var Example = Example || {};

Example.raycasting = function() {
    var Engine = Matter.Engine,
        Render = Matter.Render,
        Runner = Matter.Runner,
        Composite = Matter.Composite,
        Composites = Matter.Composites,
        Common = Matter.Common,
        Query = Matter.Query,
        MouseConstraint = Matter.MouseConstraint,
        Mouse = Matter.Mouse,
        Events = Matter.Events,
        Vertices = Matter.Vertices,
        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,
            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));
        }
    });

    // for testing raycasting on concave bodies
    Common.setDecomp(require('poly-decomp'));

    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);
    
    Composite.add(world, [
        stack, 
        concave,
        // walls
        Bodies.rectangle(400, 0, 800, 50, { isStatic: true }),
        Bodies.rectangle(400, 600, 800, 50, { isStatic: true }),
        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
                }
            }
        });

    Composite.add(world, mouseConstraint);

    // 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: 800, 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);
        }
    };
};

Example.raycasting.title = 'Raycasting';
Example.raycasting.for = '>0.16.1';

if (typeof module !== 'undefined') {
    module.exports = Example.raycasting;
}

@@ -2129,24 +2129,86 @@
 %7D)%0A    %5D);%0A%0A
+    var collisions,%0A        startPoint = %7B x: 400, y: 100 %7D;%0A%0A
     Events.o
@@ -2213,22 +2213,22 @@
 .on(
-r
 en
-der
+gine
 , 'after
 Rend
@@ -2223,22 +2223,22 @@
 , 'after
-Render
+Update
 ', funct
@@ -2292,46 +2292,8 @@
 se,%0A
-            context = render.context,%0A
     
@@ -2360,40 +2360,57 @@
     
-start
+end
 Point =
+ mouse.position %7C%7C
  %7B x: 
-4
+1
 00, y: 
-1
+6
 00 %7D
-,
+;%0A
 %0A   
@@ -2418,52 +2418,8 @@
     
-    endPoint = mouse.position;%0A%0A        var 
 coll
@@ -2462,24 +2462,225 @@
 t, endPoint)
+;%0A    %7D);%0A%0A    Events.on(render, 'afterRender', function() %7B%0A        var mouse = mouseConstraint.mouse,%0A            context = render.context,%0A            endPoint = mouse.position %7C%7C %7B x: 100, y: 600 %7D
 ;%0A%0A        R

change raycasting example events to enable use in tests