The code below causes the rotateLayer
physics body to fall off the screen after a few seconds. Commenting out the circle physics body, however, keeps rotateLayer
on screen. Is it not possible to add a physics body to a node, and then rotate the parent node? Conceptually, the goal is to rotate the circle around a fixed point, as described here, but attach a physics body to the circle while also controlling rotation through angular velocity as opposed to zRotation.
let size = CGSize(width: 10, height: 250)
rotateLayer = SKNode()
gameLayer.addChild(rotateLayer)
let circleSize = CGSize(width: 10, height: 10)
let circle = SKSpriteNode(color: SKColor.whiteColor(), size: circleSize)
circle.position = CGPoint(x: 0, y: size.height/2 + circleSize.height/2)
circle.physicsBody = SKPhysicsBody(rectangleOfSize: circleSize)
circle.physicsBody?.affectedByGravity = false
circle.physicsBody?.friction = 0
circle.physicsBody?.linearDamping = 0
circle.physicsBody?.angularDamping = 0
circle.physicsBody?.collisionBitMask = 0
circle.physicsBody?.dynamic = false
rotateLayer.addChild(circle)
rotateLayer.physicsBody = SKPhysicsBody(rectangleOfSize: size)
rotateLayer.physicsBody?.affectedByGravity = false
rotateLayer.physicsBody?.friction = 0
rotateLayer.physicsBody?.linearDamping = 0
rotateLayer.physicsBody?.angularDamping = 0
rotateLayer.physicsBody?.angularVelocity = -3.0