Select which objects in the radial gravity field affect the speed

I'm working on an Xcode project with a bunch of swipes and sprites and I'm wondering how I can choose which objects are affected by ray gravity fields. I currently have two stars and some planets, and each star has a radial gravitational field following it. The problem is that stars are drawn to their own gravitational fields. How can I do this so that each gravity field affects only one star and all the planets, but not the next star. I know it has something to do with categoryBitMask and / or fieldBitMask, but I don't know exactly how to do it. Thank you in advance. Below is the code for the star and gravitational field. I don't want this gravitational field to affect this 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)

      

+3


source to share


1 answer


Here is an example of how to set category and field discharge masks:

Specify Category Bit Masks for Gravity Fields

gravityField1.categoryBitMask = gravityField1Category
gravityField2.categoryBitMask = gravityField2Category

      

Set the bitmasks so that each star is affected by a different stellar gravity field, but not your own



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

      

Set the bitmasks so that the planets are affected by both gravity fields

planet.physicsBody?.fieldBitMask = gravityField1Category | gravityField2Category

      

+4


source







All Articles