diff --git a/src/collision/Query.js b/src/collision/Query.js new file mode 100644 index 0000000..f1e2a70 --- /dev/null +++ b/src/collision/Query.js @@ -0,0 +1,43 @@ +/** +* Functions for performing collision queries +* +* @class Query +*/ + +var Query = {}; + +(function() { + + /** + * Casts a ray segment against a set of bodies and returns all collisions, ray width is optional. Intersection points are not provided. + * @method ray + * @param {body[]} bodies + * @param {vector} startPoint + * @param {vector} endPoint + * @return {object[]} Collisions + */ + Query.ray = function(bodies, startPoint, endPoint, rayWidth) { + var rayAngle = Vector.angle(startPoint, endPoint), + rayWidth = rayWidth || Number.MIN_VALUE, + rayLength = Vector.magnitude(Vector.sub(startPoint, endPoint)), + rayX = (endPoint.x + startPoint.x) * 0.5, + rayY = (endPoint.y + startPoint.y) * 0.5, + ray = Bodies.rectangle(rayX, rayY, rayLength, rayWidth, { angle: rayAngle }), + collisions = []; + + for (var i = 0; i < bodies.length; i++) { + var bodyA = bodies[i]; + + if (Bounds.overlaps(bodyA.bounds, ray.bounds)) { + var collision = SAT.collides(bodyA, ray); + if (collision.collided) { + collision.body = collision.bodyA = collision.bodyB = bodyA; + collisions.push(collision); + } + } + } + + return collisions; + }; + +})(); \ No newline at end of file diff --git a/src/module/Outro.js b/src/module/Outro.js index 3ba5c32..d6fef27 100644 --- a/src/module/Outro.js +++ b/src/module/Outro.js @@ -36,6 +36,7 @@ Matter.Gui = Gui; Matter.Render = Render; Matter.RenderPixi = RenderPixi; Matter.Events = Events; +Matter.Query = Query; // CommonJS module if (typeof exports !== 'undefined') {