forked from josephg/Chipmunk-js
/
cpSpaceQuery.js
172 lines (144 loc) · 4.92 KB
/
cpSpaceQuery.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/* Copyright (c) 2007 Scott Lembcke
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/// Query the space at a point and call @c func for each shape found.
Space.prototype.pointQuery = function(point, layers, group, func)
{
var helper = function(shape){
if(
!(shape.group && group === shape.group) && (layers & shape.layers) &&
shape.pointQuery(point)
){
func(shape);
}
};
this.lock(); {
this.activeShapes.pointQuery(point, helper);
this.staticShapes.pointQuery(point, helper);
} this.unlock(true);
};
/// Query the space at a point and return the first shape found. Returns null if no shapes were found.
Space.prototype.pointQueryFirst = function(point, layers, group)
{
var outShape = null;
this.pointQuery(point, layers, group, function(shape) {
if(!shape.sensor) outShape = shape;
});
return outShape;
};
/// Perform a directed line segment query (like a raycast) against the space calling @c func for each shape intersected.
Space.prototype.segmentQuery = function(start, end, layers, group, func)
{
var helper = function(shape){
var info;
if(
!(shape.group && group === shape.group) && (layers & shape.layers) &&
(info = shape.segmentQuery(start, end))
){
func(shape, info.t, info.n);
}
return 1;
};
this.lock(); {
this.staticShapes.segmentQuery(start, end, 1, helper);
this.activeShapes.segmentQuery(start, end, 1, helper);
} this.unlock(true);
};
/// Perform a directed line segment query (like a raycast) against the space and return the first shape hit. Returns null if no shapes were hit.
Space.prototype.segmentQueryFirst = function(start, end, layers, group)
{
var out = new SegmentQueryInfo(null, 1, vzero);
var helper = function(shape){
var info;
if(
!(shape.group && group === shape.group) && (layers & shape.layers) &&
!shape.sensor &&
(info = shape.segmentQuery(start, end)) &&
info.t < out.t
){
out = info;
}
return out.t;
};
this.staticShapes.segmentQuery(start, end, 1, helper);
this.activeShapes.segmentQuery(start, end, out.t, helper);
return out;
};
/// Perform a fast rectangle query on the space calling @c func for each shape found.
/// Only the shape's bounding boxes are checked for overlap, not their full shape.
Space.prototype.bbQuery = function(bb, layers, group, func)
{
var helper = function(shape){
if(
!(shape.group && group === shape.group) && (layers & shape.layers) &&
bbIntersects2(bb, shape.bb_l, shape.bb_b, shape.bb_r, shape.bb_t)
){
func(shape);
}
};
this.lock(); {
this.activeShapes.query(bb, helper);
this.staticShapes.query(bb, helper);
} this.unlock(true);
};
/// Query a space for any shapes overlapping the given shape and call @c func for each shape found.
Space.prototype.shapeQuery = function(shape, func)
{
var body = shape.body;
//var bb = (body ? shape.update(body.p, body.rot) : shape.bb);
if(body){
shape.update(body.p, body.rot);
}
var bb = new BB(shape.bb_l, shape.bb_b, shape.bb_r, shape.bb_t);
//shapeQueryContext context = {func, data, false};
var anyCollision = false;
var helper = function(b){
var a = shape;
// Reject any of the simple cases
if(
(a.group && a.group === b.group) ||
!(a.layers & b.layers) ||
a === b
) return;
var contacts;
// Shape 'a' should have the lower shape type. (required by cpCollideShapes() )
if(a.collisionCode <= b.collisionCode){
contacts = cpCollideShapes(a, b);
} else {
contacts = cpCollideShapes(b, a);
for(var i=0; i<contacts.length; i++) contacts[i].n = vneg(contacts[i].n);
}
if(contacts.length){
anyCollision = !(a.sensor || b.sensor);
if(func){
var set = new Array(contacts.length);
for(var i=0; i<contacts.length; i++){
set[i] = new ContactPoint(contacts[i].p, contacts[i].n, contacts[i].dist);
}
func(b, set);
}
}
};
this.lock(); {
this.activeShapes.query(bb, helper);
this.staticShapes.query(bb, helper);
} this.unlock(true);
return anyCollision;
};