I'm working on an Xcode project with swift and sprite kit, and I'm wondering how I can choose which objects are affected by radial gravity fields. I currently have two stars and some planets, and each star has a radial gravity field following it. The problem is that the stars are drawn to their own gravity fields. How can I make it so that each gravity field only affects one star and all the planets, but not the star it's following. I know that it has something to do with categoryBitMask and/or fieldBitMask, but I don't know exactly how. Thanks in advance. Below is the code for a star and a gravity field. I don't want that gravity field to affect that star.
class star: SKSpriteNode {
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
init(imageNamed: String){
let imageTexture = SKTexture(imageNamed: imageNamed)
super.init(texture: imageTexture, color: nil, size: imageTexture.size())
let radius = self.size.width / 2
self.physicsBody = SKPhysicsBody(circleOfRadius: radius )
physicsBody?.dynamic = false
}
}
let star1 = star(imageNamed: "star")
let gravityField1 = SKFieldNode.radialGravityField()
star1.position = CGPoint(x: self.size.width / 4, y: self.size.height / 2)
star1.physicsBody?.friction = 0
star1.physicsBody?.linearDamping = 0
star1.physicsBody?.angularDamping = 0
star1.physicsBody?.angularVelocity = 0.2
star1.zPosition = 1
star1.name = "star"
addChild(star1)
gravityField1.enabled = true;
gravityField1.position = CGPoint(x: self.size.width / 4, y: self.size.height / 2)
gravityField1.strength = Float(pow(radius1, 2)) * pow(10, -3)
addChild(gravityField1)
Here's an example of how to set the category and field bit masks:
Set the category bit masks for the gravity fields
Set bit masks such that each star is affected by the other star's gravity field but not its own
Set bit masks so that the planets are affected by both gravity fields