Problem with accurate simulation of planetary gravity

Just looking for a bit of help.  I have two objects, both circular.  One small, say 30 px in diameter which is dynamic and one larger, say 400 px in diameter which is static.

The system uses the physics engine with 0 gravity to simulate an object in space.  When the object moves towards the planetary surface I detect the collision and I then set-up a timer to apply a force to the smaller object to try and simulate an object falling into the larger object under its gravitational effect.

The algorithm I have come up with is as follows: -

&nbsp;&nbsp;&nbsp;&nbsp;local targetRadius = levelPropertyData[targetID].widthX / 2&nbsp;&nbsp;&nbsp;&nbsp;local forceAmount = 100 &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;local deltaX = levelPropertyData[targetID].Object.x - levelPropertyData[movingID].Object.x &nbsp;&nbsp;&nbsp;&nbsp;local deltaY = levelPropertyData[targetID].Object.y - levelPropertyData[movingID].Object.y &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;local xForce = forceAmount \* ((targetRadius - math.abs(deltaX)) / targetRadius) &nbsp;&nbsp;&nbsp;&nbsp;local yForce = forceAmount \* ((targetRadius - math.abs(deltaY)) / targetRadius)&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;if deltaX \< 0 then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;xForce = xForce - (xForce \* 2) &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;if deltaY \< 0 then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;yForce = yForce - (yForce \* 2) &nbsp;&nbsp;&nbsp;&nbsp;end&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;levelPropertyData[movingID].Object:applyForce( xForce,yForce,levelPropertyData[movingID].Object.x,levelPropertyData[movingID].Object.y ) &nbsp;

This gets called every 150 milliseconds until the collision ends.  I have tried to graduate the force so that the further the object is away from the centre the less the force is, but this graduation is uniformed.  Not sure if that is correct…

It kind of works, but I can get an oscillated effect when the object passes close to centre which can then throw the object out of the area.  It may be correct, it just does not look right…

I have posted a video of the effect on YouTube http://youtu.be/-awD4lxzlv0.  If anyone has any better ideas on how to simulate gravitational effect like this I would be really grateful…

Hi Matt,

I built a demo project for radial gravity some months back. You can view the tutorial, and download the actual project, here:

http://www.coronalabs.com/blog/2013/04/09/physics-radial-gravity-and-predicting-trajectory/

Hope this helps,

Brent Sorrentino

Brent, that is such a better solution!  Thanks a lot.  I have to get my head around the idea of using joints in this way instead of “old world” coding,  Thanks again!

Hi Matt,

I built a demo project for radial gravity some months back. You can view the tutorial, and download the actual project, here:

http://www.coronalabs.com/blog/2013/04/09/physics-radial-gravity-and-predicting-trajectory/

Hope this helps,

Brent Sorrentino

Brent, that is such a better solution!  Thanks a lot.  I have to get my head around the idea of using joints in this way instead of “old world” coding,  Thanks again!