I have two nodes in Sprite Kit, and I'm writing in Swift.
How would I make the nodes attracted to each other by the force of gravity? Also, how would I use their masses? If an object has a big mass, it should gravitate more.
I have two nodes in Sprite Kit, and I'm writing in Swift.
How would I make the nodes attracted to each other by the force of gravity? Also, how would I use their masses? If an object has a big mass, it should gravitate more.
You can loop through all nodes and calculate the impulse to all other nodes using the appropriate ratios of the universal gravitation equation. I just wrote a quick example showing how this is done. You can make your own custom "mass" factor, however I'm simply using Sprite Kit's. I also added a strength factor to amplify the impulse. I'm also assuming fixed time step of 1/60 seconds.
Instead of performing the calculation manually you could also add field nodes to your physics bodies to simulate the effect. Although be warned, field nodes are broken in certain versions of Sprite Kit.