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: -
local targetRadius = levelPropertyData[targetID].widthX / 2 local forceAmount = 100 local deltaX = levelPropertyData[targetID].Object.x - levelPropertyData[movingID].Object.x local deltaY = levelPropertyData[targetID].Object.y - levelPropertyData[movingID].Object.y local xForce = forceAmount \* ((targetRadius - math.abs(deltaX)) / targetRadius) local yForce = forceAmount \* ((targetRadius - math.abs(deltaY)) / targetRadius) if deltaX \< 0 then xForce = xForce - (xForce \* 2) end if deltaY \< 0 then yForce = yForce - (yForce \* 2) end levelPropertyData[movingID].Object:applyForce( xForce,yForce,levelPropertyData[movingID].Object.x,levelPropertyData[movingID].Object.y )
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…