3

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)
Nightman
  • 49
  • 1
  • 8

1 Answers1

5

Here's an example of how to set the category and field bit masks:

Set the category bit masks for the gravity fields

gravityField1.categoryBitMask = gravityField1Category
gravityField2.categoryBitMask = gravityField2Category

Set bit masks such that each star is affected by the other star's gravity field but not its own

star1.physicsBody?.fieldBitMask = gravityField2Category
star2.physicsBody?.fieldBitMask = gravityField1Category

Set bit masks so that the planets are affected by both gravity fields

planet.physicsBody?.fieldBitMask = gravityField1Category | gravityField2Category
0x141E
  • 12,613
  • 2
  • 41
  • 54
  • what do the values look like inside gravityField1Category, etc? – Confused Sep 30 '16 at 15:17
  • @Confused those are category values such as 0x1<<0, 0x1<<1, 0x1<<2, etc. – 0x141E Sep 30 '16 at 20:22
  • Ok, thank you. I think. That might be less clear. They look very confusing. Which ones meet the criteria of being AND? – Confused Sep 30 '16 at 22:08
  • @Confused you'll need to set the physics body's `fieldBitMask` to match the field node's category bit mask. For example, if you want a planet node to be affected by a gravity field, set the planet's physics body's `fieldBitMask` to the same value (for example, 0x1<<8) as the gravity field's `categoryBitMask` (0x1<<8). – 0x141E Oct 01 '16 at 00:44
  • Thank you. These numbers are more than a little confusing. But the 'logic' of a "LOGICAL AND" is even more confusing. Is it the same as saying "is equal to"? – Confused Oct 01 '16 at 01:40
  • @Confused I think you mean _bitwise-and_ (`&`) not logical-and (`&&`). The bitwise-and works as follows. If `a = 0b0111` and `b = 0b0011`, then `a & b == 0b0011`. The bits in the same position in both `a` and `b` are set in the answer. The other bits are zero. – 0x141E Oct 01 '16 at 02:04
  • I don't understand any of it. I'm completely... CONFUSED. – Confused Oct 01 '16 at 02:25
  • Do you set these variables to these weird numbers, and then that makes them match? – Confused Oct 01 '16 at 02:25