1
votes

I have an interesting problem. I create an SKSpriteNode with associated physics body.

SKTexture *shipTexture = [SKTexture textureWithImageNamed:@"Spaceship"];
self.heroSpriteNode = [HeroSpriteNode spriteNodeWithTexture:shipTexture size:CGSizeMake(40, 35.2)];
self.heroSpriteNode.physicsBody = [SKPhysicsBody bodyWithTexture:shipTexture size:CGSizeMake(40, 35.2)];
self.heroSpriteNode.physicsBody.dynamic = YES;
self.heroSpriteNode.physicsBody.mass = 1000.0;
self.heroSpriteNode.physicsBody.affectedByGravity = NO;
self.heroSpriteNode.physicsBody.allowsRotation = YES;
self.heroSpriteNode.physicsBody.angularDamping = 0.5;
self.heroSpriteNode.anchorPoint = CGPointMake(0.5,0.5);
self.heroSpriteNode.physicsBody.categoryBitMask = heroCategory;
self.heroSpriteNode.physicsBody.contactTestBitMask = groundCategory;
self.heroSpriteNode.physicsBody.collisionBitMask = groundCategory | edgeCategory;

The Scene itself has no gravity.

In the update routine I call applyTorque: to the physics body.

-(void) updateWithDeltaTime:(NSTimeInterval)seconds
{
   CGFloat rotateTorque = self.rotateRate;

   switch (self.rotateForceApplied)
     {
       case leftForceApplied:    
         break;

       case rightForceApplied:
         rotateTorque = -1.0 * rotateTorque;
         break;

      case zeroForceApplied: // separate from default in case behavior should change
         rotateTorque = 0.0;
         break;

      default:
         rotateTorque = 0.0;
         break;

      }

   NSLog(@"before physics position: x %f,  y %f", self.heroSpriteNode.position.x, self.heroSpriteNode.position.y);

   [self.heroSpriteNode.physicsBody applyTorque:rotateTorque];

   NSLog(@"after physics position: x %f,  y %f", self.heroSpriteNode.position.x, self.heroSpriteNode.position.y);

}

The sprite rotates and appears to rotate in place. What it is actually doing is rotating around a center point (so no net lateral movement). This is seen by logging the sprite position before and after applying the torque. No other actions are being applied to the physics body. The movement in position is about 9 points in each direction at most.

Because the camera is pinned by constraints to the "hero" sprite, and the world moves with the camera (the centerOnNode: sample code), this causes the whole world to move in a circular pattern as the sprite spins. The world itself does not spin but moves at the same rate in a circular pattern with the spinning.

With the sprite anchorPoint being 0.5, 0.5 I would think it should rotate around a center point which should not change the position of the sprite.

What would I being doing wrong with this?

If it matters this is on iOS9, Xcode7, running on device not in the simulator. (The iOS9 SpriteKit documentation is publicly available on Apple's website, as is a public beta of iOS9 itself so this should not be breaking any NDA, and I don't think anything here is iOS9 specific anyway)

1

1 Answers

2
votes

Sprite Kit is a physics engine that attempts to mimic characteristics of objects in the real world, so you should expect an object with a physics body in the simulation to behave as they would in nature. In this case, you are rotating a non-uniform physics body that may rotate non-uniformly and, possibly, move over time (unless the body is rotating about its center of mass). If you change the physics body to a circle, the sprite should rotate uniformly and should remain at a fixed location.