Search code examples
swiftsprite-kitskphysicsbodyskshapenode

SKPhysicsBody not lining up with SKShapeNode


I have a circle node created like so:

 let path = CGPathCreateWithEllipseInRect(CGRect(x: 0, y: 150, width: playerWidth, height: playerHeight), nil)
    self.path = path


    fillColor = UIColor.whiteColor()

Whenever I add a physicsBody (circleOfRadius: CGFloat), it refuses to match up with the position of the node and ends up near the bottom left of the simulator screen.

Is there a way I can change the position of the physics body? Will post more code if necessary.


Solution

  • If you want to move physics body do not set the shape's body. Create a SKNode which is attached to the shape, then set the SKNode's physicsBody. To move the body, play around with the position of the SKNode that you created on the shape.

     let shape = SKShapeNode(initialize here)
     let bodyNode = SKNode()
     shape.addChild(bodyNode)
     bodyNode.physicsBody = SKPhysicsBody(initialize here)
    
     // To move the body;
     bodyNode.position = CGPoint(point you wish)