From 6593a72e46e8436670f5f3d9562e92664b3fc7e3 Mon Sep 17 00:00:00 2001 From: liabru Date: Sat, 25 Nov 2017 17:19:55 +0000 Subject: [PATCH] added Query.collides, closes #478 --- src/collision/Query.js | 54 ++++++++++++++++++++++++++++-------------- 1 file changed, 36 insertions(+), 18 deletions(-) diff --git a/src/collision/Query.js b/src/collision/Query.js index 0b99e9d9..7166478a 100644 --- a/src/collision/Query.js +++ b/src/collision/Query.js @@ -19,35 +19,26 @@ var Vertices = require('../geometry/Vertices'); (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 + * Returns a list of collisions between `body` and `bodies`. + * @method collides + * @param {body} body * @param {body[]} bodies - * @param {vector} startPoint - * @param {vector} endPoint - * @param {number} [rayWidth] * @return {object[]} Collisions */ - Query.ray = function(bodies, startPoint, endPoint, rayWidth) { - rayWidth = rayWidth || 1e-100; - - var rayAngle = Vector.angle(startPoint, endPoint), - 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 = []; + Query.collides = function(body, bodies) { + var collisions = []; for (var i = 0; i < bodies.length; i++) { var bodyA = bodies[i]; - if (Bounds.overlaps(bodyA.bounds, ray.bounds)) { + if (Bounds.overlaps(bodyA.bounds, body.bounds)) { for (var j = bodyA.parts.length === 1 ? 0 : 1; j < bodyA.parts.length; j++) { var part = bodyA.parts[j]; - if (Bounds.overlaps(part.bounds, ray.bounds)) { - var collision = SAT.collides(part, ray); + if (Bounds.overlaps(part.bounds, body.bounds)) { + var collision = SAT.collides(part, body); + if (collision.collided) { - collision.body = collision.bodyA = collision.bodyB = bodyA; collisions.push(collision); break; } @@ -59,6 +50,33 @@ var Vertices = require('../geometry/Vertices'); return collisions; }; + /** + * 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 + * @param {number} [rayWidth] + * @return {object[]} Collisions + */ + Query.ray = function(bodies, startPoint, endPoint, rayWidth) { + rayWidth = rayWidth || 1e-100; + + var rayAngle = Vector.angle(startPoint, endPoint), + 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 = Query.collides(ray, bodies); + + for (var i = 0; i < collisions.length; i += 1) { + var collision = collisions[i]; + collision.body = collision.bodyB = collision.bodyA; + } + + return collisions; + }; + /** * Returns all bodies whose bounds are inside (or outside if set) the given set of bounds, from the given set of bodies. * @method region