Is it possible to rotate a Node around an arbitrary point in SpriteKit?

后端 未结 4 1351
猫巷女王i
猫巷女王i 2020-12-15 19:51

Is there a way to rotate a Node in SpriteKit around an arbitrary point? I now I can manipulate the anchorPoint of my Node, but that is not suff

4条回答
  •  南笙
    南笙 (楼主)
    2020-12-15 20:50

    I think the best way to make this work is through two SKNode and joint them with SKPhysicsJointPin (Look at the pin example below)

    I tried to hang a door sign (SKSpriteNode) on my door(`SkScene), and would like to rotate around on the hanging spot when someone touch it

    What I did is making a 1x1 SKNode with a HUGH mass and disabled it's gravity effects.

    var doorSignAnchor = SKSpriteNode(color: myUIColor, size: CGSize(width: 1, height: 1))
    doorSignAnchor.physicsBody = SKPhysicsBody(rectangleOf: doorSignAnchor.frame.size)
    doorSignAnchor.physicsBody!.affectedByGravity = false // MAGIC PART
    doorSignAnchor.physicsBody!.mass = 9999999999 // MAGIC PART
    
    var doorSignNode = SKSpriteNode(imageNamed:"doorSign")
    doorSignNode.physicsBody = SKPhysicsBody(rectangleOf: doorSignNode.frame.size)
    

    and created a SKPhysicsJointPin to connect them all

    let joint = SKPhysicsJointPin.joint(
          withBodyA:  doorSignAnchor.physicsBody!,
          bodyB: doorSignNode.physicsBody!,
          anchor: doorSignAnchor.position)
    mySkScene.physicsWorld.add(joint)
    

    So it will move like actual door sign, rotate around an arbitrary point (doorSignAnchor)

    Reference:

    • Official document about Sumulating Physics
    • How to Make Hanging Chains With SpriteKit Physis Joints

提交回复
热议问题