Debian Codegraph @

xcode/ObjectiveChipmunkTests/SpaceTest.m

raw
1 /* Copyright (c) 2013 Scott Lembcke and Howling Moon Software
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
4 * of this software and associated documentation files (the "Software"), to deal
5 * in the Software without restriction, including without limitation the rights
6 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 * copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
9 *
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 * SOFTWARE.
20 */
21
22 #import <XCTest/XCTest.h>
23
24 #import "ObjectiveChipmunk/ObjectiveChipmunk.h"
25
26 @interface SpaceTest : XCTestCase {}
27 @end
28
29 @implementation SpaceTest
30
31 #define TestAccessors(o, p, v) o.p = v; XCTAssertEqual(o.p, v, @"");
32 #define TestAccessorsV(o, p, v) o.p = v; XCTAssertTrue(cpveql(o.p, v), @"");
33 #define AssertRetainCount(obj, count) XCTAssertEqual([obj retainCount], (NSUInteger)count, @"")
34
35 -(void)testProperties {
36 ChipmunkSpace *space = [[ChipmunkSpace alloc] init];
37 XCTAssertTrue(cpveql(space.gravity, cpvzero), @"");
38 XCTAssertEqual(space.damping, (cpFloat)1.0, @"");
39 XCTAssertEqual(space.idleSpeedThreshold, (cpFloat)0, @"");
40 XCTAssertEqual(space.sleepTimeThreshold, (cpFloat)INFINITY, @"");
41
42 XCTAssertNotEqual(space.space, NULL, @"");
43 XCTAssertNotNil(space.staticBody, @"");
44
45 TestAccessors(space, iterations, 50);
46 TestAccessorsV(space, gravity, cpv(1,2));
47 TestAccessors(space, damping, (cpFloat)5);
48 TestAccessors(space, idleSpeedThreshold, (cpFloat)5);
49 TestAccessors(space, sleepTimeThreshold, (cpFloat)5);
50
51 [space release];
52 }
53
54 static NSSet *
55 pointQueryInfoToShapes(NSArray *arr)
56 {
57 NSMutableSet *set = [NSMutableSet setWithCapacity:[arr count]];
58 for(ChipmunkPointQueryInfo *info in arr)[set addObject:info.shape];
59 return set;
60 }
61
62 static NSSet *
63 segmentQueryInfoToShapes(NSArray *arr)
64 {
65 NSMutableSet *set = [NSMutableSet setWithCapacity:[arr count]];
66 for(ChipmunkSegmentQueryInfo *info in arr)[set addObject:info.shape];
67 return set;
68 }
69
70 static NSSet *
71 shapeQueryInfoToShapes(NSArray *arr)
72 {
73 NSMutableSet *set = [NSMutableSet setWithCapacity:[arr count]];
74 for(ChipmunkShapeQueryInfo *info in arr)[set addObject:info.shape];
75 return set;
76 }
77
78 static void
79 testPointQueries_helper(id self, ChipmunkSpace *space, ChipmunkBody *body)
80 {
81 ChipmunkShape *circle = [space add:[[ChipmunkCircleShape alloc] initWithBody:body radius:1 offset:cpv(1,1)]];
82 ChipmunkShape *segment = [space add:[[ChipmunkSegmentShape alloc] initWithBody:body from:cpvzero to:cpv(1,1) radius:1]];
83 ChipmunkShape *box = [space add:[[ChipmunkPolyShape alloc] initBoxWithBody:body width:1 height:1 radius:0]];
84
85 NSSet *set;
86 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
87
88 // Point queries
89 set = pointQueryInfoToShapes([space pointQueryAll:cpvzero maxDistance:0.0 filter:CP_SHAPE_FILTER_ALL]);
90 XCTAssertEqualObjects(set, ([NSSet setWithObjects:segment, box, nil]), @"");
91
92 set = pointQueryInfoToShapes([space pointQueryAll:cpv(1,1) maxDistance:0.0 filter:CP_SHAPE_FILTER_ALL]);
93 XCTAssertEqualObjects(set, ([NSSet setWithObjects:circle, segment, nil]), @"");
94
95 set = pointQueryInfoToShapes([space pointQueryAll:cpv(0.4, 0.4) maxDistance:0.0 filter:CP_SHAPE_FILTER_ALL]);
96 XCTAssertEqualObjects(set, ([NSSet setWithObjects:circle, segment, box, nil]), @"");
97
98 set = pointQueryInfoToShapes([space pointQueryAll:cpv(-0.5, -0.5) maxDistance:0.0 filter:CP_SHAPE_FILTER_ALL]);
99 XCTAssertEqualObjects(set, ([NSSet setWithObjects:segment, nil]), @"");
100
101 set = pointQueryInfoToShapes([space pointQueryAll:cpv(-1,-1) maxDistance:0.0 filter:CP_SHAPE_FILTER_ALL]);
102 XCTAssertEqualObjects(set, ([NSSet set]), @"");
103
104 cpSpacePointQuery_b(space.space, cpv(-0.6, -0.6), 0.0, CP_SHAPE_FILTER_ALL, ^(cpShape *shape, cpVect p, cpFloat d, cpVect g){
105 XCTAssertEqual(shape, segment.shape, @"");
106 XCTAssertEqualWithAccuracy(cpvdist(p, cpvnormalize(cpv(-1, -1))), (cpFloat)0.0, 1e-5, @"");
107 XCTAssertEqualWithAccuracy(d, cpfsqrt(2*0.6*0.6) - 1.0f, 1e-5, @"");
108 });
109
110 ChipmunkPointQueryInfo *pointInfo = nil;
111 pointInfo = [space pointQueryNearest:cpv(-3, -3) maxDistance:10 filter:CP_SHAPE_FILTER_ALL];
112 XCTAssertEqual(pointInfo.shape, segment, @"");
113
114 // A point query that misses should return nil.
115 pointInfo = [space pointQueryNearest:cpv(-3, -3) maxDistance:0 filter:CP_SHAPE_FILTER_ALL];
116 XCTAssertNil(pointInfo, @"");
117
118 // Segment queries
119 set = segmentQueryInfoToShapes([space segmentQueryAllFrom:cpv(-2,-2) to:cpv(4,4) radius:0.0 filter:CP_SHAPE_FILTER_ALL]);
120 XCTAssertEqualObjects(set, ([NSSet setWithObjects:circle, segment, box, nil]), @"");
121
122 set = segmentQueryInfoToShapes([space segmentQueryAllFrom:cpv(2,-2) to:cpv(-2,2) radius:0.0 filter:CP_SHAPE_FILTER_ALL]);
123 XCTAssertEqualObjects(set, ([NSSet setWithObjects:segment, box, nil]), @"");
124
125 set = segmentQueryInfoToShapes([space segmentQueryAllFrom:cpv(3,-1) to:cpv(-1,3) radius:0.0 filter:CP_SHAPE_FILTER_ALL]);
126 XCTAssertEqualObjects(set, ([NSSet setWithObjects:circle, segment, nil]), @"");
127
128 set = segmentQueryInfoToShapes([space segmentQueryAllFrom:cpv(2.4,-1.6) to:cpv(-1.6,2.4) radius:0.0 filter:CP_SHAPE_FILTER_ALL]);
129 XCTAssertEqualObjects(set, ([NSSet setWithObjects:circle, segment, box, nil]), @"");
130
131 set = segmentQueryInfoToShapes([space segmentQueryAllFrom:cpv(2,2) to:cpv(3,3) radius:0.0 filter:CP_SHAPE_FILTER_ALL]);
132 XCTAssertEqualObjects(set, ([NSSet set]), @"");
133
134 ChipmunkSegmentQueryInfo *segmentInfo = nil;
135 segmentInfo = [space segmentQueryFirstFrom:cpv(-2,-2) to:cpv(1,1) radius:0.0 filter:CP_SHAPE_FILTER_ALL];
136 XCTAssertEqual(segmentInfo.shape, segment, @"");
137
138 segmentInfo = [space segmentQueryFirstFrom:cpv(-2,-2) to:cpv(-1,-1) radius:0.0 filter:CP_SHAPE_FILTER_ALL];
139 XCTAssertEqualObjects(segmentInfo.shape, nil, @"");
140
141 cpSpaceSegmentQuery_b(space.space, cpv(-1.0, -0.6), cpv(1.0, -0.6), 0.0f, CP_SHAPE_FILTER_ALL, ^(cpShape *shape, cpVect p, cpVect n, cpFloat t){
142 XCTAssertEqual(shape, segment.shape, @"");
143 XCTAssertEqualWithAccuracy(cpvlength(n), 1.0f, 1e-5, @"");
144 XCTAssertEqualWithAccuracy(n.y, -0.6f, 1e-5, @"");
145 XCTAssertEqualWithAccuracy(cpvdist(cpv(-1.0, -0.6), n)/2.0f, t, 1e-5, @"");
146 });
147
148 // Segment queries starting from inside a shape
149 segmentInfo = [space segmentQueryFirstFrom:cpvzero to:cpv(1,1) radius:0.0 filter:CP_SHAPE_FILTER_ALL];
150 XCTAssertEqual(segmentInfo.t, (cpFloat)0, @"Starting inside a shape should return t=0.");
151
152 segmentInfo = [space segmentQueryFirstFrom:cpv(1,1) to:cpvzero radius:0.0 filter:CP_SHAPE_FILTER_ALL];
153 XCTAssertEqual(segmentInfo.t, (cpFloat)0, @"Starting inside a shape should return t=0.");
154
155 segmentInfo = [space segmentQueryFirstFrom:cpv(-0.6, -0.6) to:cpvzero radius:0.0 filter:CP_SHAPE_FILTER_ALL];
156 XCTAssertEqual(segmentInfo.t, (cpFloat)0, @"Starting inside a shape should return t=0.");
157 XCTAssertEqual(segmentInfo.shape, segment, @"Should have picked the segment shape.");
158
159 // A segment query that misses should return nil.
160 segmentInfo = [space segmentQueryFirstFrom:cpv(-10, 0) to:cpv(-10, -10) radius:0.0 filter:CP_SHAPE_FILTER_ALL];
161 XCTAssertNil(segmentInfo, @"");
162
163 // Shape queries
164 ChipmunkBody *queryBody = [ChipmunkBody bodyWithMass:1 andMoment:1];
165 ChipmunkShape *queryShape = [ChipmunkCircleShape circleWithBody:queryBody radius:1 offset:cpvzero];
166
167 queryBody.position = cpvzero;
168 set = shapeQueryInfoToShapes([space shapeQueryAll:queryShape]);
169 XCTAssertEqualObjects(set, ([NSSet setWithObjects:circle, segment, box, nil]), @"");
170
171 queryBody.position = cpv(1,1);
172 set = shapeQueryInfoToShapes([space shapeQueryAll:queryShape]);
173 XCTAssertEqualObjects(set, ([NSSet setWithObjects:circle, segment, box, nil]), @"");
174
175 queryBody.position = cpv(0,-1);
176 set = shapeQueryInfoToShapes([space shapeQueryAll:queryShape]);
177 XCTAssertEqualObjects(set, ([NSSet setWithObjects:segment, box, nil]), @"");
178
179 queryBody.position = cpv(0,-1.6);
180 set = shapeQueryInfoToShapes([space shapeQueryAll:queryShape]);
181 XCTAssertEqualObjects(set, ([NSSet setWithObjects:segment, nil]), @"");
182
183 cpSpaceShapeQuery_b(space.space, queryShape.shape, ^(cpShape *shape, cpContactPointSet *points){
184 XCTAssertEqual(shape, segment.shape, @"");
185 XCTAssertEqual(points->count, 1, @"");
186 });
187
188 queryBody.position = cpv(2,2);
189 set = shapeQueryInfoToShapes([space shapeQueryAll:queryShape]);
190 XCTAssertEqualObjects(set, ([NSSet setWithObjects:circle, segment, nil]), @"");
191
192 queryBody.position = cpv(4,4);
193 set = shapeQueryInfoToShapes([space shapeQueryAll:queryShape]);
194 XCTAssertEqualObjects(set, ([NSSet set]), @"");
195
196 [space remove:circle];
197 [space remove:segment];
198 [space remove:box];
199 [pool release];
200
201 AssertRetainCount(circle, 1);
202 AssertRetainCount(segment, 1);
203 AssertRetainCount(box, 1);
204
205 [circle release];
206 [segment release];
207 [box release];
208 }
209
210 -(void)testPointQueries {
211 ChipmunkSpace *space = [[ChipmunkSpace alloc] init];
212
213 // With static bodies
214 testPointQueries_helper(self, space, space.staticBody);
215 testPointQueries_helper(self, space, [ChipmunkBody staticBody]);
216
217 // Kinematic bodies
218 testPointQueries_helper(self, space, [space add:[ChipmunkBody kinematicBody]]);
219
220 // With regular bodies.
221 testPointQueries_helper(self, space, [space add:[ChipmunkBody bodyWithMass:1 andMoment:1]]);
222
223 // Rogue bodies
224 testPointQueries_helper(self, space, [ChipmunkBody bodyWithMass:1 andMoment:1]);
225
226 [space release];
227 }
228
229 -(void)testShapes {
230 ChipmunkSpace *space = [[ChipmunkSpace alloc] init];
231 ChipmunkShape *shape;
232
233 // Check that static shapes get added correctly
234 shape = [space add:[ChipmunkCircleShape circleWithBody:space.staticBody radius:1 offset:cpvzero]];
235 XCTAssertTrue(cpSpatialIndexContains(space.space->staticShapes, shape.shape, shape.shape->hashid), @"");
236
237 shape = [space add:[ChipmunkCircleShape circleWithBody:[ChipmunkBody staticBody] radius:1 offset:cpvzero]];
238 XCTAssertTrue(cpSpatialIndexContains(space.space->staticShapes, shape.shape, shape.shape->hashid), @"");
239
240 // Check that normal shapes get added correctly
241 ChipmunkBody *rogue = [ChipmunkBody bodyWithMass:1 andMoment:1];
242 shape = [space add:[ChipmunkCircleShape circleWithBody:rogue radius:1 offset:cpvzero]];
243 XCTAssertTrue(cpSpatialIndexContains(space.space->dynamicShapes, shape.shape, shape.shape->hashid), @"");
244
245 ChipmunkBody *normal = [space add:[ChipmunkBody bodyWithMass:1 andMoment:1]];
246 shape = [space add:[ChipmunkCircleShape circleWithBody:normal radius:1 offset:cpvzero]];
247 XCTAssertTrue(cpSpatialIndexContains(space.space->dynamicShapes, shape.shape, shape.shape->hashid), @"");
248
249 [space release];
250 }
251
252 -(void)testBasicSimulation {
253 ChipmunkSpace *space = [[ChipmunkSpace alloc] init];
254 space.gravity = cpv(0, -100);
255
256 [space addBounds:cpBBNew(-50, 0, 50, 100) thickness:1 elasticity:1 friction:1 filter:CP_SHAPE_FILTER_ALL collisionType:nil];
257
258 ChipmunkBody *ball = [space add:[ChipmunkBody bodyWithMass:1 andMoment:cpMomentForCircle(1, 0, 1, cpvzero)]];
259 ball.position = cpv(-10, 10);
260 [space add:[ChipmunkCircleShape circleWithBody:ball radius:1 offset:cpvzero]];
261
262 ChipmunkBody *box = [space add:[ChipmunkBody bodyWithMass:1 andMoment:cpMomentForBox(1, 2, 2)]];
263 box.position = cpv(10, 10);
264 [space add:[ChipmunkPolyShape boxWithBody:box width:2 height:2 radius:0]];
265
266 for(int i=0; i<100; i++) [space step:0.01];
267
268 XCTAssertTrue(cpfabs(ball.position.y - 1) < 1.1*space.collisionSlop, @"");
269 XCTAssertTrue(cpfabs(box.position.y - 1) < 1.1*space.collisionSlop, @"");
270
271 [space release];
272 }
273
274 - (void)testInitialSleepingObjects
275 {
276 // http://www.cocos2d-iphone.org/forum/topic/28896#post-142428
277 ChipmunkSpace *space = [[ChipmunkSpace alloc] init];
278 space.sleepTimeThreshold = 10.0;
279
280 ChipmunkBody *body1 = [space add:[ChipmunkBody bodyWithMass:1.0 andMoment:1.0]];
281 [space add:[ChipmunkCircleShape circleWithBody:body1 radius:1.0 offset:cpvzero]];
282
283 ChipmunkBody *body2 = [space add:[ChipmunkBody bodyWithMass:1.0 andMoment:1.0]];
284 [space add:[ChipmunkCircleShape circleWithBody:body2 radius:1.0 offset:cpvzero]];
285
286 [body1 sleep];
287 [space step:1.0];
288
289 XCTAssertFalse(body1.isSleeping, @"");
290 XCTAssertFalse(body2.isSleeping, @"");
291
292 [space release];
293 }
294
295 -(void)testInitStepFree
296 {
297 // This was crashing on GCC.
298 // Possible uninitialized field?
299 cpSpace *space = cpSpaceNew();
300 cpSpaceStep(space, 1);
301 cpSpaceFree(space);
302 }
303
304 -(bool)beginSleepSensorRemoveBug:(cpArbiter *)arb space:(ChipmunkSpace*)space
305 {
306 // 'b' is the shape we are using to trigger the callback.
307 CHIPMUNK_ARBITER_GET_SHAPES(arb, a, b);
308 NSLog(@"space: %p, arb: %p, a: %p, b: %p", space.space, arb, a.shape, a.body);
309 [space addPostStepRemoval:b];
310
311 return FALSE;
312 }
313
314 static void
315 VerifyContactGraph(id self, ChipmunkBody *body1, ChipmunkBody *body2)
316 {
317 __block int counter = 0;
318
319 [body1 eachArbiter:^(cpArbiter *arb){
320 CHIPMUNK_ARBITER_GET_BODIES(arb, a, b);
321 XCTAssertTrue(a == body1 && b == body2, @"Unexpected contact graph");
322 counter++;
323 }];
324
325 [body2 eachArbiter:^(cpArbiter *arb){
326 CHIPMUNK_ARBITER_GET_BODIES(arb, a, b);
327 XCTAssertTrue(a == body2 && b == body1, @"Unexpected contact graph");
328 counter++;
329 }];
330
331 XCTAssertEqual(counter, 2, @"Wrong number of arbiters in contact graph.");
332 }
333
334 // This one was a doozy to find. :-\
335 // http://chipmunk-physics.net/forum/viewtopic.php?f=1&t=2472&start=40#p10924
336 -(void)testSleepSensorRemoveBug
337 {
338 ChipmunkSpace *space = [[ChipmunkSpace alloc] init];
339 space.sleepTimeThreshold = 0.1;
340
341 ChipmunkBody *body1 = [space add:[ChipmunkBody bodyWithMass:1 andMoment:INFINITY]];
342 [space add:[ChipmunkCircleShape circleWithBody:body1 radius:1 offset:cpvzero]];
343
344 ChipmunkBody *body2 = [space add:[ChipmunkBody bodyWithMass:1 andMoment:INFINITY]];
345 [space add:[ChipmunkCircleShape circleWithBody:body2 radius:1 offset:cpvzero]];
346 body2.position = cpv(0, 1.9);
347
348 for(int i=0; !body1.isSleeping; i++){
349 [space step:0.01];
350 XCTAssertTrue(i < 100, @"body1 failed to fall asleep");
351 }
352
353 XCTAssertTrue(body2.isSleeping, @"body2 is not sleeping");
354 VerifyContactGraph(self, body1, body2);
355
356 // Objects are now sleeping and have their contact graph data all set up carefully.
357
358 NSString *type = @"woo";
359 ChipmunkBody *body3 = [space add:[ChipmunkBody bodyWithMass:1 andMoment:INFINITY]];
360 ChipmunkShape *shape = [space add:[ChipmunkCircleShape circleWithBody:body3 radius:1 offset:cpv(-0.5, 0)]];
361 shape.collisionType = type;
362
363 [space addCollisionHandler:self typeA:nil typeB:type begin:@selector(beginSleepSensorRemoveBug:space:) preSolve:nil postSolve:nil separate:nil];
364
365 // Now step again and shape should get removed.
366 [space step:0.01];
367
368 XCTAssertFalse([space contains:shape], @"'shape' did not get removed.");
369 VerifyContactGraph(self, body1, body2);
370 }
371
372 -(bool)beginSleepActivateOnImpact:(cpArbiter *)arb space:(ChipmunkSpace*)space
373 {
374 // 'b' is the shape we are using to trigger the callback.
375 CHIPMUNK_ARBITER_GET_BODIES(arb, sleeping, awake);
376
377 XCTAssertTrue(sleeping.isSleeping, @"Body 'sleeping' should be sleeping.");
378 XCTAssertFalse(awake.isSleeping, @"Body 'awake' should not be sleeping.");
379
380 [sleeping activate];
381
382 return TRUE;
383 }
384
385 // Causing a sleeping body to be activated from a pre-solve callback causes issues with the deffered waking.
386 // http://chipmunk-physics.net/forum/viewtopic.php?f=1&t=2477
387 -(void)testSleepActivateOnImpact
388 {
389 NSString *sleepType = @"sleeping";
390 NSString *awakeType = @"awake";
391
392 ChipmunkSpace *space = [[ChipmunkSpace alloc] init];
393 space.sleepTimeThreshold = 0.1;
394
395 ChipmunkBody *body1 = [space add:[ChipmunkBody bodyWithMass:1 andMoment:INFINITY]];
396 ChipmunkShape *shape1 = [space add:[ChipmunkCircleShape circleWithBody:body1 radius:1 offset:cpvzero]];
397 shape1.collisionType = sleepType;
398 [body1 sleep];
399
400 ChipmunkBody *body2 = [space add:[ChipmunkBody bodyWithMass:1 andMoment:INFINITY]];
401 ChipmunkShape *shape2 = [space add:[ChipmunkCircleShape circleWithBody:body2 radius:1 offset:cpvzero]];
402 shape2.collisionType = awakeType;
403
404 [space addCollisionHandler:self typeA:sleepType typeB:awakeType begin:@selector(beginSleepActivateOnImpact:space:) preSolve:nil postSolve:nil separate:nil];
405 [space step:0.01];
406
407 XCTAssertFalse(body1.isSleeping, @"body1 did not awake.");
408 XCTAssertFalse(body2.isSleeping, @"body2 did not awake.");
409 }
410
411
412 -(void)testAddBounds
413 {
414 ChipmunkSpace *space = [[ChipmunkSpace alloc] init];
415 NSArray *objs = [space addBounds:cpBBNew(0, 0, 10, 10) thickness:5 elasticity:0 friction:1 filter:CP_SHAPE_FILTER_ALL collisionType:nil];
416 XCTAssertTrue([space contains:objs], @"");
417
418 [space release];
419 }
420 // TODO more sleeping tests
421
422 @end