Crashalot
Crashalot

Reputation: 34513

SpriteKit: child does not rotate with parent SKNode

The goal is to rotate a physics body around a point using angular velocity, not zRotation. Because of this, it seems necessary to add a second physics body (the one controlling the rotations), but changing the angular velocity on this parent doesn't move the child. In other words, the parent rotates, but the child remains in place.

How can you rotate both the parent and child?

   parent = SKNode()
   sprite = SKSpriteNode(color: SKColor.whiteColor(), size: spriteSize)
   sprite.position = CGPoint(x: 0, y: 50)
   parent.addChild(sprite)

   parent.physicsBody = SKPhysicsBody(rectangleOfSize: parentSize)
   parent.physicsBody?.affectedByGravity = false
   parent.physicsBody?.friction = 0
   parent.physicsBody?.linearDamping = 0
   parent.physicsBody?.angularDamping = 0
   parent.physicsBody?.collisionBitMask = 0

   sprite.physicsBody = SKPhysicsBody(rectangleOfSize: spriteSize)
   sprite.physicsBody?.affectedByGravity = false
   sprite.physicsBody?.friction = 0
   sprite.physicsBody?.linearDamping = 0
   sprite.physicsBody?.angularDamping = 0
   sprite.physicsBody?.categoryBitMask = bitMask
   sprite.physicsBody?.collisionBitMask = 0

   parent.physicsBody?.angularVelocity = 2.0

Upvotes: 2

Views: 363

Answers (1)

Sergey Kuryanov
Sergey Kuryanov

Reputation: 6114

You can try to create joint between parent and child before setting angular velocity

let fixedJoint = SKPhysicsJointFixed()
fixedJoint.bodyA = parent.physicsBody
fixedJoint.bodyB = sprite.physicsBody
self.physicsWorld.addJoint(fixedJoint)

Upvotes: 1

Related Questions