缩放用 SKSpriteNodes 制作的钟摆

Scaling a pendulum made with SKSpriteNodes

我有一个带钟摆的工作时钟,但摆臂无法正确缩放。这是缩放前的样子...

然后,缩放后...

钟面缩小了,摆臂也缩小了(绿线),但看起来它是围绕它的中心点而不是钟面中心的支点缩放的。我可以通过设置秋千的 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];

在Swift 3这个例子:

    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)

之所以缩放很奇怪,是因为你做锚点的方式。想象一个带有图钉的橡皮筋。锚点是图钉所在的位置。现在要扩展。只需抓住橡皮筋并拉动即可。当图钉在中间时,你会发现很容易均匀地拉伸两侧(这就是你在 y 轴上所做的)。当你把它放在橡皮筋的左边时,你将只能缩放右边(这就是你想要做的)

现在使用 PhysicsBody,您需要根据 sprite 锚点调整身体锚点。为此,您需要做一些数学运算:

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 可以移动 body 的中心。

// 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];