Mercurial > projects > chipmunkd
comparison trunk/chipmunkd/cpSpaceQuery.d @ 23:4ceef5833c8c
updated to chipmunk 5.3.3
author | Extrawurst |
---|---|
date | Fri, 10 Dec 2010 02:10:27 +0100 |
parents | df4ebc8add66 |
children | 80058cee1a77 |
comparison
equal
deleted
inserted
replaced
22:ed2c81f3d1df | 23:4ceef5833c8c |
---|---|
26 | 26 |
27 void | 27 void |
28 cpSpacePointQuery(cpSpace *space, cpVect point, cpLayers layers, cpGroup group, cpSpacePointQueryFunc func, void *data) | 28 cpSpacePointQuery(cpSpace *space, cpVect point, cpLayers layers, cpGroup group, cpSpacePointQueryFunc func, void *data) |
29 { | 29 { |
30 pointQueryContext context = {layers, group, func, data}; | 30 pointQueryContext context = {layers, group, func, data}; |
31 cpSpaceHashPointQuery(space.activeShapes, point, cast(cpSpaceHashQueryFunc)&pointQueryHelper, &context); | 31 |
32 cpSpaceHashPointQuery(space.staticShapes, point, cast(cpSpaceHashQueryFunc)&pointQueryHelper, &context); | 32 cpBool locked = space.locked; space.locked = cpTrue; { |
33 cpSpaceHashPointQuery(space.activeShapes, point, cast(cpSpaceHashQueryFunc)&pointQueryHelper, &context); | |
34 cpSpaceHashPointQuery(space.staticShapes, point, cast(cpSpaceHashQueryFunc)&pointQueryHelper, &context); | |
35 } space.locked = locked; | |
33 } | 36 } |
34 | 37 |
35 static void | 38 static void |
36 rememberLastPointQuery(cpShape *shape, cpShape **outShape) | 39 rememberLastPointQuery(cpShape *shape, cpShape **outShape) |
37 { | 40 { |
87 start, end, | 90 start, end, |
88 layers, group, | 91 layers, group, |
89 func, | 92 func, |
90 }; | 93 }; |
91 | 94 |
92 cpSpaceHashSegmentQuery(space.staticShapes, &context, start, end, 1.0f, cast(cpSpaceHashSegmentQueryFunc)&segQueryFunc, data); | 95 cpBool locked = space.locked; space.locked = cpTrue; { |
93 cpSpaceHashSegmentQuery(space.activeShapes, &context, start, end, 1.0f, cast(cpSpaceHashSegmentQueryFunc)&segQueryFunc, data); | 96 cpSpaceHashSegmentQuery(space.staticShapes, &context, start, end, 1.0f, cast(cpSpaceHashSegmentQueryFunc)&segQueryFunc, data); |
97 cpSpaceHashSegmentQuery(space.activeShapes, &context, start, end, 1.0f, cast(cpSpaceHashSegmentQueryFunc)&segQueryFunc, data); | |
98 } space.locked = locked; | |
94 } | 99 } |
95 | 100 |
96 struct segQueryFirstContext { | 101 struct segQueryFirstContext { |
97 cpVect start, end; | 102 cpVect start, end; |
98 cpLayers layers; | 103 cpLayers layers; |
160 | 165 |
161 void | 166 void |
162 cpSpaceBBQuery(cpSpace *space, cpBB bb, cpLayers layers, cpGroup group, cpSpaceBBQueryFunc func, void *data) | 167 cpSpaceBBQuery(cpSpace *space, cpBB bb, cpLayers layers, cpGroup group, cpSpaceBBQueryFunc func, void *data) |
163 { | 168 { |
164 bbQueryContext context = {layers, group, func, data}; | 169 bbQueryContext context = {layers, group, func, data}; |
170 | |
171 cpBool locked = space.locked; space.locked = cpTrue; { | |
165 cpSpaceHashQuery(space.activeShapes, &bb, bb, cast(cpSpaceHashQueryFunc)&bbQueryHelper, &context); | 172 cpSpaceHashQuery(space.activeShapes, &bb, bb, cast(cpSpaceHashQueryFunc)&bbQueryHelper, &context); |
166 cpSpaceHashQuery(space.staticShapes, &bb, bb, cast(cpSpaceHashQueryFunc)&bbQueryHelper, &context); | 173 cpSpaceHashQuery(space.staticShapes, &bb, bb, cast(cpSpaceHashQueryFunc)&bbQueryHelper, &context); |
167 } | 174 } space.locked = locked; |
175 } | |
176 | |
177 //#pragma mark Shape Query Functions | |
178 | |
179 struct shapeQueryContext { | |
180 cpSpaceShapeQueryFunc func; | |
181 void *data; | |
182 cpBool anyCollision; | |
183 } | |
184 | |
185 // Callback from the spatial hash. | |
186 static void | |
187 shapeQueryHelper(cpShape *a, cpShape *b, shapeQueryContext *context) | |
188 { | |
189 // Reject any of the simple cases | |
190 if( | |
191 (a.group && a.group == b.group) || | |
192 !(a.layers & b.layers) || | |
193 a.sensor || b.sensor | |
194 ) return; | |
195 | |
196 cpContact contacts[CP_MAX_CONTACTS_PER_ARBITER]; | |
197 int numContacts = 0; | |
198 | |
199 // Shape 'a' should have the lower shape type. (required by cpCollideShapes() ) | |
200 if(a.klass.type <= b.klass.type){ | |
201 numContacts = cpCollideShapes(a, b, contacts.ptr); | |
202 } else { | |
203 numContacts = cpCollideShapes(b, a, contacts.ptr); | |
204 for(int i=0; i<numContacts; i++) contacts[i].n = cpvneg(contacts[i].n); | |
205 } | |
206 | |
207 if(numContacts){ | |
208 context.anyCollision = cpTrue; | |
209 | |
210 if(context.func){ | |
211 cpContactPointSet set; set.count = numContacts; | |
212 for(int i=0; i<set.count; i++){ | |
213 set.points[i].point = contacts[i].p; | |
214 set.points[i].normal = contacts[i].p; | |
215 set.points[i].dist = contacts[i].dist; | |
216 } | |
217 | |
218 context.func(b, &set, context.data); | |
219 } | |
220 } | |
221 } | |
222 | |
223 cpBool | |
224 cpSpaceShapeQuery(cpSpace *space, cpShape *shape, cpSpaceShapeQueryFunc func, void *data) | |
225 { | |
226 cpBB bb = cpShapeCacheBB(shape); | |
227 shapeQueryContext context = {func, data, cpFalse}; | |
228 | |
229 cpBool locked = space.locked; space.locked = cpTrue; { | |
230 cpSpaceHashQuery(space.activeShapes, shape, bb, cast(cpSpaceHashQueryFunc)&shapeQueryHelper, &context); | |
231 cpSpaceHashQuery(space.staticShapes, shape, bb, cast(cpSpaceHashQueryFunc)&shapeQueryHelper, &context); | |
232 } space.locked = locked; | |
233 | |
234 return context.anyCollision; | |
235 } |