1

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
Community
  • 1
  • 1
Crashalot
  • 33,605
  • 61
  • 269
  • 439
  • Firstly, as per Apple Docs, the most efficient physicsBody for a node would be a circle. I don't see why you would hinder performance by giving a node named 'circle' a rectangular physicsBody. Second, I can see that your rotateLayer is simply an `SKNode`. If you want each child inside the layer to rotate, you will definitely have to give rotateLayer a size (you have not specified any properties for the rotateLayer as far as we can see). Try adding a size to rotateLayer (not its physicsBody, and see what happens). – Andriko13 Apr 29 '15 at 01:27
  • Thanks @Andriko13. How can you add size to a SKNode without making it a different class? – Crashalot Apr 29 '15 at 01:30
  • Do you specifically need to keep it an SKNode? I had a similar problem in one of my games, where I had an airplane made of several components, and I wanted each of them to rotate when the 'airplane' node rotated. Just like you, I made `airplane` an SKNode and added the parts as children, but couldn't rotate the damn thing. Switching the SKNode to SKSpriteNode let me keep all my code (inheritance), set a size, and it rotated. – Andriko13 Apr 29 '15 at 01:36
  • @Andriko13 No, doesn't need to be SKSpriteNode, but the problem isn't that the children fail to rotate ... the circle child rotates, but the parent SKNode (with the circle child) falls off the screen after a few seconds, like it's gradually getting rotated off the screen. – Crashalot Apr 29 '15 at 01:39
  • @Andriko13 Just tried SKNode -> SKSpriteNode, and the problem persists. The parent node, along with the child node, rotate off the screen after a few seconds. – Crashalot Apr 29 '15 at 01:40
  • I see, sorry for the confusion. Can you try setting the `circle.physicsBody?.dynamic` property to `TRUE`? – Andriko13 Apr 29 '15 at 02:01
  • @Andriko13 we tried setting it to true before -- no difference. – Crashalot Apr 29 '15 at 02:02

1 Answers1

0

Adding rotateLayer.collisionBitMask = 0 fixed the problem. No clue why.

Crashalot
  • 33,605
  • 61
  • 269
  • 439