1 year ago

#378665

test-img

Bo King

How to calculate the center of mass of node tree in swift spritekit

I'm working on swift spritekit, I need to calculate the center of masses of children in a parent node now. This image shows what I want to get. image. parent node is radialGravityField of SKFieldNode in SKScene.

open lazy var magneticField: SKFieldNode = { [unowned self] in
        let field = SKFieldNode.radialGravityField()
        self.addChild(field)
        return field
    }()

And this is physicsBody of child node.

public func regeneratePhysicsBody(withPath path: CGPath) {
        self.physicsBody = {
          var transform = CGAffineTransform.identity.scaledBy(x: marginScale, y: marginScale)
          let body = SKPhysicsBody(polygonFrom: path.copy(using: &transform)!)
          body.allowsRotation = false
          body.friction = 0
          body.linearDamping = 3
          return body
        }()
    }
    

I couldn't find any way to calculate the center of masses of children.

swift

sprite-kit

0 Answers

Your Answer

Accepted video resources