radialGravityField goes nuts after a while

343 Views Asked by At

I'm trying to create a SpriteKit game using Swift that looks somewhat like a 2D gravity simulator, but when my objects approach a bit they go to the opposite direction with crazy speeds.

This may be related with the minimumRadius, categoryBitMask or fieldBitMask properties on the field and physicsBody, but I don't know exactly.

Here's the relevant code:

override func mouseDown(theEvent: NSEvent) {

    let location = theEvent.locationInNode(self)

    let sprite = SKShapeNode(circleOfRadius: 5)
    sprite.fillColor = SKColor(red: 0, green: 0.5, blue: 1, alpha: 1)
    sprite.strokeColor = SKColor.clearColor()
    sprite.position = location
    sprite.physicsBody = SKPhysicsBody(circleOfRadius: 5)
    let gravityField = SKFieldNode.radialGravityField()
    //gravityField.minimumRadius = 5
    sprite.addChild(gravityField)

    self.addChild(sprite)
}

And a gif demonstration of the problem:

Problem demonstration

As you can see, the distance that the objects need to move to trigger the issue seems to be random.

0

There are 0 best solutions below