Skip to content

Commit 985467a

Browse files
committed
CCPhysicsBody sleeping fixes and tests.
1 parent 38b688c commit 985467a

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

UnitTests/CCPhysicsTests.m

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,6 +1112,62 @@ -(void)testApplyForce
11121112
[physics onExit];
11131113
}
11141114

1115+
-(void)testBodySleep
1116+
{
1117+
CCPhysicsNode *physics = [CCPhysicsNode node];
1118+
[physics onEnter];
1119+
1120+
CCNode *staticNode = [CCNode node];
1121+
staticNode.physicsBody = [CCPhysicsBody bodyWithCircleOfRadius:1 andCenter:CGPointZero];
1122+
staticNode.physicsBody.type = CCPhysicsBodyTypeStatic;
1123+
[physics addChild:staticNode];
1124+
1125+
CCNode *node = [CCNode node];
1126+
node.physicsBody = [CCPhysicsBody bodyWithCircleOfRadius:1 andCenter:CGPointZero];
1127+
node.physicsBody.mass = 5;
1128+
1129+
// Bodies default to being active.
1130+
XCTAssertFalse(node.physicsBody.sleeping, @"");
1131+
1132+
// Setting the sleeping property before adding to a scene should be ignored.
1133+
node.physicsBody.sleeping = YES;
1134+
XCTAssertFalse(node.physicsBody.sleeping, @"");
1135+
1136+
[physics addChild:node];
1137+
1138+
node.physicsBody.sleeping = YES;
1139+
XCTAssertTrue(node.physicsBody.sleeping, @"");
1140+
1141+
node.physicsBody.sleeping = NO;
1142+
XCTAssertFalse(node.physicsBody.sleeping, @"");
1143+
1144+
// Changing various flags should wake a body up.
1145+
node.physicsBody.sleeping = YES;
1146+
XCTAssertTrue(node.physicsBody.sleeping, @"");
1147+
node.physicsBody.affectedByGravity = YES;
1148+
XCTAssertFalse(node.physicsBody.sleeping, @"");
1149+
1150+
node.physicsBody.sleeping = YES;
1151+
XCTAssertTrue(node.physicsBody.sleeping, @"");
1152+
node.physicsBody.mass = 1.0;
1153+
XCTAssertFalse(node.physicsBody.sleeping, @"");
1154+
1155+
// Removing the node from the scene and re-adding it should wake up its body.
1156+
node.physicsBody.sleeping = YES;
1157+
XCTAssertTrue(node.physicsBody.sleeping, @"");
1158+
[node removeFromParent];
1159+
[physics addChild:node];
1160+
XCTAssertFalse(node.physicsBody.sleeping, @"");
1161+
1162+
// Adding joints should wake up a body.
1163+
node.physicsBody.sleeping = YES;
1164+
XCTAssertTrue(node.physicsBody.sleeping, @"");
1165+
[CCPhysicsJoint connectedMotorJointWithBodyA:node.physicsBody bodyB:staticNode.physicsBody rate:1.0];
1166+
XCTAssertFalse(node.physicsBody.sleeping, @"");
1167+
1168+
[physics onExit];
1169+
}
1170+
11151171
// TODO
11161172
// * Check that body and shape settings are preserved through multiple add/remove cycles and are actually applied to the cpBody.
11171173
// * Check that changing properties before and after adding to an active physics node updates the properties correctly.

cocos2d/CCPhysicsBody.m

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ -(void)setAffectedByGravity:(BOOL)affectedByGravity
234234
cpBodySetVelocityUpdateFunc(self.body.body, func);
235235

236236
_affectedByGravity = affectedByGravity;
237+
[_body activate];
237238
}
238239

239240
-(BOOL)allowsRotation {
@@ -367,6 +368,24 @@ -(NSArray *)joints
367368
}
368369

369370
-(BOOL)sleeping {return _body.isSleeping;}
371+
-(void)setSleeping:(BOOL)sleep
372+
{
373+
if(_body.type != CP_BODY_TYPE_DYNAMIC){
374+
CCLOGWARN(@"Warning: [CCPhysicsBody setSleeping:] has no effect on static bodies.");
375+
return;
376+
}
377+
378+
if(_body.space == nil){
379+
CCLOGWARN(@"Warning: [CCPhysicsBody setSleeping:] has no effect on bodies before they are added to a scene.");
380+
return;
381+
}
382+
383+
if(sleep){
384+
[_body sleep];
385+
} else {
386+
[_body activate];
387+
}
388+
}
370389

371390
@end
372391

0 commit comments

Comments
 (0)