Skip to content

Commit 6593a72

Browse files
committed
added Query.collides, closes #478
1 parent 3307760 commit 6593a72

File tree

1 file changed

+36
-18
lines changed

1 file changed

+36
-18
lines changed

src/collision/Query.js

+36-18
Original file line numberDiff line numberDiff line change
@@ -19,35 +19,26 @@ var Vertices = require('../geometry/Vertices');
1919
(function() {
2020

2121
/**
22-
* Casts a ray segment against a set of bodies and returns all collisions, ray width is optional. Intersection points are not provided.
23-
* @method ray
22+
* Returns a list of collisions between `body` and `bodies`.
23+
* @method collides
24+
* @param {body} body
2425
* @param {body[]} bodies
25-
* @param {vector} startPoint
26-
* @param {vector} endPoint
27-
* @param {number} [rayWidth]
2826
* @return {object[]} Collisions
2927
*/
30-
Query.ray = function(bodies, startPoint, endPoint, rayWidth) {
31-
rayWidth = rayWidth || 1e-100;
32-
33-
var rayAngle = Vector.angle(startPoint, endPoint),
34-
rayLength = Vector.magnitude(Vector.sub(startPoint, endPoint)),
35-
rayX = (endPoint.x + startPoint.x) * 0.5,
36-
rayY = (endPoint.y + startPoint.y) * 0.5,
37-
ray = Bodies.rectangle(rayX, rayY, rayLength, rayWidth, { angle: rayAngle }),
38-
collisions = [];
28+
Query.collides = function(body, bodies) {
29+
var collisions = [];
3930

4031
for (var i = 0; i < bodies.length; i++) {
4132
var bodyA = bodies[i];
4233

43-
if (Bounds.overlaps(bodyA.bounds, ray.bounds)) {
34+
if (Bounds.overlaps(bodyA.bounds, body.bounds)) {
4435
for (var j = bodyA.parts.length === 1 ? 0 : 1; j < bodyA.parts.length; j++) {
4536
var part = bodyA.parts[j];
4637

47-
if (Bounds.overlaps(part.bounds, ray.bounds)) {
48-
var collision = SAT.collides(part, ray);
38+
if (Bounds.overlaps(part.bounds, body.bounds)) {
39+
var collision = SAT.collides(part, body);
40+
4941
if (collision.collided) {
50-
collision.body = collision.bodyA = collision.bodyB = bodyA;
5142
collisions.push(collision);
5243
break;
5344
}
@@ -59,6 +50,33 @@ var Vertices = require('../geometry/Vertices');
5950
return collisions;
6051
};
6152

53+
/**
54+
* Casts a ray segment against a set of bodies and returns all collisions, ray width is optional. Intersection points are not provided.
55+
* @method ray
56+
* @param {body[]} bodies
57+
* @param {vector} startPoint
58+
* @param {vector} endPoint
59+
* @param {number} [rayWidth]
60+
* @return {object[]} Collisions
61+
*/
62+
Query.ray = function(bodies, startPoint, endPoint, rayWidth) {
63+
rayWidth = rayWidth || 1e-100;
64+
65+
var rayAngle = Vector.angle(startPoint, endPoint),
66+
rayLength = Vector.magnitude(Vector.sub(startPoint, endPoint)),
67+
rayX = (endPoint.x + startPoint.x) * 0.5,
68+
rayY = (endPoint.y + startPoint.y) * 0.5,
69+
ray = Bodies.rectangle(rayX, rayY, rayLength, rayWidth, { angle: rayAngle }),
70+
collisions = Query.collides(ray, bodies);
71+
72+
for (var i = 0; i < collisions.length; i += 1) {
73+
var collision = collisions[i];
74+
collision.body = collision.bodyB = collision.bodyA;
75+
}
76+
77+
return collisions;
78+
};
79+
6280
/**
6381
* Returns all bodies whose bounds are inside (or outside if set) the given set of bounds, from the given set of bodies.
6482
* @method region

0 commit comments

Comments
 (0)