Skip to content

Commit

Permalink
added Query.collides, closes #478
Browse files Browse the repository at this point in the history
  • Loading branch information
liabru committed Nov 25, 2017
1 parent 3307760 commit 6593a72
Showing 1 changed file with 36 additions and 18 deletions.
54 changes: 36 additions & 18 deletions src/collision/Query.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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
Expand Down

0 comments on commit 6593a72

Please sign in to comment.