Ok so from the SKFieldNode documentation, under the region property:
A field node applies its effect to all physics bodies that are partially or completely inside its region. The default value is a region of infinite size.
From this I understand that once a part of the physics body attached to a node enters the SKRegion on the field node ("partially or complete inside the region"), the field will start acting on the body.
So I started using the fields but came across the fact that they only start acting on the physics body when the body's center is within the region, not any part of the body.
Here is the code I wrote:
import SpriteKit
class GameScene: SKScene {
override func didMoveToView(view: SKView) {
backgroundColor = SKColor.whiteColor()
self.physicsWorld.gravity = CGVector(dx: 0, dy: 0)
let field = SKFieldNode.springField()
field.position = view.center
let shape = SKShapeNode(circleOfRadius: size.height / 3)
shape.fillColor = SKColor.grayColor()
field.region = SKRegion(path: shape.path!)
field.addChild(shape)
addChild(field)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
/* Called when a touch begins */
for touch in touches {
let location = touch.locationInNode(self)
let ball = SKShapeNode(circleOfRadius: size.height / 10)
ball.position = location
ball.strokeColor = SKColor.blackColor()
ball.physicsBody = SKPhysicsBody(circleOfRadius: size.height / 10)
let center = SKShapeNode(circleOfRadius: 1)
center.strokeColor = SKColor.blackColor()
ball.addChild(center)
self.addChild(ball)
ball.physicsBody!.applyImpulse(CGVectorMake(60, 60))
}
}
}
(I added the shape node to the field to see where it`s region ends, and added the center to the ball to make it clear when it runs that the field only acts once the center is in the region.)
When I run it the field only starts acting on the balls when their center is within the region. It seems to me this is not what should happen based on the documentation. I haven't changed any of the field node's properties before adding it, so I expected the standard behavior.
I don't know it I am misinterpreting the documentation or there is some other property I have to set on the field for it to act on any physics body that is partially within its region.
A walkaround to let
ballbe effected byfieldwhen they touched immediately is to enlarge the region (radius) offield. Something like: