Skip to content

Commit 2ad5dbd

Browse files
committed
Fix and unit tests for CCPhysicsBody.allowsRotation
Former-commit-id: ef6d7e7
1 parent 3e9dc2c commit 2ad5dbd

File tree

3 files changed

+87
-1
lines changed

3 files changed

+87
-1
lines changed

UnitTests/CCPhysicsTests.m

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,87 @@ -(void)testAffectedByGravity
395395
XCTAssertTrue(node2.position.y == 0.0, @"");
396396
}
397397

398+
-(void)testAllowsRotation
399+
{
400+
CCPhysicsNode *physicsNode = [CCPhysicsNode node];
401+
[physicsNode onEnter];
402+
403+
{
404+
// Regular body.
405+
CCNode *node = [CCNode node];
406+
node.physicsBody = [CCPhysicsBody bodyWithCircleOfRadius:1.0 andCenter:CGPointZero];
407+
XCTAssert(node.physicsBody.allowsRotation == YES, @"");
408+
409+
[physicsNode addChild:node];
410+
XCTAssert(node.physicsBody.allowsRotation == YES, @"");
411+
412+
XCTAssert(node.physicsBody.body.moment < INFINITY, @"");
413+
}{
414+
// Set before adding.
415+
CCNode *node = [CCNode node];
416+
node.physicsBody = [CCPhysicsBody bodyWithCircleOfRadius:1.0 andCenter:CGPointZero];
417+
node.physicsBody.allowsRotation = NO;
418+
XCTAssert(node.physicsBody.allowsRotation == NO, @"");
419+
420+
[physicsNode addChild:node];
421+
XCTAssert(node.physicsBody.allowsRotation == NO, @"");
422+
423+
XCTAssert(node.physicsBody.body.moment == INFINITY, @"");
424+
}{
425+
// Set after adding.
426+
CCNode *node = [CCNode node];
427+
node.physicsBody = [CCPhysicsBody bodyWithCircleOfRadius:1.0 andCenter:CGPointZero];
428+
XCTAssert(node.physicsBody.allowsRotation == YES, @"");
429+
430+
[physicsNode addChild:node];
431+
XCTAssert(node.physicsBody.allowsRotation == YES, @"");
432+
node.physicsBody.allowsRotation = NO;
433+
XCTAssert(node.physicsBody.allowsRotation == NO, @"");
434+
435+
XCTAssert(node.physicsBody.body.moment == INFINITY, @"");
436+
}{
437+
// Set and reverted before adding.
438+
CCNode *node = [CCNode node];
439+
node.physicsBody = [CCPhysicsBody bodyWithCircleOfRadius:1.0 andCenter:CGPointZero];
440+
node.physicsBody.allowsRotation = NO;
441+
XCTAssert(node.physicsBody.allowsRotation == NO, @"");
442+
node.physicsBody.allowsRotation = YES;
443+
XCTAssert(node.physicsBody.allowsRotation == YES, @"");
444+
445+
[physicsNode addChild:node];
446+
XCTAssert(node.physicsBody.allowsRotation == YES, @"");
447+
448+
XCTAssert(node.physicsBody.body.moment < INFINITY, @"");
449+
}{
450+
// Set before and reverted after adding.
451+
CCNode *node = [CCNode node];
452+
node.physicsBody = [CCPhysicsBody bodyWithCircleOfRadius:1.0 andCenter:CGPointZero];
453+
node.physicsBody.allowsRotation = NO;
454+
XCTAssert(node.physicsBody.allowsRotation == NO, @"");
455+
456+
[physicsNode addChild:node];
457+
XCTAssert(node.physicsBody.allowsRotation == NO, @"");
458+
node.physicsBody.allowsRotation = YES;
459+
XCTAssert(node.physicsBody.allowsRotation == YES, @"");
460+
461+
XCTAssert(node.physicsBody.body.moment < INFINITY, @"");
462+
}{
463+
// Set reverted after adding.
464+
CCNode *node = [CCNode node];
465+
node.physicsBody = [CCPhysicsBody bodyWithCircleOfRadius:1.0 andCenter:CGPointZero];
466+
XCTAssert(node.physicsBody.allowsRotation == YES, @"");
467+
468+
[physicsNode addChild:node];
469+
XCTAssert(node.physicsBody.allowsRotation == YES, @"");
470+
node.physicsBody.allowsRotation = NO;
471+
XCTAssert(node.physicsBody.allowsRotation == NO, @"");
472+
node.physicsBody.allowsRotation = YES;
473+
XCTAssert(node.physicsBody.allowsRotation == YES, @"");
474+
475+
XCTAssert(node.physicsBody.body.moment < INFINITY, @"");
476+
}
477+
}
478+
398479
-(void)testBodyType
399480
{
400481
CGPoint points[3] = {};

cocos2d/CCNode.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -973,6 +973,7 @@ -(void)setupPhysicsBody:(CCPhysicsBody *)physicsBody
973973
cpTransform nonRigid = self.nonRigidTransform;
974974
[_physicsBody willAddToPhysicsNode:physics nonRigidTransform:nonRigid];
975975
[physics.space smartAdd:physicsBody];
976+
[_physicsBody didAddToPhysicsNode:physics];
976977

977978
NSArray *joints = physicsBody.joints;
978979
for(NSUInteger i=0, count=joints.count; i<count; i++){

cocos2d/CCPhysicsBody.m

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ @implementation CCPhysicsBody
4242
NSMutableArray *_chipmunkObjects;
4343

4444
BOOL _affectedByGravity;
45+
BOOL _allowsRotation;
4546
}
4647

4748
//MARK: Constructors:
@@ -53,6 +54,7 @@ -(id)initWithShapeList:(CCPhysicsShape *)shapeList
5354
_body.userData = self;
5455

5556
_affectedByGravity = YES;
57+
_allowsRotation = YES;
5658

5759
_chipmunkObjects = [NSMutableArray arrayWithCapacity:2];
5860
[_chipmunkObjects addObject:_body];
@@ -217,7 +219,7 @@ -(void)setAffectedByGravity:(BOOL)affectedByGravity
217219

218220
-(BOOL)allowsRotation {
219221
if(self.type == CCPhysicsBodyTypeDynamic){
220-
return (_body.moment < INFINITY);
222+
return _allowsRotation;
221223
} else {
222224
// The allowsRotation property is only applicable to dynamic bodies.
223225
return NO;
@@ -236,6 +238,8 @@ -(void)setAllowsRotation:(BOOL)allowsRotation
236238
_body.angularVelocity = 0.0;
237239
}
238240
}
241+
242+
_allowsRotation = allowsRotation;
239243
}
240244

241245
static CCPhysicsBodyType ToCocosBodyType[] = {CCPhysicsBodyTypeDynamic, CCPhysicsBodyTypeStatic, CCPhysicsBodyTypeStatic};

0 commit comments

Comments
 (0)