缩放用SKSpriteNodes制作的钟摆

时间:2017-07-22 20:54:08

标签: ios objective-c sprite-kit skphysicsbody skphysicsjoint

我有一个带摆锤的工作钟,但摆臂不能正确伸缩。这是缩放之前的样子......

enter image description here

然后,缩放......

enter image description here

钟面缩小,摆臂(绿线)缩小,但它看起来像是围绕它的中心点而不是脸部中心的支点。我可以通过设置摇摆的anchorPoint来确定缩放比例,使其向一端而不是中心缩放......

swing.anchorPoint = CGPointMake(0, 0.5);

但这样做会导致钟摆无法摆动。 (我认为因为物理力被应用于摆动的anchorPoint,如果我把它移动到一端,则是固定的针锚。)

我怎样才能让手臂向支点伸展并让它摆动?

这是代码......

// in my SKScene
ClockFace *face = [ClockFace face]; // just an SKNode subclass
face.name = @"face";
face.position = CGPointMake(150, 150);
[self addChild:face];

// add the pendulum
SKSpriteNode *fulcrum = [SKSpriteNode spriteNodeWithColor:[UIColor redColor] size:CGSizeMake(5, 5)];
[face addChild:fulcrum];
fulcrum.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:fulcrum.frame.size];
fulcrum.physicsBody.dynamic = NO;

SKSpriteNode *swing = [SKSpriteNode spriteNodeWithColor:[UIColor greenColor] size:CGSizeMake(190, 2)];
[face addChild:swing];
swing.position = CGPointMake(95, 0);
swing.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:swing.frame.size];

SKPhysicsJointPin *pinJoint = [SKPhysicsJointPin jointWithBodyA:fulcrum.physicsBody bodyB:swing.physicsBody anchor:face.position];
[self.physicsWorld addJoint:pinJoint];

2 个答案:

答案 0 :(得分:3)

在Swift 3的例子中:

enter image description here

    let nodeSize = CGSize(width: 10, height: 10)
    let node = SKSpriteNode(color: .red, size: nodeSize)
    node.physicsBody = SKPhysicsBody(rectangleOf: nodeSize)
    node.physicsBody?.isDynamic = false
    self.addChild(node)

    let node2Size = CGSize(width: 60, height: 8)
    let node2 = SKSpriteNode(color: .green, size: node2Size)
    node2.position = CGPoint(x: 5, y: 0)
    node2.anchorPoint = CGPoint(x: 0.0, y: 0.5) // <- New Line
    node2.physicsBody = SKPhysicsBody(rectangleOf: node2Size)
    node2.physicsBody?.mass = 1.0
    self.addChild(node2)

    // Scale Line
    node2.run(SKAction.repeatForever(SKAction.sequence([
        SKAction.scale(to: 0.2, duration: 1.0),
        SKAction.scale(to: 1.5, duration: 0.5),
        ])))
// Anchor Point
        let a = SKPhysicsJointPin.joint(withBodyA: node.physicsBody! , bodyB: node2.physicsBody!, anchor: CGPoint(x: 0.0, y: 0.0))
            self.physicsWorld.add(a)

答案 1 :(得分:1)

缩放很奇怪的原因在于你如何做你的锚点。想象一下带有图钉的橡皮筋。锚点是图钉的位置。现在要扩展。抓住橡皮筋拉。当图钉位于中间位置时,您会发现两侧均匀拉伸(这就是您在y轴上所做的事情)。当你把它放在橡皮筋的左边时,你只能缩放右边(这就是你想要做的)

现在使用PhysicsBody,您需要根据精灵定位点调整实体定位点。要做到这一点,你需要做一些数学运算:

let centerPoint = CGPointMake(sprite.size.width / 2 - (sprite.size.width * sprite.anchorPoint.x), sprite.size.height / 2 - (sprite.size.height * sprite.anchorPoint.y))
 sprite.physicsBody = SKPhysicsBody(rectangleOfSize: sprite.size, center: centerPoint)

使用centerPoint可以移动到身体中心的位置。

// in my SKScene
ClockFace *face = [ClockFace face]; // just an SKNode subclass
face.name = @"face";
face.position = CGPointMake(150, 150);
[self addChild:face];

// add the pendulum
SKSpriteNode *fulcrum = [SKSpriteNode spriteNodeWithColor:[UIColor redColor] size:CGSizeMake(5, 5)];
[face addChild:fulcrum];
fulcrum.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:fulcrum.frame.size];
fulcrum.physicsBody.dynamic = NO;

SKSpriteNode *swing = [SKSpriteNode spriteNodeWithColor:[UIColor greenColor] size:CGSizeMake(190, 2)];
[face addChild:swing];
swing.anchorPoint = CGPointMake(0,0.5);
CGPoint *centerPoint = CGPointMake(swing.size.width / 2 - (swing.size.width * swing.anchorPoint.x), swing.size.height / 2 - (swing.size.height * swing.anchorPoint.y));

swing.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:swing.frame.size center:centerPoint];

SKPhysicsJointPin *pinJoint = [SKPhysicsJointPin jointWithBodyA:fulcrum.physicsBody bodyB:swing.physicsBody anchor:face.position];
[self.physicsWorld addJoint:pinJoint];