Hartix
Hartix

Reputation: 330

Child node not keeping same physicsBody when added to parent

Using the Xcode SpriteKit Scene editor I have a few objects added to the scene as "obstacles." I like to animate them across the screen. They are not dynamic. To make things easier I thought to add them to an obstacleParent node and just animate that one object. The problem is that adding the obstacles to a parent totally screws up their physics bodies. Meaning the obstacle physics bodies do not match the outline of the sprite.

Upvotes: 0

Views: 288

Answers (1)

Ron Myschuk
Ron Myschuk

Reputation: 6061

I'm not sure if it is a bug in the Scene editor, but adding the physics bodies programmatically yields results as expected.

Some interesting things worth noting are:

the physicsBodies moved with the parent regardless of if they were not Dynamic or not.

I could not get them to overlap each other until I made them all have the same contactTestBitMask

It is difficult to tell in the screen shot but all 3 children kept their physics shapes

If the parent has a physics body and an physics impulse is applied to it, only the parent moves. However setting the position of the parent programmatically moves the parent and child nodesenter image description here

    testObject = SKSpriteNode(texture: nil, color: .greenColor(), size: CGSize(width: 100, height: 100))
    testObject.zPosition = Layer.Controls.rawValue
    testObject.position = CGPoint(x: self.size.width / 2, y: self.size.height / 2)
//        testObject.physicsBody = SKPhysicsBody(circleOfRadius: testObject.width / 2)
//        testObject.physicsBody!.dynamic = true
//        testObject.physicsBody!.affectedByGravity = false
//        testObject.physicsBody!.allowsRotation = true
    addChild(testObject)

    let testerObject = SKSpriteNode(texture: nil, color: .clearColor(), size: CGSize(width: 100, height: 100))
    testerObject.zPosition = Layer.Controls.rawValue
    testerObject.position = CGPoint(x:50, y: 50)

    testerObject.physicsBody = SKPhysicsBody(circleOfRadius: testerObject.width / 2)
    testerObject.physicsBody!.affectedByGravity = false
    testerObject.physicsBody!.dynamic = true
    testerObject.physicsBody!.allowsRotation = true
    testerObject.physicsBody!.collisionBitMask = 0
    testerObject.physicsBody!.contactTestBitMask = 0

    testObject.addChild(testerObject)

    let testerObject2 = SKSpriteNode(texture: nil, color: .clearColor(), size: CGSize(width: 100, height: 100))
    testerObject2.zPosition = Layer.Controls.rawValue
    testerObject2.position = CGPoint(x:-50, y: -50)

    testerObject2.physicsBody = SKPhysicsBody(circleOfRadius: testerObject.width / 2)
    testerObject2.physicsBody!.affectedByGravity = false
    testerObject2.physicsBody!.dynamic = true
    testerObject2.physicsBody!.allowsRotation = true
    testerObject2.physicsBody!.collisionBitMask = 0
    testerObject2.physicsBody!.contactTestBitMask = 0
    testObject.addChild(testerObject2)

    let testerObject3 = SKSpriteNode(texture: nil, color: .clearColor(), size: CGSize(width: 100, height: 100))
    testerObject3.zPosition = Layer.Controls.rawValue
    testerObject3.physicsBody = SKPhysicsBody(circleOfRadius: testerObject.width / 2)
    testerObject3.physicsBody!.affectedByGravity = false
    testerObject3.physicsBody!.dynamic = true
    testerObject3.physicsBody!.allowsRotation = true
    testerObject3.physicsBody!.collisionBitMask = 0
    testerObject3.physicsBody!.contactTestBitMask = 0
    testObject.addChild(testerObject3)

Upvotes: 1

Related Questions