Skip to content

Commit f256ba8

Browse files
committed
Fixing an issue with rotating a physics enabled node.
Former-commit-id: 94bcf9b
1 parent 64f0851 commit f256ba8

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

UnitTests/CCPhysicsTests.m

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ -(void)testBasicSequences9
307307

308308
TestBasicSequenceHelper(self, physicsNode, parent, node, body);
309309
}
310-
310+
311311
-(void)testBasicSequences10
312312
{
313313
CCPhysicsNode *physicsNode = [CCPhysicsNode node];
@@ -333,6 +333,74 @@ -(void)testBasicSequences10
333333
TestBasicSequenceHelper(self, physicsNode, parent, node, body);
334334
}
335335

336+
-(void)testBasicSequences11
337+
{
338+
CCPhysicsNode *physicsNode = [CCPhysicsNode node];
339+
340+
CCNode *parent = [CCNode node];
341+
parent.contentSize = CGSizeMake(25, 35);
342+
parent.anchorPoint = ccp(0.3, 0.7);
343+
parent.position = ccp(20, 60);
344+
parent.rotation = -15;
345+
parent.scaleX = 1.5;
346+
parent.scaleY = 8.0;
347+
348+
CCNode *node = [CCNode node];
349+
node.contentSize = CGSizeMake(30, 30);
350+
node.anchorPoint = ccp(0,0);
351+
node.position = ccp(100, 100);
352+
node.rotation = 30;
353+
node.scaleX = 2.0;
354+
node.scaleY = 3.0;
355+
356+
CCPhysicsBody *body = [CCPhysicsBody bodyWithCircleOfRadius:1.0 andCenter:CGPointZero];
357+
body.type = CCPhysicsBodyTypeStatic;
358+
359+
TestBasicSequenceHelper(self, physicsNode, parent, node, body);
360+
}
361+
362+
-(void)testDynamicAnchorPoint
363+
{
364+
CCPhysicsNode *physicsNode = [CCPhysicsNode node];
365+
366+
CCNode *node = [CCNode node];
367+
node.contentSize = CGSizeMake(2, 2);
368+
node.anchorPoint = ccp(0.5, 0.5);
369+
XCTAssert(ccpDistance(node.position, CGPointZero) == 0.0, @"");
370+
371+
node.physicsBody = [CCPhysicsBody bodyWithCircleOfRadius:1.0 andCenter:CGPointZero];
372+
node.physicsBody.type = CCPhysicsBodyTypeDynamic;
373+
XCTAssert(ccpDistance(node.position, CGPointZero) == 0.0, @"");
374+
375+
[physicsNode addChild:node];
376+
[physicsNode onEnter];
377+
XCTAssert(ccpDistance(node.position, CGPointZero) == 0.0, @"");
378+
379+
node.rotation = 90;
380+
XCTAssert(ccpDistance(node.position, CGPointZero) == 0.0, @"");
381+
}
382+
383+
-(void)testStaticAnchorPoint
384+
{
385+
CCPhysicsNode *physicsNode = [CCPhysicsNode node];
386+
387+
CCNode *node = [CCNode node];
388+
node.contentSize = CGSizeMake(2, 2);
389+
node.anchorPoint = ccp(0.5, 0.5);
390+
XCTAssert(ccpDistance(node.position, CGPointZero) == 0.0, @"");
391+
392+
node.physicsBody = [CCPhysicsBody bodyWithCircleOfRadius:1.0 andCenter:CGPointZero];
393+
node.physicsBody.type = CCPhysicsBodyTypeStatic;
394+
XCTAssert(ccpDistance(node.position, CGPointZero) == 0.0, @"");
395+
396+
[physicsNode addChild:node];
397+
[physicsNode onEnter];
398+
XCTAssert(ccpDistance(node.position, CGPointZero) == 0.0, @"");
399+
400+
node.rotation = 90;
401+
XCTAssert(ccpDistance(node.position, CGPointZero) == 0.0, @"");
402+
}
403+
336404
-(void)testCollisionGroups
337405
{
338406
CCPhysicsNode *physicsNode = [CCPhysicsNode node];

cocos2d/CCNode.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,11 @@ -(void) setRotation: (float)newRotation
222222
{
223223
CCPhysicsBody *body = GetBodyIfRunning(self);
224224
if(body){
225+
CGPoint position = self.position;
225226
body.absoluteRadians = -CC_DEGREES_TO_RADIANS(newRotation + NodeToPhysicsRotation(self.parent));
227+
228+
// Rotating the body will cause the node to move unless the CoG is the same as the anchor point.
229+
self.position = position;
226230
} else {
227231
_rotationalSkewX = newRotation;
228232
_rotationalSkewY = newRotation;

0 commit comments

Comments
 (0)