Skip to content

Commit

Permalink
CCPhysicsBody sleeping fixes and tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
slembcke committed Oct 22, 2014
1 parent 38b688c commit 985467a
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
56 changes: 56 additions & 0 deletions UnitTests/CCPhysicsTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -1112,6 +1112,62 @@ -(void)testApplyForce
[physics onExit];
}

-(void)testBodySleep
{
CCPhysicsNode *physics = [CCPhysicsNode node];
[physics onEnter];

CCNode *staticNode = [CCNode node];
staticNode.physicsBody = [CCPhysicsBody bodyWithCircleOfRadius:1 andCenter:CGPointZero];
staticNode.physicsBody.type = CCPhysicsBodyTypeStatic;
[physics addChild:staticNode];

CCNode *node = [CCNode node];
node.physicsBody = [CCPhysicsBody bodyWithCircleOfRadius:1 andCenter:CGPointZero];
node.physicsBody.mass = 5;

// Bodies default to being active.
XCTAssertFalse(node.physicsBody.sleeping, @"");

// Setting the sleeping property before adding to a scene should be ignored.
node.physicsBody.sleeping = YES;
XCTAssertFalse(node.physicsBody.sleeping, @"");

[physics addChild:node];

node.physicsBody.sleeping = YES;
XCTAssertTrue(node.physicsBody.sleeping, @"");

node.physicsBody.sleeping = NO;
XCTAssertFalse(node.physicsBody.sleeping, @"");

// Changing various flags should wake a body up.
node.physicsBody.sleeping = YES;
XCTAssertTrue(node.physicsBody.sleeping, @"");
node.physicsBody.affectedByGravity = YES;
XCTAssertFalse(node.physicsBody.sleeping, @"");

node.physicsBody.sleeping = YES;
XCTAssertTrue(node.physicsBody.sleeping, @"");
node.physicsBody.mass = 1.0;
XCTAssertFalse(node.physicsBody.sleeping, @"");

// Removing the node from the scene and re-adding it should wake up its body.
node.physicsBody.sleeping = YES;
XCTAssertTrue(node.physicsBody.sleeping, @"");
[node removeFromParent];
[physics addChild:node];
XCTAssertFalse(node.physicsBody.sleeping, @"");

// Adding joints should wake up a body.
node.physicsBody.sleeping = YES;
XCTAssertTrue(node.physicsBody.sleeping, @"");
[CCPhysicsJoint connectedMotorJointWithBodyA:node.physicsBody bodyB:staticNode.physicsBody rate:1.0];
XCTAssertFalse(node.physicsBody.sleeping, @"");

[physics onExit];
}

// TODO
// * Check that body and shape settings are preserved through multiple add/remove cycles and are actually applied to the cpBody.
// * Check that changing properties before and after adding to an active physics node updates the properties correctly.
Expand Down
19 changes: 19 additions & 0 deletions cocos2d/CCPhysicsBody.m
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ -(void)setAffectedByGravity:(BOOL)affectedByGravity
cpBodySetVelocityUpdateFunc(self.body.body, func);

_affectedByGravity = affectedByGravity;
[_body activate];
}

-(BOOL)allowsRotation {
Expand Down Expand Up @@ -367,6 +368,24 @@ -(NSArray *)joints
}

-(BOOL)sleeping {return _body.isSleeping;}
-(void)setSleeping:(BOOL)sleep
{
if(_body.type != CP_BODY_TYPE_DYNAMIC){
CCLOGWARN(@"Warning: [CCPhysicsBody setSleeping:] has no effect on static bodies.");
return;
}

if(_body.space == nil){
CCLOGWARN(@"Warning: [CCPhysicsBody setSleeping:] has no effect on bodies before they are added to a scene.");
return;
}

if(sleep){
[_body sleep];
} else {
[_body activate];
}
}

@end

Expand Down

0 comments on commit 985467a

Please sign in to comment.