使用'bodyWithTexture'(SpriteKit)改变SKPhysicsBody的中心点

时间:2015-05-21 23:45:56

标签: sprite-kit skspritenode skphysicsbody

似乎Spritekit的API在使用'bodyWithTexture'(或者我可能缺少某些东西)时不允许更改物理主体的中心。

我想知道如何在使用像素精密物理体时解决这个问题。

//Upper Claw
SKSpriteNode *claw = [SKSpriteNode spriteNodeWithImageNamed:BOSS_CLAW_SPRITE];
claw.position = CGPointMake(800, 520);
claw.anchorPoint = CGPointMake(1, .7);

//physics
claw.physicsBody = [SKPhysicsBody bodyWithTexture:claw.texture size:claw.size];

你可以清楚地看到物理学家的中心位置是锚点所在的位置。 enter image description here

或者,bodyWithCircle / bodyWithRectangle具有'center'属性。但是,它不够精确,需要大量代码(不可扩展)。

-(SKPhysicsBody*)getPhysicsForClaw:(BOOL)isUpperClaw
{
     NSInteger reverseConstant = 1;
     if (!isUpperClaw) {
         reverseConstant = -1;
     }
     SKPhysicsBody *clawTip1 = [SKPhysicsBody bodyWithCircleOfRadius:5 center:CGPointMake(-545, -140*reverseConstant)];
     SKPhysicsBody *clawTip2 = [SKPhysicsBody bodyWithCircleOfRadius:6 center:CGPointMake(-540, -130*reverseConstant)];
     SKPhysicsBody *clawTip3 = [SKPhysicsBody bodyWithCircleOfRadius:7 center:CGPointMake(-535, -120*reverseConstant)];
     SKPhysicsBody *clawTip4 = [SKPhysicsBody bodyWithCircleOfRadius:8 center:CGPointMake(-530, -110*reverseConstant)];
     SKPhysicsBody *clawTip5 = [SKPhysicsBody bodyWithCircleOfRadius:8 center:CGPointMake(-525, -100*reverseConstant)];
     SKPhysicsBody *clawTip6 = [SKPhysicsBody bodyWithCircleOfRadius:9 center:CGPointMake(-515, -90*reverseConstant)];
     SKPhysicsBody *clawTip7 = [SKPhysicsBody bodyWithCircleOfRadius:11 center:CGPointMake(-508, -78*reverseConstant)];
     SKPhysicsBody *clawTip8 = [SKPhysicsBody bodyWithCircleOfRadius:12 center:CGPointMake(-495, -65*reverseConstant)];
     SKPhysicsBody *clawTip9 = [SKPhysicsBody bodyWithCircleOfRadius:13 center:CGPointMake(-480, -50*reverseConstant)];
     SKPhysicsBody *clawTip10 = [SKPhysicsBody bodyWithCircleOfRadius:14 center:CGPointMake(-465, -35*reverseConstant)];

     SKPhysicsBody *clawTeeth1 = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(6, 40) center:CGPointMake(-433, -70)];
     SKPhysicsBody *clawTeeth2 = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(15, 20) center:CGPointMake(-420, -60)];
     SKPhysicsBody *clawTeeth3 = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(9, 40) center:CGPointMake(-395, -70)];
     SKPhysicsBody *clawTeeth4 = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(15, 20) center:CGPointMake(-382, -60)];
     SKPhysicsBody *clawTeeth5 = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(9, 40) center:CGPointMake(-345, -70)];
     SKPhysicsBody *clawTeeth6 = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(10, 20) center:CGPointMake(-334, -60)];
     SKPhysicsBody *clawTeeth7 = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(9, 30) center:CGPointMake(-295, -60)];
     SKPhysicsBody *clawTeeth8 = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(9, 30) center:CGPointMake(-255, -42)];

     SKPhysicsBody *clawBody1 = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(100, 45) center:CGPointMake(-400, -30)];
     SKPhysicsBody *clawBody2 = [SKPhysicsBody bodyWithCircleOfRadius:26 center:CGPointMake(-325, -25*reverseConstant)];
     SKPhysicsBody *clawBody3 = [SKPhysicsBody bodyWithCircleOfRadius:28 center:CGPointMake(-290, -12*reverseConstant)];
     SKPhysicsBody *clawBody4 = [SKPhysicsBody bodyWithCircleOfRadius:29 center:CGPointMake(-250, 0*reverseConstant)];
     SKPhysicsBody *clawBody5 = [SKPhysicsBody bodyWithCircleOfRadius:28 center:CGPointMake(-210, 10*reverseConstant)];
     SKPhysicsBody *clawBody6 = [SKPhysicsBody bodyWithCircleOfRadius:30 center:CGPointMake(-165, 24*reverseConstant)];

     SKPhysicsBody *clawBase1 = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(120, 55) center:CGPointMake(-75, 24)];

     SKPhysicsBody *claw = [SKPhysicsBody bodyWithBodies:@[clawTip1, clawTip2, clawTip3, clawTip4, clawTip5, clawTip6, clawTip7, clawTip8, clawTip9, clawTip10, clawTeeth1, clawTeeth2, clawTeeth3, clawTeeth4, clawTeeth5, clawTeeth6, clawTeeth7, clawTeeth8, clawBody1, clawBody2, clawBody3, clawBody4, clawBody5, clawBody6, clawBase1]];

     return claw;
 }

