SpriteKit SKPhysicsBody applyTorque 导致精灵位置改变

SpriteKit SKPhysicsBody applyTorque causes sprite position to change

我有一个有趣的问题。我创建了一个带有相关物理体的 SKSpriteNode。

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;

场景本身没有重力。

在更新例程中,我调用 applyTorque: 物理体。

-(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);

}

精灵旋转,似乎原地旋转。它实际做的是围绕一个中心点旋转(因此没有净横向运动)。这可以通过在施加扭矩之前和之后记录精灵位置来查看。没有其他动作应用于物理体。位置的移动最多是每个方向9点左右。

因为相机被约束固定在 "hero" 精灵上,而世界随着相机移动(centerOnNode:示例代码),这导致整个世界像精灵一样以圆形模式移动旋转。世界本身并不自转,而是随着自转以相同的速度做圆周运动。

精灵 anchorPoint 为 0.5, 0.5 我认为它应该围绕中心点旋转,这不应该改变精灵的位置。

我这样做有什么问题吗?

如果重要,这是在 iOS9、Xcode7、运行 设备上而不是在模拟器中。 (iOS9 SpriteKit 文档 public 仅在 Apple 网站上可用,public 测试版 iOS9 本身也是如此,因此这不应该违反任何保密协议,而且我不无论如何,我认为这里的任何内容都是 iOS9 具体的)

Sprite Kit 是一种物理引擎,它试图模仿现实世界中物体的特性,因此您应该期望在模拟中具有物理体的物体表现得像它们在自然界中的行为。在这种情况下,您正在旋转一个不均匀的物理物体,它可能会不均匀地旋转,并且可能会随时间移动(除非该物体围绕其质心旋转)。如果将物理体更改为圆形,精灵应均匀旋转并保持在固定位置。