Skip to content

Commit 35fa912

Browse files
committed
[CCPhysicsBody eachArbiter:] fix and unit tests.
Former-commit-id: 9393904
1 parent 66f0252 commit 35fa912

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

UnitTests/CCPhysicsTests.m

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,45 @@ -(void)testBodyTypeCollisions
731731
[physicsNode onExit];
732732
}
733733

734+
-(void)testBodyEachCollisionPair
735+
{
736+
CCPhysicsNode *physicsNode = [CCPhysicsNode node];
737+
physicsNode.gravity = ccp(0, -100);
738+
739+
CCNode *node1 = [CCNode node];
740+
node1.physicsBody = [CCPhysicsBody bodyWithCircleOfRadius:1.0 andCenter:CGPointZero];
741+
node1.physicsBody.type = CCPhysicsBodyTypeStatic;
742+
[physicsNode addChild:node1];
743+
744+
CCNode *node2 = [CCNode node];
745+
node2.position = ccp(0, 10);
746+
node2.physicsBody = [CCPhysicsBody bodyWithCircleOfRadius:1.0 andCenter:CGPointZero];
747+
node2.physicsBody.type = CCPhysicsBodyTypeDynamic;
748+
[physicsNode addChild:node2];
749+
750+
// Force entering the scene to set up the physics objects.
751+
[physicsNode onEnter];
752+
753+
// Step the physics for a while.
754+
for(int i=0; i<100; i++){
755+
[physicsNode fixedUpdate:1.0/100.0];
756+
}
757+
758+
__block BOOL check = NO;
759+
760+
[node1.physicsBody eachCollisionPair:^(CCPhysicsCollisionPair *pair) {
761+
CCPhysicsShape *a, *b; [pair shapeA:&a shapeB:&b];
762+
check = (
763+
(a.node == node1 && b.node == node2) ||
764+
(b.node == node1 && a.node == node2)
765+
);
766+
}];
767+
768+
XCTAssert(check, @"The objects should have had a collision pair listed between them.");
769+
770+
[physicsNode onExit];
771+
}
772+
734773
// TODO
735774
// * Check that body and shape settings are preserved through multiple add/remove cycles and are actually applied to the cpBody.
736775
// * Check that changing properties before and after adding to an active physics node updates the properties correctly.

cocos2d/CCPhysicsBody.m

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,6 @@ -(void)eachCollisionPair:(void (^)(CCPhysicsCollisionPair *))block
281281
pair.arbiter = arbiter;
282282
block(pair);
283283
});
284-
285-
pair.arbiter = NULL;
286284
}
287285

288286
//MARK: Velocity

cocos2d/CCPhysicsNode.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ @implementation CCPhysicsCollisionPair {
4848
}
4949

5050
-(cpArbiter *)arbiter {return _arbiter;}
51+
-(void)setArbiter:(cpArbiter *)arbiter {_arbiter = arbiter;}
5152

5253
// Check that the arbiter is set and return it.
5354
-(cpArbiter *)arb

0 commit comments

Comments
 (0)