enter image description here

3 个答案:

答案 0 :(得分:3)

我对这种情况的解决方案是使用不可见的SKNode。

让爪子没有改变锚点,使其与身体对齐。然后将该节点父节点转换为空白SKNode并相应地移动爪。现在,通过旋转空白SKNode,您可以获得更改锚点的效果,同时物理体仍然排列。

答案 1 :(得分:2)

锚点对物理体没有影响。有几个物理实体可以为其定义中心点。

(SKPhysicsBody *)bodyWithCircleOfRadius:(CGFloat)r
                                 center:(CGPoint)center

(SKPhysicsBody *)bodyWithRectangleOfSize:(CGSize)s
                                  center:(CGPoint)center

不幸的是bodyWithTexture:没有这样的能力。作为一个黑客,您可以使用多个不同大小的矩形,将它们旋转到所需的角度,并使用(SKPhysicsBody *)bodyWithBodies:(NSArray *)bodies将它们连接在一起。这将让你几乎覆盖你的纹理。

另外一个好处是,使用矩形代替bodyWithTexture也不会给你的FPS带来负担。

答案 2 :(得分:1)

primaryartemis解决方案中的代码示例,适用于圆形和矩形旁边的任何其他形状,更实用。

换句话说,我们使用SKNode来包含我们的精灵, SKNode的位置将像anchorPoint ,我们移动精灵(s) )远离这个SKNode容器到我们希望转变为预期工作的愿望位置。

这是一个图形,一个简单的半圆弧,大小为(宽度:100,高度:50),我们希望这个图形在anchorPoint(0.5,0.0)处旋转。

semi_arc

class SemiArc: SKNode {

    var sprite: SKSpriteNode

    override init() {
        sprite = SKSpriteNode(imageNamed: "semi_arc")

        super.init()

        // Move away from this container SKNode, this SKNode's position act like anchorPoint
        sprite.position.y = sprite.size.height / 2

        // The physicsBody will move along as well
        sprite.physicsBody = SKPhysicsBody(texture: sprite.texture!, size: sprite.size)

        // Add the sprite to this container SKNode
        addChild(sprite)

        // To test the transformation from the anchor point we desire
        let rotationForever = SKAction.repeatActionForever(SKAction.rotateByAngle(CGFloat(M_PI) * 2, duration: 2.0))
        runAction(rotationForever)

    }

}

然后,我们可以将此SemiArc SKNode放置到我们的场景中,它将锚定在我们想要的位置。

我们可以创建这个SKNode容器的帮助器,这样我们就可以更轻松地将它重用于简单的精灵。

extension SKNode {
    class func containerNodeWithSprite(sprite: SKSpriteNode, withAnchorPoint anchorPoint: CGPoint) -> SKNode {
        let containerNode = SKNode()

        sprite.position.x = (sprite.size.width / 2) - ( sprite.size.width * anchorPoint.x)
        sprite.position.y = (sprite.size.height / 2) - ( sprite.size.height * anchorPoint.y)

        containerNode.addChild(sprite)

        return containerNode
    }
}

使用示例:

// Somewhere in a scene class

let sprite = SKSpriteNode(imageNamed: "semi_arc")
sprite.physicsBody = SKPhysicsBody(texture: sprite.texture!, size: sprite.size)

let container = SKNode.containerNodeWithSprite(sprite, withAnchorPoint: CGPointMake(0.5 , 0.0))

container.position = view!.center

let rotationForever = SKAction.repeatActionForever(SKAction.rotateByAngle(CGFloat(M_PI) * 2, duration: 2.0))
containedNode.runAction(rotationForever)

addChild(container)

警告&解决方法

我发现这个解决方案的一个警告,就是当你想要根据物理模拟位置删除这个SKNode时不会起作用,因为你没有给容器SKNode一个物理身体,所以它永远不会改变它的立场。检查它的逻辑超出范围将无效,因为您正在检查此容器。当你检测到接触和碰撞时,这也是一样的,它是容器内的物理连接节点导致接触和碰撞,而不是容器本身。但是您仍然可以对接触和碰撞做出正确的反应,只有在您要删除此SKNode时才会出现此问题

解决方法1:计算其子节点与物理主体和容器位置的相对位置。

enumerateChildNodesWithName("fallingObjectWithMiddleBottomAnchor") {
  (node, stop) in

  if ((node.children.first?.position.y)! + node.position.y) < 0 {
    node.removeFromParent()
  }
}

解决方法2 :不要将它用于动态正文,因为它的属性应该是模拟的,而不是手动更新,所以不要弄乱动态主体的锚点。

解决方法3:更好的解决方案是使用SKConstraint来实现您的需求。