Inconsistent results with SKPhysicsBody

My goal is to have a formula which, given the three inputs below, generates the necessary CGVector to put an SKPhysicsBody in perfect circular orbit around a gravity field.


Inputs:

- the gravity field's Strength can vary

- the orbiting SKPhysicsBody's Mass can vary

- the orbiting SKPhysicsBody's distance from the gravity field can vary


I have a test program set up to try to find some constants within SKSpriteKit that will help me come up with a formula. In this test program, I do the following:

- create a gravity field in the middle of the viewing area with a Strength of 1

- create an SKPhysicsBody with a Mass of 1

- place the SKPhysicsBody at a certain distance from the gravity field


Through trial-and-error, I find the appropriate ApplyImpulse CGVector that puts the body into a near-perfect circular orbit. After the ApplyImpulse, I calculate the body's distance from the gravity field inside the "Update" method.


But what I am finding is that given the EXACT same inputs, results can vary literally from one attempt to the next. It's very bizarre. The differences aren't huge, but I would think that with all inputs the same, the results would be the same.


Is there some unknown random element to the SpriteKit when it comes to gravity and impulses?

Replies

You're not going to get exact results because it's a physics body simulation, and (while we don't know exactly how the simulation is done) it's a discrete simulation where the properties of physics bodies are recalculated at a periodic interval. So the "random element" is the variance in the time interval resulting from running in a non-real-time process.


It may also be that the simulation is "tuned" for particular ranges of physical properties. You could try a larger mass (e.g. 1000) and different scales of starting distance. The resolution of the simulation may also depend on the dimensions of your SKScene.


>> My goal is to have a formula


How is trial and error going to give you an actual formula? How is a formula with gravity 1 and mass 1 going to help with other gravity and mass values?

AFAIK, SK gravity is constant - something else, perhaps force and/or natural variations in timing are root cause in your example.

Thanks for the response Quincey. Your explaination makes sense. To answer your question, I basically used trend analysis. I used a Gravity strength of 1 and a mass of 1 for the orbiting shape. I then placed the orbiting body at various distances from the gravity (100, 200, 300, up to 1200). For each distance, I found the CGVector which seemed to keep it in a nice circular orbit (consistent radius from the gravity).


Distance of 300 needs a CGVector x/y of 106.0645

Distance of 400 needs a CGVector x/y of 91.8534

Distance of 500 needs a CGVector x/y of 82.1558

etc...


I then plugged the results into Excel to come up with a polynomial function. It's a hideous formula which goes to the 6th degree (ax^6 + bx^5 + cx^4...)


I then did the same analysis for a Gravity of 2, 3, etc. to determine the effect that the gravity strength had.


I did the same yet again to determine how the orbiting body's mass impacted it.


In the end, the hideous formula that I came up with isn't perfect but it should do the trick for what I'm trying to accomplish.

The simpler but related problem — calculating the escape velocity of a projectile — is well known and has a straightforward quadratic solution. I would assume that your problem also has an analytic solution and is also quadratic. (It's actually the same problem, just with a different starting position.)


You might try looking at the derivation of the escape velocity solution, and see if you can follow the same principles to derive your own analytic solution.


Or, if you can frame the problem in the correct terms, an internet search may yield the correct formula.


Or, ask a physicist!


(There's an "It's not rocket science!" joke in here somewhere, too.)