I have the following code that simulates bouncing balls. Although I mentioned that the bodies should be kept within in the self.frame, they leave (the whole ball is not visible anymore) and then comeback. It's as the frame has larger width than the visible borders. However, height is totally fine.
override func didMoveToView(view: SKView) {
self.physicsWorld.gravity = CGVectorMake(0, -9.8)
let sceneBody = SKPhysicsBody(edgeLoopFromRect: frame)
sceneBody.friction = 0
self.physicsBody = sceneBody
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches{
var pos = touch.locationInNode(self)
var ball = SKShapeNode(circleOfRadius: 25)
ball.position = pos
ball.physicsBody = SKPhysicsBody(circleOfRadius: 25)
ball.physicsBody?.affectedByGravity = true
ball.physicsBody?.restitution = 0.9
ball.physicsBody?.linearDamping = 0
self.addChild(ball)
}
}