我正在开发一个带有 swift 和 sprite 工具包的 Xcode 项目,我想知道如何选择受径向重力场影响的对象。我目前有两颗恒星和一些行星,每颗恒星后面都有一个径向重力场。问题是恒星被它们自己的引力场所吸引。我怎样才能使每个重力场只影响一颗恒星和所有行星,而不影响它所跟随的恒星。我知道它与 categoryBitMask 和/或 fieldBitMask 有关,但我不知道具体如何。提前致谢。下面是星星和重力场的代码。我不希望那个重力场影响那颗恒星。
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)