I want all my SKNodes that are being affected from gravity to go to the horizontal Line on my SKScene view and not to the bottom. Something like a line that is a gravity pole. Is there any way to do it?

1 Answers

1
Community On Best Solutions

Have you tried using the SKFieldNode ?

It has various simulations regarding physics; for example, it can create the

class func radialGravityField() -> SKFieldNode

Which does the following:

Creates a field node that accelerates physics bodies toward the field node.

Edit:

Here is an example done in Playground of how an SKFieldNode works with an SKShapeNode

override func didMove(to view: SKView) {
      let sampleNodeRadius: CGFloat = 15
      let gravityNode = SKFieldNode.radialGravityField()
      let sampleNode = SKShapeNode(circleOfRadius: sampleNodeRadius)

      sampleNode.fillColor = .red
      sampleNode.physicsBody = SKPhysicsBody(circleOfRadius: sampleNodeRadius)

      gravityNode.minimumRadius = 1000
      gravityNode.strength = 50

      addChild(gravityNode)
      addChild(sampleNode)

      physicsWorld.gravity = CGVector(dx: 0, dy: 0)

      sampleNode.position = CGPoint(x: -100, y: -100)

      gravityNode.position = CGPoint(x: 0, y: 0)
   }