@@ -19,35 +19,26 @@ var Vertices = require('../geometry/Vertices');
19
19
( function ( ) {
20
20
21
21
/**
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
24
25
* @param {body[] } bodies
25
- * @param {vector } startPoint
26
- * @param {vector } endPoint
27
- * @param {number } [rayWidth]
28
26
* @return {object[] } Collisions
29
27
*/
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 = [ ] ;
39
30
40
31
for ( var i = 0 ; i < bodies . length ; i ++ ) {
41
32
var bodyA = bodies [ i ] ;
42
33
43
- if ( Bounds . overlaps ( bodyA . bounds , ray . bounds ) ) {
34
+ if ( Bounds . overlaps ( bodyA . bounds , body . bounds ) ) {
44
35
for ( var j = bodyA . parts . length === 1 ? 0 : 1 ; j < bodyA . parts . length ; j ++ ) {
45
36
var part = bodyA . parts [ j ] ;
46
37
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
+
49
41
if ( collision . collided ) {
50
- collision . body = collision . bodyA = collision . bodyB = bodyA ;
51
42
collisions . push ( collision ) ;
52
43
break ;
53
44
}
@@ -59,6 +50,33 @@ var Vertices = require('../geometry/Vertices');
59
50
return collisions ;
60
51
} ;
61
52
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
+
62
80
/**
63
81
* Returns all bodies whose bounds are inside (or outside if set) the given set of bounds, from the given set of bodies.
64
82
* @method region
0 commit comments